-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinventory.py
More file actions
122 lines (95 loc) · 4.43 KB
/
inventory.py
File metadata and controls
122 lines (95 loc) · 4.43 KB
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
from flask import Blueprint, request, jsonify
import mysql.connector
from cache import get_cache
from db import get_shards, get_shard, execute_query
from rate_limiter import limiter
class InventoryRoutes:
def __init__(self):
self.shards = get_shards()
self.cache = get_cache()
def get_inventory(self):
try:
product_id = request.args.get("product_id")
page = int(request.args.get("page", 1))
per_page = int(request.args.get("per_page", 10))
offset = (page - 1) * per_page
cache_key = f"inventory_{product_id}_{page}_{per_page}"
cached_inventory = self.cache.get(cache_key)
if cached_inventory:
return jsonify({"inventory": cached_inventory.decode("utf-8")})
shard = get_shard(int(product_id), self.shards)
query = f"""
SELECT
i.product_id, i.quantity, p.name AS product_name, c.name AS category_name
FROM
inventory i
JOIN
products p ON i.product_id = p.id
JOIN
categories c ON p.category_id = c.id
WHERE
i.product_id = %s
LIMIT %s OFFSET %s
"""
cursor = execute_query(shard, query, (product_id, per_page, offset))
inventory = cursor.fetchall()
if inventory:
self.cache.setex(cache_key, 300, str(inventory)) # Cache for 5 minutes
return jsonify({"inventory": inventory})
return jsonify({"error": "Product not found"}), 404
except mysql.connector.Error as err:
return jsonify({"error": str(err)}), 500
def add_inventory(self):
try:
data = request.json
product_id = data.get("product_id")
quantity = data.get("quantity")
if not product_id or not quantity:
return jsonify({"error": "Invalid data"}), 400
shard = get_shard(int(product_id), self.shards)
query = "INSERT INTO inventory (product_id, quantity) VALUES (%s, %s)"
cursor = execute_query(shard, query, (product_id, quantity))
shard.commit()
return jsonify({"message": "Inventory added successfully"}), 201
except mysql.connector.Error as err:
return jsonify({"error": str(err)}), 500
def update_inventory(self):
try:
data = request.json
product_id = data.get("product_id")
quantity = data.get("quantity")
if not product_id or not quantity:
return jsonify({"error": "Invalid data"}), 400
shard = get_shard(int(product_id), self.shards)
query = "UPDATE inventory SET quantity = %s WHERE product_id = %s"
cursor = execute_query(shard, query, (quantity, product_id))
shard.commit()
# Invalidate cache after update
self.cache.delete(f"inventory_{product_id}")
return jsonify({"message": "Inventory updated successfully"}), 200
except mysql.connector.Error as err:
return jsonify({"error": str(err)}), 500
def delete_inventory(self):
try:
data = request.json
product_id = data.get("product_id")
if not product_id:
return jsonify({"error": "Invalid data"}), 400
shard = get_shard(int(product_id), self.shards)
query = "DELETE FROM inventory WHERE product_id = %s"
cursor = execute_query(shard, query, (product_id,))
shard.commit()
# Invalidate cache after delete
self.cache.delete(f"inventory_{product_id}")
# Confirm deletion
if cursor.rowcount == 0:
return jsonify({"error": "Product not found"}), 404
return jsonify({"message": "Inventory deleted successfully"}), 200
except mysql.connector.Error as err:
return jsonify({"error": str(err)}), 500
inventory_routes = InventoryRoutes()
inventory_bp = Blueprint("inventory", __name__)
inventory_bp.route("/", methods=["GET"])(inventory_routes.get_inventory)
inventory_bp.route("/add", methods=["POST"])(inventory_routes.add_inventory)
inventory_bp.route("/update", methods=["POST"])(inventory_routes.update_inventory)
inventory_bp.route("/delete", methods=["POST"])(inventory_routes.delete_inventory)