meal_homework/mealapi/resources/mealindividualresource.py

33 lines
920 B
Python
Raw Normal View History

2022-02-13 22:32:40 +01:00
#!/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