示例#1
0
    def post(self):
        usr = current_user
        flight_id = api.payload['flight_id']
        airlines = api.payload['airlines']
        departure = api.payload['departure']
        seat_type = api.payload['seat_type']
        seats = int(api.payload['seats'])
        fare = float(api.payload['fare'])

        q1 = """
        SELECT fs.flight_id, fs.seat_no FROM Flight f, FlightSeat fs
        WHERE f.flight_id = fs.flight_id
        AND f.flight_id = {}
        AND f.airlines = '{}'
        AND f.departure = '{}'
        AND fs.seat_type = '{}'
        AND fs.fare = {}
        AND (fs.flight_id, fs.seat_no) NOT IN
        (SELECT flight_id, seat_no FROM FlightBooking) 
        LIMIT {}
        """

        res = Query(query=q1)
        seats = [
            a for a in res.getAll(flight_id, airlines, departure, seat_type,
                                  fare, seats)
        ]

        q2 = []

        now = datetime.now().strftime("%Y-%m-%d")
        for s in seats:
            q2.append(
            "INSERT INTO FlightBooking VALUES ({},{},{},'{}',NULL,{:.2f},FALSE);"\
                .format(usr.user_id,s['flight_id'],s['seat_no'],now,fare))

        tx = Transaction(query=q2)
        try:
            tx.execute()
            q3 = []
            for s in seats:
                q3.append(
                    "SELECT flight_id, seat_no FROM FlightBooking WHERE flight_id={} AND user_id={} AND seat_no={}"\
                        .format(s['flight_id'],usr.user_id,s['seat_no'])
                )
            results = []
            for qry in q3:
                ress = Query(query=qry)
                results.append(ress.getOne())
            print("results")
            print(results)
            return {"bookings": results}, 201
        except Exception as e:
            print(e)
            return {"message": "Booking failed"}, 403
示例#2
0
    def post(self):
        usr = current_user
        hotel_id = api.payload['hotel_id']
        room_no = api.payload['room_no']
        q = Query("SELECT cost FROM Room WHERE hotel_id = {} AND room_no = {}")
        cost = q.getOne(hotel_id, room_no)['cost']
        
        start_date = api.payload['start_date']
        end_date = api.payload['end_date']

        days = datetime.strptime(end_date, "%Y-%m-%d") - datetime.strptime(start_date, "%Y-%m-%d")

        if days.days <= 0:
            return {
                "message": "Invalid input"
            }, 403

        amount = cost * days.days
        no_of_persons = api.payload['no_of_persons']
        booking_id = uuid4()

        query = [
            "SELECT COUNT(*) INTO @cnt FROM HotelBooking\
                WHERE hotel_id = {} AND room_no = {}\
                AND NOT (start_date > '{}' OR end_date < '{}');"\
                    .format(hotel_id, room_no, end_date, start_date),
            
            "INSERT INTO HotelBooking VALUES (\
                IF(@cnt > 0, -1, {}), {}, {}, CURRENT_TIMESTAMP(),\
                '{}', '{}', {}, 'All is well',\
                {}, 0, '{}');"\
                    .format(usr.user_id, hotel_id, room_no, start_date,
                    end_date, no_of_persons, amount, booking_id)
        ]

        tx = Transaction(query=query)
        try:
            tx.execute()
            q = Query("SELECT booking_id FROM HotelBooking WHERE booking_id = '{}';")
            res = q.getAll(booking_id)
            assert len(res) == 1
            return {
                "message": "Booking successful"
            }, 201
        except Exception as e:
            return {
                "message": "Booking failed"
            }, 403
示例#3
0
def get_user_from_email(email):
    q = Query("SELECT * FROM User WHERE email = '{}'", model=User)
    usr = q.getOne(email)
    assert usr is not None

    return usr
示例#4
0
def load_user(user_id):
    q = Query("SELECT * FROM User WHERE user_id = {}", model=User)
    usr = q.getOne(user_id)
    return usr