@@ -630,6 +630,166 @@ def logout_confirmation():
630630# –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
631631
632632
633+ @app .route ("/get_category_items" )
634+ def get_category_items ():
635+ category = request .args .get ("category" )
636+ if not category :
637+ return jsonify ({"error" : "Category not specified" }), 400
638+
639+ if category == "Favorites" :
640+ user_id = session .get ("user_id" )
641+ if not user_id :
642+ return jsonify ({"error" : "User not logged in" }), 401
643+
644+ conn = get_user_db_connection ()
645+ cursor = conn .cursor ()
646+ cursor .execute (
647+ "SELECT item_id FROM favorites WHERE user_id = %s" ,
648+ (user_id ,),
649+ )
650+ favorite_items = {
651+ str (row ["item_id" ]) for row in cursor .fetchall ()
652+ }
653+ conn .close ()
654+
655+ response = requests .get (
656+ f"{ SERVER_URL } /items" , timeout = REQUEST_TIMEOUT
657+ )
658+ all_items = response .json ()
659+ items_in_category = {
660+ k : v for k , v in all_items .items () if k in favorite_items
661+ }
662+ else :
663+ db_category = category .upper ()
664+ response = requests .get (
665+ f"{ SERVER_URL } /items" , timeout = REQUEST_TIMEOUT
666+ )
667+ all_items = response .json ()
668+ items_in_category = {
669+ k : v
670+ for k , v in all_items .items ()
671+ if v .get ("category" , "" ).replace (" " , "" )
672+ == db_category .replace (" " , "" )
673+ }
674+
675+ user_id = session .get ("user_id" )
676+ favorite_item_ids = set ()
677+ if user_id :
678+ conn = get_user_db_connection ()
679+ cursor = conn .cursor ()
680+ cursor .execute (
681+ "SELECT item_id FROM favorites WHERE user_id = %s" ,
682+ (user_id ,),
683+ )
684+ favorite_items = cursor .fetchall ()
685+ conn .close ()
686+ favorite_item_ids = {
687+ str (row ["item_id" ]) for row in favorite_items
688+ }
689+
690+ for item_id_str , item in items_in_category .items ():
691+ item ["is_favorite" ] = item_id_str in favorite_item_ids
692+
693+ return jsonify ({"items" : items_in_category })
694+
695+
696+ @app .route ("/get_cart_data" )
697+ def get_cart_data ():
698+ user_id = session .get ("user_id" )
699+ if not user_id :
700+ return (
701+ jsonify ({"success" : False , "error" : "User not logged in" }),
702+ 401 ,
703+ )
704+
705+ cart_response = requests .get (
706+ f"{ SERVER_URL } /cart" ,
707+ json = {"user_id" : user_id },
708+ timeout = REQUEST_TIMEOUT ,
709+ )
710+
711+ if cart_response .status_code != 200 :
712+ return (
713+ jsonify (
714+ {"success" : False , "error" : "Failed to fetch cart data" }
715+ ),
716+ 500 ,
717+ )
718+
719+ items_response = requests .get (
720+ f"{ SERVER_URL } /items" , timeout = REQUEST_TIMEOUT
721+ )
722+ items = items_response .json ()
723+
724+ cart = cart_response .json ()
725+ subtotal = sum (
726+ details .get ("quantity" , 0 )
727+ * items .get (item_id , {}).get ("price" , 0 )
728+ for item_id , details in cart .items ()
729+ if isinstance (details , dict )
730+ )
731+ delivery_fee = round (subtotal * DELIVERY_FEE_PERCENTAGE , 2 )
732+ total = round (subtotal + delivery_fee , 2 )
733+
734+ return jsonify (
735+ {
736+ "success" : True ,
737+ "cart" : cart ,
738+ "items" : items ,
739+ "subtotal" : f"{ subtotal :.2f} " ,
740+ "delivery_fee" : f"{ delivery_fee :.2f} " ,
741+ "total" : f"{ total :.2f} " ,
742+ }
743+ )
744+
745+
746+ @app .route ("/get_cart_count" , methods = ["GET" ])
747+ def get_cart_count ():
748+ user_id = session .get ("user_id" )
749+ if not user_id :
750+ return (
751+ jsonify ({"success" : False , "error" : "User not logged in" }),
752+ 401 ,
753+ )
754+
755+ response = requests .get (
756+ f"{ SERVER_URL } /cart" ,
757+ json = {"user_id" : user_id },
758+ timeout = REQUEST_TIMEOUT ,
759+ )
760+
761+ if response .status_code != 200 :
762+ return (
763+ jsonify (
764+ {"success" : False , "error" : "Failed to fetch cart data" }
765+ ),
766+ 500 ,
767+ )
768+
769+ items_in_cart = len (response .json ())
770+ return jsonify ({"success" : True , "cart_count" : items_in_cart })
771+
772+
773+ @app .route ("/get_cart_status" , methods = ["GET" ])
774+ def get_cart_status ():
775+ user_id = session .get ("user_id" )
776+ if not user_id :
777+ return (
778+ jsonify ({"success" : False , "error" : "User not logged in" }),
779+ 401 ,
780+ )
781+
782+ user = get_user_cart (user_id )
783+ if user is None :
784+ return (
785+ jsonify ({"success" : False , "error" : "User not found" }),
786+ 404 ,
787+ )
788+
789+ cart = json .loads (user ["cart" ]) if user ["cart" ] else {}
790+ return jsonify ({"success" : True , "cart" : cart })
791+
792+
633793@app .route ("/add_to_cart/<item_id>" , methods = ["POST" ])
634794def add_to_cart (item_id ):
635795 response = requests .post (
@@ -766,56 +926,6 @@ def update_cart(item_id, action):
766926 return jsonify ({"success" : True })
767927
768928
769- @app .route ("/get_cart_data" )
770- def get_cart_data ():
771- user_id = session .get ("user_id" )
772- if not user_id :
773- return (
774- jsonify ({"success" : False , "error" : "User not logged in" }),
775- 401 ,
776- )
777-
778- cart_response = requests .get (
779- f"{ SERVER_URL } /cart" ,
780- json = {"user_id" : user_id },
781- timeout = REQUEST_TIMEOUT ,
782- )
783-
784- if cart_response .status_code != 200 :
785- return (
786- jsonify (
787- {"success" : False , "error" : "Failed to fetch cart data" }
788- ),
789- 500 ,
790- )
791-
792- items_response = requests .get (
793- f"{ SERVER_URL } /items" , timeout = REQUEST_TIMEOUT
794- )
795- items = items_response .json ()
796-
797- cart = cart_response .json ()
798- subtotal = sum (
799- details .get ("quantity" , 0 )
800- * items .get (item_id , {}).get ("price" , 0 )
801- for item_id , details in cart .items ()
802- if isinstance (details , dict )
803- )
804- delivery_fee = round (subtotal * DELIVERY_FEE_PERCENTAGE , 2 )
805- total = round (subtotal + delivery_fee , 2 )
806-
807- return jsonify (
808- {
809- "success" : True ,
810- "cart" : cart ,
811- "items" : items ,
812- "subtotal" : f"{ subtotal :.2f} " ,
813- "delivery_fee" : f"{ delivery_fee :.2f} " ,
814- "total" : f"{ total :.2f} " ,
815- }
816- )
817-
818-
819929@app .route ("/order_status/<int:order_id>" )
820930def order_status (order_id ):
821931 conn = get_main_db_connection ()
@@ -903,69 +1013,6 @@ def place_order():
9031013
9041014 return jsonify ({"success" : True }), 200
9051015
906-
907- @app .route ("/get_category_items" )
908- def get_category_items ():
909- category = request .args .get ("category" )
910- if not category :
911- return jsonify ({"error" : "Category not specified" }), 400
912-
913- if category == "Favorites" :
914- user_id = session .get ("user_id" )
915- if not user_id :
916- return jsonify ({"error" : "User not logged in" }), 401
917-
918- conn = get_user_db_connection ()
919- cursor = conn .cursor ()
920- cursor .execute (
921- "SELECT item_id FROM favorites WHERE user_id = %s" ,
922- (user_id ,),
923- )
924- favorite_items = {
925- str (row ["item_id" ]) for row in cursor .fetchall ()
926- }
927- conn .close ()
928-
929- response = requests .get (
930- f"{ SERVER_URL } /items" , timeout = REQUEST_TIMEOUT
931- )
932- all_items = response .json ()
933- items_in_category = {
934- k : v for k , v in all_items .items () if k in favorite_items
935- }
936- else :
937- db_category = category .upper ()
938- response = requests .get (
939- f"{ SERVER_URL } /items" , timeout = REQUEST_TIMEOUT
940- )
941- all_items = response .json ()
942- items_in_category = {
943- k : v
944- for k , v in all_items .items ()
945- if v .get ("category" , "" ).replace (" " , "" )
946- == db_category .replace (" " , "" )
947- }
948-
949- user_id = session .get ("user_id" )
950- favorite_item_ids = set ()
951- if user_id :
952- conn = get_user_db_connection ()
953- cursor = conn .cursor ()
954- cursor .execute (
955- "SELECT item_id FROM favorites WHERE user_id = %s" ,
956- (user_id ,),
957- )
958- favorite_items = cursor .fetchall ()
959- conn .close ()
960- favorite_item_ids = {
961- str (row ["item_id" ]) for row in favorite_items
962- }
963-
964- for item_id_str , item in items_in_category .items ():
965- item ["is_favorite" ] = item_id_str in favorite_item_ids
966-
967- return jsonify ({"items" : items_in_category })
968-
9691016@app .route ("/accept_delivery/<int:delivery_id>" , methods = ["POST" ])
9701017def accept_delivery (delivery_id ):
9711018 user_id = session .get ("user_id" )
@@ -1147,54 +1194,6 @@ def remove_favorite(item_id):
11471194 conn .close ()
11481195
11491196
1150-
1151- @app .route ("/get_cart_count" , methods = ["GET" ])
1152- def get_cart_count ():
1153- user_id = session .get ("user_id" )
1154- if not user_id :
1155- return (
1156- jsonify ({"success" : False , "error" : "User not logged in" }),
1157- 401 ,
1158- )
1159-
1160- response = requests .get (
1161- f"{ SERVER_URL } /cart" ,
1162- json = {"user_id" : user_id },
1163- timeout = REQUEST_TIMEOUT ,
1164- )
1165-
1166- if response .status_code != 200 :
1167- return (
1168- jsonify (
1169- {"success" : False , "error" : "Failed to fetch cart data" }
1170- ),
1171- 500 ,
1172- )
1173-
1174- items_in_cart = len (response .json ())
1175- return jsonify ({"success" : True , "cart_count" : items_in_cart })
1176-
1177-
1178- @app .route ("/get_cart_status" , methods = ["GET" ])
1179- def get_cart_status ():
1180- user_id = session .get ("user_id" )
1181- if not user_id :
1182- return (
1183- jsonify ({"success" : False , "error" : "User not logged in" }),
1184- 401 ,
1185- )
1186-
1187- user = get_user_cart (user_id )
1188- if user is None :
1189- return (
1190- jsonify ({"success" : False , "error" : "User not found" }),
1191- 404 ,
1192- )
1193-
1194- cart = json .loads (user ["cart" ]) if user ["cart" ] else {}
1195- return jsonify ({"success" : True , "cart" : cart })
1196-
1197-
11981197@app .route ("/deliverer_timeline/<int:delivery_id>" )
11991198def deliverer_timeline (delivery_id ):
12001199 current_username = authenticate ()
0 commit comments