Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storeitem #2

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/store/delete_item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from utils import database
def dltItem(ItemID):
db=database.Db
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Db is a class, so needs to have ( ), chek user module.

query=("DELETE FROM ItemRateChart WHERE ItemID=%s")
params=(ItemID)
return db.delete(query,params)


7 changes: 7 additions & 0 deletions app/store/get_item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from utils import database

def findItem(ItemID):
db= database.Db()
query=("SELECT * FROM ItemRateChart WHERE ItemName=%s")
params=(ItemID,)
return db.select(query,params)
7 changes: 7 additions & 0 deletions app/store/post_item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from utils import database

def addItem(ItemID,ItemName,ItemUnit,CurrentRate):
db=database.Db
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

query=("INSERT INTO ItemRateChart (ItemID,ItemName,ItemUnit,CurrentRate) VALUES(%s,%s,%s,%s)")
params=(ItemID,ItemName,ItemUnit,CurrentRate)
return db.insert(query, params)
9 changes: 9 additions & 0 deletions app/store/put_item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from utils import database

def updateInfo(ItemID,ItemName,ItemUnit,CurrentRate):
db= database.Db
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

query=("UPDATE ItemRateChart SET ItemName=%s,ItemUnit=%s,CurrentRate=%s WHERE ItemID=%s")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if i want to update a single column?

params=(ItemName,ItemUnit,CurrentRate)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why params only have 3 value? How to pass the item_id in the where clause?

return db.update(query,params)


37 changes: 37 additions & 0 deletions app/store/store_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from flask_restful import Resource
from utils import response
from store import get_item,post_item,put_item,delete_item
from flask import request
class Item(Resource):
def get(self,ItemID):
key, data = get_item.findItem(ItemID)
if len(data) == 0:
return response.styler(404)
itemdetails = dict(zip(key,data[0]))
return response.styler(200, itemdetails)

def post(self,ItemID):
ItemName= request.form["ItemName"]
ItemUnit= request.form["ItemUnit"]
CurrentRate= request.form["CurrentRate"]
err=post_item.addItem(ItemID,ItemName,ItemUnit,CurrentRate)
if err != True:
return response.styler(204)
return response.styler(400)

def put(self,ItemID):
ItemName=request.form["ItemName"]
ItemUnit=request.form["ItemUnit"]
CurrentRate=request.form["CurrentRate"]
err=put_item.updateInfo(ItemID,ItemName,ItemUnit,CurrentRate)
if err!=True:
return response.styler(204)
return response.styler(400)

def delete(self,ItemID):
err=delete_item.dltItem(ItemID)
if err!= True:
return response.styler(204)
return response.styler(404)


3 changes: 3 additions & 0 deletions routes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from flask import Flask
from flask_restful import Resource, Api
from user import user_routes
from store import store_routes

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

api.add_resource(user_routes.User, '/user/<string:mobile_num>')

api.add_resource(store_routes.Item,'/store/<string:ItemID>')
21 changes: 21 additions & 0 deletions utils/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,25 @@ def insert(self,query,params):
return True
self.mariadb_connection.commit()
return self.cursor.lastrowid

def update(self,query,params):
try:
self.cursor.execute(query,params)
print(self.cursor.rowcount, "record updated")
except mariadb.Error as error:
print("Error: {}".format(error))
return True
self.mariadb_connection.commit()
return self.cursor.lastrowid
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why retrun last row id for update?


def delete(self,query,params):
try:
self.cursor.execute(query,params)
print(self.cursor.rowcount, "row deleted")
except mariadb.Error as error:
print("Error: {}".format(error))
return True
self.mariadb_connection.commit()
return self.cursor.lastrowid
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why return last row ID for delete?