diff --git a/app/api/routes.py b/app/api/routes.py index f5665c4c2a6a28043f0941177afedc2188c5c764..5fe5e7b5b58333e89e15d55b20ad83aab3b842b7 100644 --- a/app/api/routes.py +++ b/app/api/routes.py @@ -1,6 +1,6 @@ from flask import render_template, redirect, url_for, Flask, jsonify from app.api import bp -from app.models import User +from app.models import User, Listings from sqlalchemy import text @bp.route('/user_id/<int:id>', methods=['GET']) @@ -42,3 +42,48 @@ def create_user(): except Exception as e: return jsonify({'error': str(e)}), 500 +@bp.route('/listing/create', methods=['GET']) +def create_listing(): + + #Temporary import + from datetime import datetime + + + try: + #Hardcoded for now as when running upgrade on new db no users exist yet, will change at some point + data = { + "depart_location": "New York", + "depart_time": "2024-12-01T08:00:00", + "destination_location": "London", + "destination_time": "2024-12-01T18:00:00", + "fair_cost": 500.00, + "transport_type": "Airplane", + "business_tickets": 10, + "economy_tickets": 50 + } + + # Extract the required fields + depart_location = data['depart_location'] + depart_time = datetime.strptime(data['depart_time'], '%Y-%m-%dT%H:%M:%S') # Ensure date is in correct format + destination_location = data['destination_location'] + destination_time = datetime.strptime(data['destination_time'], '%Y-%m-%dT%H:%M:%S') + fair_cost = data['fair_cost'] + transport_type = data['transport_type'] + business_tickets = data['business_tickets'] + economy_tickets = data['economy_tickets'] + + result = Listings.create_listing(depart_location, depart_time, destination_location, destination_time, fair_cost, transport_type, business_tickets, economy_tickets) + + if result is None: + return jsonify({'error': 'User not found'}), 404 + + user_data = { + 'depart_location': result.depart_location, + 'depart_time': result.depart_time, + 'id': result.id + } + return jsonify(user_data), 200 + + #If something falls over throw nice error for debugging, will change for admin only users to see errors otherwise throw generic 500 + except Exception as e: + return jsonify({'error': str(e)}), 500 \ No newline at end of file diff --git a/app/models/listings.py b/app/models/listings.py index c210adac9786cee9dbbda55effc25864a9ed0882..423a2e30340efad9cccd63bf099bb1e137231353 100644 --- a/app/models/listings.py +++ b/app/models/listings.py @@ -16,4 +16,22 @@ class Listings(db.Model): @classmethod def get_all_listings(cls): - return cls.query.all() \ No newline at end of file + return cls.query.all() + + @classmethod + def create_listing(cls, depart_location, depart_time, destination_location, destination_time, fair_cost, transport_type, business_tickets, economy_tickets): + new_flight = cls(depart_location=depart_location, + depart_time=depart_time, + destination_location=destination_location, + destination_time=destination_time, + fair_cost=fair_cost, + transport_type=transport_type, + business_tickets=business_tickets, + economy_tickets=economy_tickets) + + # Add the new flight to the session and commit + db.session.add(new_flight) + db.session.commit() + return new_flight + #return cls.query.all() + \ No newline at end of file diff --git a/app/templates/bookings/listings.html b/app/templates/bookings/listings.html index 9cc31c53e35dea91f5b4b16d17c8e95cf134feff..47a201fd4ac5bdfdedbdd2ec963d8e0856e5920c 100644 --- a/app/templates/bookings/listings.html +++ b/app/templates/bookings/listings.html @@ -2,7 +2,31 @@ {% block content %} <div class="content"> - <div style="margin-left:50px">Create Listings Logic</div> - <div style="margin-left:50px">{{all_listings}}</div> + <div class="content"> + <p>{{all_listings}}</p> + <h1>All Listings</h1> + <table border="1"> + <thead> + <tr> + {% for column in column_names %} + <th>{{ column.replace('_', ' ').title() }}</th> + {% endfor %} + </tr> + </thead> + <tbody> + {% for listing in all_listings %} + <tr> + {% for column in column_names %} + <td>{{ getattr(listing, column) }}</td> + {% endfor %} + </tr> + {% else %} + <tr> + <td colspan="{{ column_names | length }}">No listings available.</td> + </tr> + {% endfor %} + </tbody> + </table> + </div> </div> {% endblock %} \ No newline at end of file