This commit is contained in:
parent
70c473dc76
commit
00cacf77b7
@ -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/<mealid>")
|
||||
|
||||
health.add_check(health_database_status)
|
||||
|
||||
|
@ -1 +1,2 @@
|
||||
from .mealresource import MealBaseResource
|
||||
from .mealbaseresource import MealBaseResource
|
||||
from .mealindividualresource import MealIndividualResource
|
||||
|
@ -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
|
32
mealapi/resources/mealindividualresource.py
Normal file
32
mealapi/resources/mealindividualresource.py
Normal 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
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user