-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
278 lines (244 loc) · 7.61 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
from fastapi import FastAPI, HTTPException
import sqlite3
from typing import List
from pydantic import BaseModel, Field
import random
class Book(BaseModel):
title: str
author: str
genre: str
price: float = Field(..., ge=0, description="Price must be non-negative")
stock: int = Field(..., ge=0, description="Stock must be non-negative")
# Initialize the FastAPI app
app = FastAPI()
# Connect to the SQLite database
DATABASE = "bookstore.db"
def get_db_connection():
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
return conn
# Models for request validation and responses
class Book(BaseModel):
book_id: int
title: str
author: str
genre: str
price: float
stock: int
class Customer(BaseModel):
customer_id: int
name: str
email: str
phone: str
class Sale(BaseModel):
book_id: int
customer_id: int
sale_date: str # Use 'YYYY-MM-DD' format
quantity: int
total_amount: float
# Routes for Books
@app.get("/books", response_model=List[Book])
def get_books(limit: int = 10, offset: int = 0):
conn = get_db_connection()
books = conn.execute(
"SELECT * FROM Books LIMIT ? OFFSET ?", (limit, offset)
).fetchall()
conn.close()
return [
{
"book_id": book["BookID"],
"title": book["Title"],
"author": book["Author"],
"genre": book["Genre"],
"price": book["Price"],
"stock": book["Stock"],
}
for book in books
]
@app.post("/books")
def add_book(book: Book):
conn = get_db_connection()
conn.execute(
"INSERT INTO Books (Title, Author, Genre, Price, Stock) VALUES (?, ?, ?, ?, ?)",
(book.title, book.author, book.genre, book.price, book.stock),
)
conn.commit()
conn.close()
return {"message": "Book added successfully"}
@app.put("/books/{book_id}")
def update_book(book_id: int, book: Book):
conn = get_db_connection()
conn.execute(
"UPDATE Books SET Title = ?, Author = ?, Genre = ?, Price = ?, Stock = ? WHERE BookID = ?",
(book.title, book.author, book.genre, book.price, book.stock, book_id),
)
conn.commit()
conn.close()
return {"message": "Book updated successfully"}
@app.delete("/books/{book_id}")
def delete_book(book_id: int):
conn = get_db_connection()
conn.execute("DELETE FROM Books WHERE BookID = ?", (book_id,))
conn.commit()
conn.close()
return {"message": "Book deleted successfully"}
# Routes for Customers
@app.get("/customers", response_model=List[Customer])
def get_customers(limit: int = 10, offset: int = 0):
conn = get_db_connection()
customers = conn.execute(
"SELECT * FROM Customers LIMIT ? OFFSET ?", (limit, offset)
).fetchall()
conn.close()
return [
{
"customer_id": customer["CustomerID"],
"name": customer["Name"],
"email": customer["Email"],
"phone": customer["Phone"],
}
for customer in customers
]
@app.post("/customers")
def add_customer(customer: Customer):
conn = get_db_connection()
conn.execute(
"INSERT INTO Customers (Name, Email, Phone) VALUES (?, ?, ?)",
(customer.name, customer.email, customer.phone),
)
conn.commit()
conn.close()
return {"message": "Customer added successfully"}
@app.put("/customers/{customer_id}")
def update_customer(customer_id: int, customer: Customer):
conn = get_db_connection()
conn.execute(
"UPDATE Customers SET Name = ?, Email = ?, Phone = ? WHERE CustomerID = ?",
(customer.name, customer.email, customer.phone, customer_id),
)
conn.commit()
conn.close()
return {"message": "Customer updated successfully"}
@app.delete("/customers/{customer_id}")
def delete_customer(customer_id: int):
conn = get_db_connection()
conn.execute("DELETE FROM Customers WHERE CustomerID = ?", (customer_id,))
conn.commit()
conn.close()
return {"message": "Customer deleted successfully"}
# Routes for Sales
@app.post("/sales")
def add_sale(sale: Sale):
conn = get_db_connection()
# Check if the book exists and has enough stock
book = conn.execute(
"SELECT Stock FROM Books WHERE BookID = ?", (sale.book_id,)
).fetchone()
if not book:
conn.close()
raise HTTPException(status_code=404, detail="Book not found.")
if book["Stock"] < sale.quantity:
conn.close()
raise HTTPException(status_code=400, detail="Not enough stock available.")
# Record the sale
conn.execute(
"INSERT INTO Sales (BookID, CustomerID, SaleDate, Quantity, TotalAmount) VALUES (?, ?, ?, ?, ?)",
(sale.book_id, sale.customer_id, sale.sale_date, sale.quantity, sale.total_amount),
)
# Update the stock
conn.execute(
"UPDATE Books SET Stock = Stock - ? WHERE BookID = ?",
(sale.quantity, sale.book_id),
)
conn.commit()
conn.close()
return {"message": "Sale recorded successfully, and stock updated."}
@app.get("/sales/book/{book_id}")
def get_total_sales_by_book(book_id: int):
"""
Get the total sales revenue and quantity sold for a specific book.
"""
conn = get_db_connection()
sales = conn.execute
(
"""
SELECT SUM(S.Quantity) AS TotalQuantity, SUM(S.TotalAmount) AS TotalRevenue
FROM Sales S
WHERE S.BookID = ?
""",
(book_id,),
).fetchone()
conn.close()
if not sales or sales["TotalQuantity"] is None:
raise HTTPException(status_code=404, detail="No sales found for this book.")
return
{
"book_id": book_id,
"total_quantity_sold": sales["TotalQuantity"],
"total_revenue": sales["TotalRevenue"],
}
@app.get("/sales/top-books")
def get_top_selling_books(limit: int = 5):
"""
Get the top-selling books based on total quantity sold.
"""
conn = get_db_connection()
books = conn.execute(
"""
SELECT B.BookID, B.Title, B.Author, B.Genre, SUM(S.Quantity) AS TotalSold
FROM Sales S
JOIN Books B ON S.BookID = B.BookID
GROUP BY B.BookID
ORDER BY TotalSold DESC
LIMIT ?
""",
(limit,),
).fetchall()
conn.close()
if not books:
raise HTTPException(status_code=404, detail="No sales data available.")
return [dict(book) for book in books]
@app.get("/customers/{customer_id}/total-spent")
def get_customer_total_spent(customer_id: int):
"""
Get the total amount a customer has spent on purchases.
"""
conn = get_db_connection()
total_spent = conn.execute(
"""
SELECT SUM(S.TotalAmount) AS TotalSpent
FROM Sales S
WHERE S.CustomerID = ?
""",
(customer_id,),
).fetchone()
conn.close()
if not total_spent or total_spent["TotalSpent"] is None:
raise HTTPException(status_code=404, detail="No purchases found for this customer.")
return {
"customer_id": customer_id,
"total_spent": total_spent["TotalSpent"],
}
@app.get("/sales/popular-genre")
def get_most_popular_genre():
"""
Get the most popular book genre based on total sales.
"""
conn = get_db_connection()
genre = conn.execute(
"""
SELECT B.Genre, SUM(S.Quantity) AS TotalSold
FROM Sales S
JOIN Books B ON S.BookID = B.BookID
GROUP BY B.Genre
ORDER BY TotalSold DESC
LIMIT 1
"""
).fetchone()
conn.close()
if not genre:
raise HTTPException(status_code=404, detail="No sales data available.")
return {
"genre": genre["Genre"],
"total_sold": genre["TotalSold"],
}