-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merged with main: new table recipe_materials_amt
- Loading branch information
Showing
5 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
from db import db | ||
from constants import constants # constants dictionary | ||
from datetime import datetime | ||
|
||
# defines the model for 'recipe_material_amt' table in db | ||
class RecipeMaterialAmountModel(db.Model): | ||
|
||
# name of the table in database | ||
__tablename__ = 'recipe_material_amt' | ||
|
||
# define columns in table | ||
id = db.Column(db.Integer, primary_key=True) | ||
recipe_id = db.Column(db.Integer) | ||
material_id = db.Column(db.Integer) | ||
amount = db.Column(db.Float) | ||
creation_date = db.Column(db.String(constants['MEDIUM_LENGTH'])) | ||
last_update = db.Column(db.String(constants['MEDIUM_LENGTH'])) | ||
|
||
def __init__(self, recipe_id, material_id, amount): | ||
self.recipe_id = recipe_id | ||
self.material_id = material_id | ||
self.amount = amount | ||
self.creation_date = datetime.now().strftime("%d/%m/%Y %H:%M") | ||
self.last_update = self.creation_date | ||
|
||
def json(self): | ||
return {'id' : self.id, | ||
'recipe_id' : self.recipe_id, | ||
'material_id' : self.material_id, | ||
'amount' : self.amount, | ||
'creation_date': self.creation_date, | ||
'last_update' : self.last_update, | ||
} | ||
|
||
def save_to_db(self): | ||
db.session.add(self) | ||
db.session.commit() | ||
|
||
def delete_from_db(self): | ||
db.session.delete(self) | ||
db.session.commit() | ||
|
||
@classmethod | ||
def find_by_map(cls, recipe_id, material_id): | ||
return cls.query.filter_by(recipe_id=recipe_id).filter_by(material_id=material_id).first() | ||
|
||
@classmethod | ||
def find_by_recipe_id(cls, id): | ||
return cls.query.filter_by(recipe_id=id).first() | ||
|
||
@classmethod | ||
def find_by_material_id(cls, id): | ||
return cls.query.filter_by(material_id=id).first() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# import flask libs | ||
from flask_restful import Resource, reqparse | ||
from constants import constants | ||
from datetime import datetime | ||
#from flask_jwt import jwt_required | ||
|
||
# import model | ||
from models.recipe_material_amt import RecipeMaterialAmountModel | ||
|
||
class RecipeMaterialAmount(Resource): | ||
|
||
# adds a parser to handle PUT HTTP requests | ||
parser = reqparse.RequestParser() | ||
parser.add_argument('amount',type=float,required=True) | ||
|
||
# handles HTTP PUT /recipe/<int:recipe_id>/material/<int:material_id> | ||
def put(self, recipe_id, material_id): | ||
# gets parameter from parser | ||
data = RecipeMaterialAmount.parser.parse_args() | ||
|
||
# checks if material exists in database | ||
recipe_material = RecipeMaterialAmountModel.find_by_map(recipe_id, material_id) | ||
|
||
# in case it exists, updates it | ||
if recipe_material: | ||
for key in data.keys(): | ||
if key=='amount': | ||
recipe_material.amount = data['amount'] | ||
|
||
recipe_material.last_update = datetime.now().strftime("%d/%m/%Y %H:%M") | ||
|
||
# in case it does not exist, creates a new material using data passed | ||
# along with the HTTP request | ||
else: | ||
recipe_material = RecipeMaterialAmountModel(recipe_id, material_id, data['amount']) | ||
|
||
# tries to insert in database | ||
# returns 500 (internal server error) in case of database failure | ||
try: | ||
recipe_material.save_to_db() | ||
except: | ||
return {"message": "An error occurred with the database."}, 500 | ||
|
||
# returns | ||
return recipe_material.json() | ||
|
||
# handles HTTP POST /recipe/<int:recipe_id>/material/<int:material_id> | ||
def post(self, recipe_id, material_id): | ||
# gets parameter from parser | ||
data = RecipeMaterialAmount.parser.parse_args() | ||
|
||
# checks if material exists in database | ||
recipe_material = RecipeMaterialAmountModel.find_by_map(recipe_id, material_id) | ||
|
||
# in case it exists, returns a message and HTTP 400 code (BAD REQUEST) | ||
if recipe_material: | ||
return {'message': "The related recipe-material map already exists."}, 400 | ||
|
||
# in case it does not exist, creates a new material using data passed | ||
# along with the HTTP request | ||
recipe_material = RecipeMaterialAmountModel(recipe_id, material_id, data['amount']) | ||
|
||
# tries to insert in database | ||
# returns 500 (internal server error) in case of database failure | ||
try: | ||
recipe_material.save_to_db() | ||
except: | ||
return {"message": "An error occurred upon inserting the into the database."}, 500 | ||
|
||
# returns JSON with the created Material and returns CREATED status (201) | ||
return recipe_material.json(), 201 |