Skip to content

Commit

Permalink
Added changes for the Recipe model with pg db
Browse files Browse the repository at this point in the history
  • Loading branch information
qtsathish committed Nov 21, 2021
1 parent e1ba920 commit 86b1526
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 38 deletions.
41 changes: 36 additions & 5 deletions InstaCook/app.py
@@ -1,14 +1,45 @@
from flask import Flask
from flask_restful import Api
from resources.recipe import RecipeListResource, RecipeResource, RecipePublishResource
from config import Config
from extensions import db
from flask_migrate import Migrate

app = Flask(__name__)
api = Api(app)

api.add_resource(RecipeListResource, '/recipes')
api.add_resource(RecipeResource, '/recipes/<int:recipe_id>')
api.add_resource(RecipePublishResource, '/recipes/<int:recipe_id>/publish')
def create_app() -> Flask:
"""
Create the Flask Application Object
:return:
"""
app = Flask(__name__)
app.config.from_object(Config)
register_extensions(app)
register_resources(app)
return app


def register_extensions(app):
"""
Perform database initialization and migrate the database to create tables
:param app: flask application object
:return: Nothing
"""
db.init_app(app)
migrate = Migrate(app, db)


def register_resources(app):
"""
This method registers api resources
:param app: flask application object
:return: Nothing
"""
api = Api(app)
api.add_resource(RecipeListResource, '/recipes')
api.add_resource(RecipeResource, '/recipes/<int:recipe_id>')
api.add_resource(RecipePublishResource, '/recipes/<int:recipe_id>/publish')


if __name__ == '__main__':
app = create_app()
app.run(port='5000', host='0.0.0.0', debug=True)
16 changes: 16 additions & 0 deletions InstaCook/config.py
@@ -0,0 +1,16 @@
import os


class Config:
"""
This class represents the db configuration
"""
__username = os.getenv('DB_USER', 'sqlalchemy')
__password = os.getenv('DB_PASSWORD', 'sqlalchemy')
__host = os.getenv('DB_HOST', 'localhost')
__port = os.getenv('DB_PORT', '5432')
__db_name = os.getenv('DB_NAME', 'instacook')

DEBUG = True
SQLALCHEMY_DATABASE_URI = f'postgresql+psycopg2://{__username}:{__password}@{__host}:{__port}/{__db_name}'
SQLALCHEMY_TRACK_MODIFICATIONS = False
3 changes: 3 additions & 0 deletions InstaCook/extensions.py
@@ -0,0 +1,3 @@
from flask_sqlalchemy import SQLAlchemy

db: SQLAlchemy = SQLAlchemy()
49 changes: 16 additions & 33 deletions InstaCook/models/recipe.py
@@ -1,51 +1,34 @@
from extensions import db

recipe_list = []


def get_last_id():
"""
This method will return the recipe id
This method will return the last id
:return: id to be generated
"""
if recipe_list:
last_recipe = recipe_list[-1]
else:
return 1

return last_recipe.id + 1


class Recipe:
class Recipe(db.Model):
"""
This class represents the Recipe Model
"""
__tablename__ = 'recipe'

def __init__(self, name, description, num_of_servings, cook_time, directions):
"""
The initializer for the recipe
:param name: name of the recipe
:param description: description
:param num_of_servings: number of servings
:param cook_time: cook_time in seconds
:param directions: directions to follow
"""
self.id = get_last_id()
self.name = name
self.description = description
self.num_of_servings = num_of_servings
self.cook_time = cook_time
self.directions = directions
self.is_publish = False

@property
def data(self):
"""
property to return the Recipe as dictionary
:return: dictionary representation of current Recipe object
"""
return {
'id': self.id,
'name': self.name,
'description': self.description,
'num_of_servings': self.num_of_servings,
'cook_time': self.cook_time,
'directions': self.directions,
}
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False)
description = db.Column(db.String(256))
num_of_servings = db.Column(db.Integer)
cook_time = db.Column(db.Integer)
directions = db.Column(db.String(1000))
is_publish = db.Column(db.Boolean(), default=False)
created_at = db.Column(db.DateTime(), nullable=False, server_default=db.func.now())
updated_at = db.Column(db.DateTime(), nullable=False, server_default=db.func.now(), onupdate=db.func.now())

0 comments on commit 86b1526

Please sign in to comment.