From 88c30e016beb51dc988058318961caf8f269d76f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20Sch=C3=B6nfelder?= <carl@schonfelder.se> Date: Thu, 1 Apr 2021 17:25:13 +0200 Subject: [PATCH] add: crud-api for City --- server/app/apis/misc.py | 36 ++++++++++++++++++++++++++++-- server/app/core/controller/edit.py | 9 +++++--- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/server/app/apis/misc.py b/server/app/apis/misc.py index d88c6ae3..d44c26ee 100644 --- a/server/app/apis/misc.py +++ b/server/app/apis/misc.py @@ -1,7 +1,8 @@ +import app.core.controller as dbc from app.core.dto import MiscDTO from app.core.models import City, MediaType, QuestionType, Role from flask_jwt_extended import jwt_required -from flask_restx import Resource +from flask_restx import Resource, reqparse api = MiscDTO.api @@ -11,6 +12,10 @@ role_model = MiscDTO.role_model city_model = MiscDTO.city_model +name_parser = reqparse.RequestParser() +name_parser.add_argument("name", type=str, required=True, location="json") + + @api.route("/media_types") class MediaTypeList(Resource): @jwt_required @@ -36,8 +41,35 @@ class RoleList(Resource): @api.route("/cities") -class CityList(Resource): +class CitiesList(Resource): @jwt_required @api.marshal_with(city_model) def get(self): return City.query.all() + + @jwt_required + @api.marshal_with(role_model) + def post(self): + args = name_parser.parse_args(strict=True) + dbc.add.default(City(args["name"])) + return City.query.all() + + +@api.route("/cities/<ID>") +@api.param("ID") +class Cities(Resource): + @jwt_required + @api.marshal_with(city_model) + def put(self, ID): + item = City.query.filter(City.id == ID).first() + args = name_parser.parse_args(strict=True) + item.name = args["name"] + dbc.edit.default(item) + return City.query.all() + + @jwt_required + @api.marshal_with(city_model) + def delete(self, ID): + item = City.query.filter(City.id == ID).first() + dbc.delete.default(item) + return City.query.all() diff --git a/server/app/core/controller/edit.py b/server/app/core/controller/edit.py index bd6255a7..77c726a7 100644 --- a/server/app/core/controller/edit.py +++ b/server/app/core/controller/edit.py @@ -1,7 +1,10 @@ -import math - from app.core import db -from app.core.models import Blacklist, City, Competition, Role, Slide, User + + +def default(item): + db.session.commit() + db.session.refresh(item) + return item def switch_order(item1, item2): -- GitLab