Skip to content

Commit

Permalink
feat: added new shop route and minor patches
Browse files Browse the repository at this point in the history
  • Loading branch information
AshminJayson committed Sep 23, 2023
1 parent 9ee6cd3 commit 1d23f89
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 10 deletions.
40 changes: 33 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from fastapi import FastAPI, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from uuid import uuid4
import uvicorn
from mltoolkit import apriori
from store import db
from utils import csv_parser



app = FastAPI()
Expand Down Expand Up @@ -33,7 +36,7 @@ def run_init():


def run_tests():
print(db.get_all_shops().data)
print(db.get_all_shops_of_user().data)
print(db.get_all_users().data)
return

Expand All @@ -50,14 +53,26 @@ async def frequent_patterns():
return frequent_patterns


@app.get('/get_suggested_patterns')
async def frequent_pattterns_by_shop(shop_id):
shop_details = db.get_shop_details(shop_id)
transactions = db.get_all_transactions_of_shop(shop_id)
frequent_patterns = apriori.get_frequent_patterns(transactions)
return {'shop_details': shop_details, 'buy_patterns' : frequent_patterns}

class User(BaseModel):
username: str
password: str

class Shop(BaseModel):
user_id: str
shop_name: str
district: str
state: str

@app.post('/register_user')
async def register_user(user: User):
res = db.add_new_user(user.username, user.password)
res = db.add_new_shop(user.username, user.password)
return {"message": res}


Expand All @@ -70,16 +85,27 @@ async def login_user(user: User):
async def get_all_transactions():
return db.get_all_transactions()

@app.post('/file')
async def upload_file(file: UploadFile):
# file_data = parse_file(file.file)
pass
@app.post('/add_transactions')
async def upload_file(file: UploadFile, shop_id):
file_data = csv_parser.parse_file(file)
db.insert_transactions(shop_id, file_data)
return {"message": file_data}

@app.get('/shops')
async def get_shops(user_id):
user_shops = db.get_all_shops(user_id)
user_shops = db.get_all_shops_of_user(user_id)
return user_shops

@app.get('/shop')
async def get_shop_details(shop_id):
shop_details = db.get_shop_details(shop_id)
return {'shop_details': shop_details}

@app.post('/shop')
async def add_shop(shop: Shop):
res = db.add_new_shop(shop.shop_name, shop.district, shop.state, shop.user_id)
return {'message': res}

if __name__ == '__main__':
run_init()
# run_tests()
Expand Down
5 changes: 4 additions & 1 deletion mltoolkit/apriori.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@


def get_frequent_patterns(transactions):
"""Takes input as the plain transaction object"""
# print(transactions[0])
dataset = []
for transaction in transactions:
item_list = transaction['item_list'][1:-1]
item_list = transaction['item_list']
items = item_list.split(',')
cleaned_items = [item.strip() for item in items]
dataset.append(cleaned_items)

# for row in dataset:
# print(row)
# print(dataset)
# return ''

Expand Down
41 changes: 39 additions & 2 deletions store/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def init_client():
supabase_url=SUPABASE_PUBLIC_URL, supabase_key=SUPABASE_SERVICE_ROLE)


def get_all_shops(user_id):
def get_all_shops_of_user(user_id):
shop_records = supabase.table('shop').select("*").eq('user_id', user_id).execute()
return shop_records.data

Expand All @@ -28,11 +28,21 @@ def get_all_transactions():
transactions = supabase.table('transaction').select("*").execute()
return transactions.data

def get_all_transactions_of_shop(shop_id):
init_client()
transactions = supabase.table('transaction').select("*").eq('shop_id', shop_id).execute()
return transactions.data


def get_all_users():
user_records = supabase.table('user').select("*").execute()
return user_records

def get_shop_details(shop_id):
init_client()
shop_details = supabase.table('shop').select("*").eq('shop_id', shop_id).execute()
res = shop_details.data[0]
return {'shop_id': shop_id, 'shop_name': res['shop_name'], 'district': res['district'], 'state': res['state']}

def user_exists(username):
try:
Expand Down Expand Up @@ -73,6 +83,22 @@ def add_new_user(username: str, password: str):
except postgrest.exceptions.APIError as err:
print(err.message)
return "error adding user"


def add_new_shop(shop_name: str, district: str, state: str, user_id: any):
init_client()
try:
supabase.table('shop').insert({
'shop_name': shop_name,
'district': district.lower(),
'state': state.lower(),
'user_id': user_id
}).execute()

return "shop successfully added"
except postgrest.exceptions.APIError as err:
print(err.message)
return "error adding shop"


def login(username: str, password: str):
Expand All @@ -88,4 +114,15 @@ def login(username: str, password: str):
else:
return {'user_id': userdata[0]['user_id'], 'message': 'login successful'}
except:
return "error login"
return "error login"

def insert_transactions(shop_id, transactions):
init_client()
try:
for transaction in transactions:
transaction['shop_id'] = shop_id
res = supabase.table('transaction').insert(transaction).execute()
return res
except postgrest.exceptions.APIError as err:
print(err)
return "error inserting transaction"
12 changes: 12 additions & 0 deletions utils/csv_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import csv
import codecs


def parse_file(file):
csv_reader = csv.DictReader(codecs.iterdecode(file.file, 'utf-8'))
data = []

for row in csv_reader:
data.append({'item_list': row['item_list'], 'date': row['date']})
file.file.close()
return data

0 comments on commit 1d23f89

Please sign in to comment.