Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -510,6 +510,42 @@ def create_demand_listing():
|
|
| 510 |
return jsonify({'success': True, 'message': 'Demand listing created, pending approval.', 'listing_id': listing_id}), 201
|
| 511 |
except Exception as e: return handle_route_errors(e, uid_context=uid)
|
| 512 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 513 |
# NEW: User Update/Remove Own Listing
|
| 514 |
@app.route('/api/listings/<listing_id>', methods=['PUT', 'DELETE'])
|
| 515 |
def user_manage_listing(listing_id):
|
|
|
|
| 510 |
return jsonify({'success': True, 'message': 'Demand listing created, pending approval.', 'listing_id': listing_id}), 201
|
| 511 |
except Exception as e: return handle_route_errors(e, uid_context=uid)
|
| 512 |
|
| 513 |
+
@app.route('/api/listings/my', methods=['GET'])
|
| 514 |
+
def get_my_listings():
|
| 515 |
+
auth_header = request.headers.get('Authorization')
|
| 516 |
+
uid = None
|
| 517 |
+
try:
|
| 518 |
+
uid = verify_token(auth_header)
|
| 519 |
+
if not FIREBASE_INITIALIZED:
|
| 520 |
+
return jsonify({'error': 'Server configuration error: Firebase not ready.'}), 503
|
| 521 |
+
|
| 522 |
+
# Get all listings from Firebase
|
| 523 |
+
listings_ref = db.reference('listings', app=db_app)
|
| 524 |
+
all_listings = listings_ref.get()
|
| 525 |
+
|
| 526 |
+
if not all_listings or not isinstance(all_listings, dict):
|
| 527 |
+
return jsonify({'success': True, 'listings': []}), 200
|
| 528 |
+
|
| 529 |
+
# Filter listings by the authenticated user's UID
|
| 530 |
+
user_listings = []
|
| 531 |
+
for listing_id, listing_data in all_listings.items():
|
| 532 |
+
if isinstance(listing_data, dict) and listing_data.get('lister_id') == uid:
|
| 533 |
+
# Add the listing_id to the listing data for frontend convenience
|
| 534 |
+
listing_data['id'] = listing_id
|
| 535 |
+
user_listings.append(listing_data)
|
| 536 |
+
|
| 537 |
+
# Sort by creation date (most recent first) if available
|
| 538 |
+
user_listings.sort(key=lambda x: x.get('created_at', ''), reverse=True)
|
| 539 |
+
|
| 540 |
+
return jsonify({
|
| 541 |
+
'success': True,
|
| 542 |
+
'listings': user_listings,
|
| 543 |
+
'count': len(user_listings)
|
| 544 |
+
}), 200
|
| 545 |
+
|
| 546 |
+
except Exception as e:
|
| 547 |
+
return handle_route_errors(e, uid_context=uid)
|
| 548 |
+
|
| 549 |
# NEW: User Update/Remove Own Listing
|
| 550 |
@app.route('/api/listings/<listing_id>', methods=['PUT', 'DELETE'])
|
| 551 |
def user_manage_listing(listing_id):
|