From 00cacf77b7e50cfb85421eb2d6ae47056ca6425d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Sun, 13 Feb 2022 22:32:40 +0100 Subject: [PATCH] all resources done --- mealapi/app.py | 3 +- mealapi/resources/__init__.py | 3 +- .../{mealresource.py => mealbaseresource.py} | 2 +- mealapi/resources/mealindividualresource.py | 32 +++++++++++++++++++ mealapi/schemas/mealschema.py | 2 +- 5 files changed, 38 insertions(+), 4 deletions(-) rename mealapi/resources/{mealresource.py => mealbaseresource.py} (93%) create mode 100644 mealapi/resources/mealindividualresource.py diff --git a/mealapi/app.py b/mealapi/app.py index 1b29d85..c5d87f6 100644 --- a/mealapi/app.py +++ b/mealapi/app.py @@ -5,7 +5,7 @@ from healthcheck import HealthCheck from utils import Config, register_all_error_handlers, health_database_status from model import db from schemas import ma -from resources import MealBaseResource +from resources import MealBaseResource, MealIndividualResource app = Flask(__name__) app.config.from_object(Config) @@ -22,6 +22,7 @@ def init_db(): api.add_resource(MealBaseResource, "/meals") +api.add_resource(MealIndividualResource, "/meals/") health.add_check(health_database_status) diff --git a/mealapi/resources/__init__.py b/mealapi/resources/__init__.py index ab9c64b..e104f82 100644 --- a/mealapi/resources/__init__.py +++ b/mealapi/resources/__init__.py @@ -1 +1,2 @@ -from .mealresource import MealBaseResource +from .mealbaseresource import MealBaseResource +from .mealindividualresource import MealIndividualResource diff --git a/mealapi/resources/mealresource.py b/mealapi/resources/mealbaseresource.py similarity index 93% rename from mealapi/resources/mealresource.py rename to mealapi/resources/mealbaseresource.py index e020b8a..3bec3cb 100644 --- a/mealapi/resources/mealresource.py +++ b/mealapi/resources/mealbaseresource.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from flask_restful import Resource -from flask import request, current_app, abort +from flask import request, abort from marshmallow import ValidationError from model import db, Meal diff --git a/mealapi/resources/mealindividualresource.py b/mealapi/resources/mealindividualresource.py new file mode 100644 index 0000000..525aa62 --- /dev/null +++ b/mealapi/resources/mealindividualresource.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +from flask_restful import Resource +from flask import request, abort +from marshmallow import ValidationError + +from model import db, Meal +from schemas import MealSchema + + +class MealIndividualResource(Resource): + + mealschema = MealSchema(many=False) + + def put(self, mealid): + body = request.get_json() + + try: + self.mealschema.load(body, instance=Meal.query.get(mealid).first_or_404()) + db.session.commit() + return '', 200 + except ValidationError as error: + abort(406, error.messages) + + def get(self, mealid): + meals = Meal.query.filter_by(id=mealid).first_or_404() + return self.mealschema.dump(list(meals)), 200 + + def delete(self, mealid): + meal = Meal.query.filter_by(id=mealid).first_or_404() + db.session.delete(meal) + db.session.commit() + return self.mealschema.dump(meal), 200 diff --git a/mealapi/schemas/mealschema.py b/mealapi/schemas/mealschema.py index 41f56b8..1926467 100644 --- a/mealapi/schemas/mealschema.py +++ b/mealapi/schemas/mealschema.py @@ -6,7 +6,7 @@ from .ingredientschema import IngredientSchema class MealSchema(ma.SQLAlchemyAutoSchema): - ingredients = ma.Nested(IngredientSchema, many=True) + ingredients = ma.Nested(IngredientSchema, many=True, required=True) class Meta: model = Meal