|
@@ -94,7 +94,8 @@ async def get_my_orders(token: str = Depends(auth_utils.oauth2_scheme)):
|
|
|
user_id = payload.get("id")
|
|
user_id = payload.get("id")
|
|
|
query = """
|
|
query = """
|
|
|
SELECT o.*,
|
|
SELECT o.*,
|
|
|
- GROUP_CONCAT(JSON_OBJECT('filename', f.filename, 'file_path', f.file_path, 'quantity', f.quantity, 'preview_path', f.preview_path, 'print_time', f.print_time, 'filament_g', f.filament_g)) as files
|
|
|
|
|
|
|
+ (SELECT count(*) FROM order_messages om WHERE om.order_id = o.id AND om.is_from_admin = TRUE AND om.is_read = FALSE) as unread_count,
|
|
|
|
|
+ GROUP_CONCAT(JSON_OBJECT('file_id', f.id, 'filename', f.filename, 'file_path', f.file_path, 'quantity', f.quantity, 'preview_path', f.preview_path, 'print_time', f.print_time, 'filament_g', f.filament_g)) as files
|
|
|
FROM orders o
|
|
FROM orders o
|
|
|
LEFT JOIN order_files f ON o.id = f.order_id
|
|
LEFT JOIN order_files f ON o.id = f.order_id
|
|
|
WHERE o.user_id = %s
|
|
WHERE o.user_id = %s
|
|
@@ -135,7 +136,8 @@ async def get_admin_orders(token: str = Depends(auth_utils.oauth2_scheme)):
|
|
|
|
|
|
|
|
query = """
|
|
query = """
|
|
|
SELECT o.*,
|
|
SELECT o.*,
|
|
|
- GROUP_CONCAT(JSON_OBJECT('filename', f.filename, 'file_path', f.file_path, 'file_size', f.file_size, 'quantity', f.quantity, 'preview_path', f.preview_path, 'print_time', f.print_time, 'filament_g', f.filament_g)) as files
|
|
|
|
|
|
|
+ (SELECT count(*) FROM order_messages om WHERE om.order_id = o.id AND om.is_from_admin = FALSE AND om.is_read = FALSE) as unread_count,
|
|
|
|
|
+ GROUP_CONCAT(JSON_OBJECT('file_id', f.id, 'filename', f.filename, 'file_path', f.file_path, 'file_size', f.file_size, 'quantity', f.quantity, 'preview_path', f.preview_path, 'print_time', f.print_time, 'filament_g', f.filament_g)) as files
|
|
|
FROM orders o
|
|
FROM orders o
|
|
|
LEFT JOIN order_files f ON o.id = f.order_id
|
|
LEFT JOIN order_files f ON o.id = f.order_id
|
|
|
GROUP BY o.id
|
|
GROUP BY o.id
|
|
@@ -215,7 +217,8 @@ async def admin_attach_file(
|
|
|
raise HTTPException(status_code=403, detail="Admin role required")
|
|
raise HTTPException(status_code=403, detail="Admin role required")
|
|
|
|
|
|
|
|
unique_filename = f"{uuid.uuid4()}{os.path.splitext(file.filename)[1]}"
|
|
unique_filename = f"{uuid.uuid4()}{os.path.splitext(file.filename)[1]}"
|
|
|
- file_path = os.path.join(config.UPLOAD_DIR, unique_filename).replace("\\", "/")
|
|
|
|
|
|
|
+ file_path = os.path.join(config.UPLOAD_DIR, unique_filename)
|
|
|
|
|
+ db_file_path = f"uploads/{unique_filename}"
|
|
|
|
|
|
|
|
sha256_hash = hashlib.sha256()
|
|
sha256_hash = hashlib.sha256()
|
|
|
with open(file_path, "wb") as buffer:
|
|
with open(file_path, "wb") as buffer:
|
|
@@ -224,9 +227,11 @@ async def admin_attach_file(
|
|
|
buffer.write(chunk)
|
|
buffer.write(chunk)
|
|
|
|
|
|
|
|
preview_path = None
|
|
preview_path = None
|
|
|
|
|
+ db_preview_path = None
|
|
|
if file_path.lower().endswith(".stl"):
|
|
if file_path.lower().endswith(".stl"):
|
|
|
preview_filename = f"{uuid.uuid4()}.png"
|
|
preview_filename = f"{uuid.uuid4()}.png"
|
|
|
- preview_path = os.path.join(config.PREVIEW_DIR, preview_filename).replace("\\", "/")
|
|
|
|
|
|
|
+ preview_path = os.path.join(config.PREVIEW_DIR, preview_filename)
|
|
|
|
|
+ db_preview_path = f"uploads/previews/{preview_filename}"
|
|
|
preview_utils.generate_stl_preview(file_path, preview_path)
|
|
preview_utils.generate_stl_preview(file_path, preview_path)
|
|
|
|
|
|
|
|
filament_g = None
|
|
filament_g = None
|
|
@@ -238,6 +243,32 @@ async def admin_attach_file(
|
|
|
print_time = result.get('print_time_str')
|
|
print_time = result.get('print_time_str')
|
|
|
|
|
|
|
|
query = "INSERT INTO order_files (order_id, filename, file_path, file_size, quantity, file_hash, print_time, filament_g, preview_path) VALUES (%s, %s, %s, %s, 1, %s, %s, %s, %s)"
|
|
query = "INSERT INTO order_files (order_id, filename, file_path, file_size, quantity, file_hash, print_time, filament_g, preview_path) VALUES (%s, %s, %s, %s, 1, %s, %s, %s, %s)"
|
|
|
- f_id = db.execute_commit(query, (order_id, file.filename, file_path, file.size, sha256_hash.hexdigest(), print_time, filament_g, preview_path))
|
|
|
|
|
|
|
+ f_id = db.execute_commit(query, (order_id, file.filename, db_file_path, file.size, sha256_hash.hexdigest(), print_time, filament_g, db_preview_path))
|
|
|
|
|
|
|
|
- return {"id": f_id, "filename": file.filename, "preview_path": preview_path, "filament_g": filament_g, "print_time": print_time}
|
|
|
|
|
|
|
+ return {"file_id": f_id, "filename": file.filename, "preview_path": db_preview_path, "filament_g": filament_g, "print_time": print_time}
|
|
|
|
|
+
|
|
|
|
|
+@router.delete("/{order_id}/files/{file_id}")
|
|
|
|
|
+async def admin_delete_file(
|
|
|
|
|
+ order_id: int,
|
|
|
|
|
+ file_id: int,
|
|
|
|
|
+ token: str = Depends(auth_utils.oauth2_scheme)
|
|
|
|
|
+):
|
|
|
|
|
+ payload = auth_utils.decode_token(token)
|
|
|
|
|
+ if not payload or payload.get("role") != 'admin':
|
|
|
|
|
+ raise HTTPException(status_code=403, detail="Admin role required")
|
|
|
|
|
+
|
|
|
|
|
+ file_record = db.execute_query("SELECT file_path, preview_path FROM order_files WHERE id = %s AND order_id = %s", (file_id, order_id))
|
|
|
|
|
+ if not file_record:
|
|
|
|
|
+ raise HTTPException(status_code=404, detail="File not found")
|
|
|
|
|
+
|
|
|
|
|
+ base_dir = config.BASE_DIR
|
|
|
|
|
+ try:
|
|
|
|
|
+ if file_record[0]['file_path']:
|
|
|
|
|
+ os.remove(os.path.join(base_dir, file_record[0]['file_path']))
|
|
|
|
|
+ if file_record[0]['preview_path']:
|
|
|
|
|
+ os.remove(os.path.join(base_dir, file_record[0]['preview_path']))
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ print(f"Error removing file from disk: {e}")
|
|
|
|
|
+
|
|
|
|
|
+ db.execute_commit("DELETE FROM order_files WHERE id = %s AND order_id = %s", (file_id, order_id))
|
|
|
|
|
+ return {"status": "success"}
|