-
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.
- Loading branch information
Showing
8 changed files
with
186 additions
and
11 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
__pycache__/ | ||
inserts/ | ||
venv/ | ||
data.db | ||
data.db | ||
app.py |
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,53 @@ | ||
# import SQLAlchemy object | ||
from db import db | ||
from constants import constants | ||
from datetime import datetime | ||
|
||
# defines the model for 'products' table in db | ||
class ProductModel(db.Model): | ||
|
||
# name of the table in database | ||
__tablename__ = 'products' | ||
|
||
# define columns in table | ||
id = db.Column(db.Integer, primary_key=True) | ||
recipe_id = db.Column(db.Integer, db.ForeignKey('recipes.id')) | ||
stock_amt = db.Column(db.Integer) | ||
fabrication_date = db.Column(db.String(constants['MEDIUM_LENGTH'])) | ||
|
||
# define relationships with other tables | ||
recipe = db.relationship('RecipeModel') | ||
|
||
def __init__(self, recipe_id): | ||
self.recipe_id = recipe_id | ||
self.fabrication_date = datetime.now().strftime("%d/%m/%Y %H:%M") | ||
|
||
def json(self): | ||
return { | ||
'id' : self.id, | ||
'recipe_id' : self.recipe_id, | ||
'fabrication_date' : self.fabrication_date | ||
} | ||
|
||
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_id(cls, id_): | ||
return cls.query.filter_by(id=id_).first() | ||
|
||
@classmethod | ||
def find_by_recipe_id(cls, id_): | ||
return cls.query.filter_by(recipe_id=id_).first() | ||
|
||
def get_recipe(self): | ||
return [self.recipe.json()] | ||
|
||
@classmethod | ||
def get_stock_amount(cls, id_): | ||
return len(cls.query.filter_by(recipe_id=id_)) |
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
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,97 @@ | ||
# import flask libs | ||
from flask_restful import Resource, reqparse | ||
from constants import constants | ||
from datetime import datetime | ||
|
||
# import model | ||
from models.product import ProductModel | ||
from models.recipe import RecipeModel | ||
|
||
class Product(Resource): | ||
# adds a parser to handle PUT HTTP requests | ||
parser = reqparse.RequestParser() | ||
parser.add_argument('recipe_id',type=int,required=False) | ||
parser.add_argument('stock_amt',type=int,required=False) | ||
|
||
# to handle HTTP GET /product/<int:id> | ||
def get(self, id): | ||
product = ProductModel.find_by_id(id) | ||
if product: | ||
return product.json(), 200 | ||
else: | ||
return {'Message': constants['ID_NOT_FOUND']} | ||
|
||
# to handle HTTP DEL /product/<int:id> | ||
def delete(self, id): | ||
# checks if material exists in database | ||
product = ProductModel.find_by_id(id) | ||
|
||
# in case it exists, delete it | ||
if product: | ||
product.delete_from_db() | ||
|
||
# return message and default HTTP status (200 - OK) | ||
return {'Message': constants['DELETED']} | ||
|
||
# to handle HTTP PUT /product/<int:id> | ||
def put(self, id): | ||
# gets parameter from parser | ||
data = Product.parser.parse_args() | ||
|
||
# checks if material exists in database | ||
product = ProductModel.find_by_id(id) | ||
|
||
# in case it exists, updates it | ||
if product: | ||
for key in data.keys(): | ||
if key=='recipe_id' and data['recipe_id']: | ||
recipe = RecipeModel.find_by_id(data['recipe_id']) | ||
if recipe: | ||
product.recipe_id = data['recipe_id'] | ||
product.recipe = recipe | ||
product.fabrication_date = 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: | ||
product = ProductModel(**data) | ||
|
||
# tries to insert in database, returns 500 (internal server error) in case of database failure | ||
try: | ||
product.save_to_db() | ||
except: | ||
return {"message": "An error occurred with the database."}, 500 | ||
|
||
# returns | ||
return product.json() | ||
|
||
class Products(Resource): | ||
# adds a parser to handle POST HTTP requests | ||
parser = reqparse.RequestParser() | ||
parser.add_argument('recipe_id',type=int,required=True) | ||
|
||
# handles HTTP request GET /products | ||
def get(self): | ||
return [x.json() for x in ProductModel.query.all()] | ||
|
||
# handles HTTP request POST /products | ||
def post(self): | ||
# gets parameter from parser | ||
data = Products.parser.parse_args() | ||
|
||
recipe = RecipeModel.find_by_id(data['recipe_id']) | ||
if recipe: | ||
product = ProductModel(data['recipe_id']) | ||
# in case provided recipe does not exist, throw error message | ||
else: | ||
return {"message": "A recipe with id '{}' does not exist.".format(data['recipe_id'])}, 400 | ||
|
||
# tries to insert in database | ||
# returns 500 (internal server error) in case of database failure | ||
try: | ||
product.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 product.json(), 201 |
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