all resources done
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Torma Kristóf 2022-02-13 22:32:40 +01:00
parent 70c473dc76
commit 00cacf77b7
5 changed files with 38 additions and 4 deletions

View File

@ -5,7 +5,7 @@ from healthcheck import HealthCheck
from utils import Config, register_all_error_handlers, health_database_status from utils import Config, register_all_error_handlers, health_database_status
from model import db from model import db
from schemas import ma from schemas import ma
from resources import MealBaseResource from resources import MealBaseResource, MealIndividualResource
app = Flask(__name__) app = Flask(__name__)
app.config.from_object(Config) app.config.from_object(Config)
@ -22,6 +22,7 @@ def init_db():
api.add_resource(MealBaseResource, "/meals") api.add_resource(MealBaseResource, "/meals")
api.add_resource(MealIndividualResource, "/meals/<mealid>")
health.add_check(health_database_status) health.add_check(health_database_status)

View File

@ -1 +1,2 @@
from .mealresource import MealBaseResource from .mealbaseresource import MealBaseResource
from .mealindividualresource import MealIndividualResource

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from flask_restful import Resource from flask_restful import Resource
from flask import request, current_app, abort from flask import request, abort
from marshmallow import ValidationError from marshmallow import ValidationError
from model import db, Meal from model import db, Meal

View File

@ -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

View File

@ -6,7 +6,7 @@ from .ingredientschema import IngredientSchema
class MealSchema(ma.SQLAlchemyAutoSchema): class MealSchema(ma.SQLAlchemyAutoSchema):
ingredients = ma.Nested(IngredientSchema, many=True) ingredients = ma.Nested(IngredientSchema, many=True, required=True)
class Meta: class Meta:
model = Meal model = Meal