#!/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): meal = Meal.query.filter_by(id=mealid).first_or_404() return self.mealschema.dump(meal), 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