Skip to content

Commit

Permalink
chore: add ticket validation and insertion to cloudsql postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuan325 committed Jul 16, 2024
1 parent cde05e2 commit 2a9ff7e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 7 deletions.
9 changes: 5 additions & 4 deletions retrieval_service/datastore/providers/alloydb.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,11 +565,12 @@ async def insert_ticket(
"flight_number": flight_number,
"departure_airport": departure_airport,
"arrival_airport": arrival_airport,
"departure_time": departure_time,
"arrival_time": arrival_time,
"departure_time": departure_time_datetime,
"arrival_time": arrival_time_datetime,
}
results = (await conn.execute(s, params)).mappings().fetchall()
if results != "INSERT 0 1":
result = (await conn.execute(s, params)).mappings()
await conn.commit()
if not result:
raise Exception("Ticket Insertion failure")

async def list_tickets(
Expand Down
84 changes: 81 additions & 3 deletions retrieval_service/datastore/providers/cloudsql_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,29 @@ async def validate_ticket(
departure_airport: str,
departure_time: str,
) -> Optional[models.Flight]:
raise NotImplementedError("Not Implemented")
departure_time_datetime = datetime.strptime(departure_time, "%Y-%m-%d %H:%M:%S")
async with self.__pool.connect() as conn:
s = text(
"""
SELECT * FROM flights
WHERE airline ILIKE :airline
AND flight_number ILIKE :flight_number
AND departure_airport ILIKE :departure_airport
AND departure_time = :departure_time
"""
)
params = {
"airline": airline,
"flight_number": flight_number,
"departure_airport": departure_airport,
"departure_time": departure_time_datetime,
}
result = (await conn.execute(s, params)).mappings().fetchone()

if result is None:
return None
res = models.Flight.model_validate(result)
return res

async def insert_ticket(
self,
Expand All @@ -506,13 +528,69 @@ async def insert_ticket(
departure_time: str,
arrival_time: str,
):
raise NotImplementedError("Not Implemented")
departure_time_datetime = datetime.strptime(departure_time, "%Y-%m-%d %H:%M:%S")
arrival_time_datetime = datetime.strptime(arrival_time, "%Y-%m-%d %H:%M:%S")

async with self.__pool.connect() as conn:
s = text(
"""
INSERT INTO tickets (
user_id,
user_name,
user_email,
airline,
flight_number,
departure_airport,
arrival_airport,
departure_time,
arrival_time
) VALUES (
:user_id,
:user_name,
:user_email,
:airline,
:flight_number,
:departure_airport,
:arrival_airport,
:departure_time,
:arrival_time
);
"""
)
params = {
"user_id": user_id,
"user_name": user_name,
"user_email": user_email,
"airline": airline,
"flight_number": flight_number,
"departure_airport": departure_airport,
"arrival_airport": arrival_airport,
"departure_time": departure_time_datetime,
"arrival_time": arrival_time_datetime,
}
result = (await conn.execute(s, params)).mappings()
await conn.commit()
if not result:
raise Exception("Ticket Insertion failure")

async def list_tickets(
self,
user_id: str,
) -> list[models.Ticket]:
raise NotImplementedError("Not Implemented")
async with self.__pool.connect() as conn:
s = text(
"""
SELECT * FROM tickets
WHERE user_id = :user_id
"""
)
params = {
"user_id": user_id,
}
results = (await conn.execute(s, params)).mappings().fetchall()

res = [models.Ticket.model_validate(r) for r in results]
return res

async def policies_search(
self, query_embedding: list[float], similarity_threshold: float, top_k: int
Expand Down

0 comments on commit 2a9ff7e

Please sign in to comment.