meal_homework/mealAPI/resources/mealresource.py

29 lines
730 B
Python
Raw Normal View History

2022-02-13 20:38:02 +01:00
#!/usr/bin/env python3
from flask_restful import Resource
from flask import request, current_app, abort
from marshmallow import ValidationError
from ..model import db, Meal
from ..schemas import MealSchema
class MealBaseResource(Resource):
mealschema = MealSchema(many=False)
mealschemas = MealSchema(many=True)
def post(self):
body = request.get_json()
try:
mealobj = self.mealschema.load(body)
db.session.add(mealobj.data)
db.session.commit()
return '', 204
except ValidationError:
abort(406, "meal validation error")
def get(self):
meals = Meal.query.all()
return self.mealschemas.dump(list(meals)), 200