Skip to content

Commit

Permalink
Modify get-event api to account for ticket-type table
Browse files Browse the repository at this point in the history
  • Loading branch information
tarun-nil committed Nov 21, 2023
1 parent ce14477 commit 813267c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
46 changes: 28 additions & 18 deletions swift-ticketing/src/swift_ticketing/db/event.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
(ns swift-ticketing.db.event
(:require [honey.sql :as sql]))
(:require [honey.sql :as sql]
[swift-ticketing.db.ticket :as ticket]
[next.jdbc.date-time :as date-time])
(:import [java.time Instant]))

(defn insert-event [uid event_id event-req]
(sql/format {:insert-into :event
Expand All @@ -13,24 +16,31 @@

(defn get-events [venue from to]
(sql/format {:select [:event_id :event_name :event_description :event_date :venue] :from :event
:where [:and
:where [:and
(if (nil? venue) [true] [:= :venue venue])
(if (nil? from) [true] [:>= :event_date [:cast from :date]])
(if (nil? to) [true] [:<= :event_date [:cast to :date]])
]}))
(if (nil? to) [true] [:<= :event_date [:cast to :date]])]}))

(defn get-event [event-id]
(sql/format {:select [:e.event_id
:event_name
:event_description
:event_date
:venue
[[:count :ticket_id] :ticket_count]
[[:min :ticket_name] :ticket_name]
[[:min :ticket_description] :ticket_description]
[[:min :ticket_price] :ticket_price]]
:from [[:event :e]]
:left-join [[:ticket :t] [:= :e.event_id :t.event_id]]
:where [:and [:= :t.booking_id nil] [:= :e.event_id [:cast event-id :uuid] ]]
:group-by [:e.event_id :t.ticket_name]
}))
(let [current-time (Instant/now)
reservation-expired [:and
[:= :ticket.ticket_status [:cast ticket/RESERVED :ticket_status]]
[:> current-time :ticket.reservation_expiration_time]]
tickets-available [:= :ticket.ticket_status [:cast ticket/AVAILABLE :ticket_status]]]
(sql/format {:select [:e.event_id
:event_name
:event_description
:event_date
:venue
:tt.ticket_type
[[:count :ticket_id] :ticket_count]
[[:min :ticket_name] :ticket_name]
[[:min :tt.ticket_type_description] :ticket_description]
[[:min :ticket_price] :ticket_price]]
:from [[:event :e]]
:left-join [[:ticket_type :tt] [:= :e.event_id :tt.event_id]
[:ticket] [:= :ticket.ticket_type_id :tt.ticket_type_id]]
:where [:and
[:or tickets-available reservation-expired]
[:= :e.event_id [:cast event-id :uuid]]]
:group-by [:e.event_id :tt.ticket_type_id]})))
25 changes: 19 additions & 6 deletions swift-ticketing/src/swift_ticketing/db/ticket.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
(ns swift-ticketing.db.ticket
(:require [honey.sql :as sql])
(:require [honey.sql :as sql]
[next.jdbc.date-time :as date-time])
(:import [java.time Instant]))

;; ticket_status
(defonce RESERVED "Reserved")
(defonce AVAILABLE "Available")
(defonce BOOKED "Booked")
Expand All @@ -16,18 +18,29 @@
reservation-timelimit-seconds (:reservation_limit_in_seconds ticket-req)
seat-type (:seat_type ticket-req)]
(sql/format {:insert-into :ticket_type
:columns [:ticket_type_id :ticket_type :ticket_type_description :event_id :reservation_timelimit_seconds :seat_type]
:values [[ticket-type-id ticket-type description [:cast event-id :uuid] reservation-timelimit-seconds [:cast seat-type :seat_type]]]})))
:columns [:ticket_type_id
:ticket_type
:ticket_type_description
:event_id
:reservation_timelimit_seconds
:seat_type]
:values [[ticket-type-id
ticket-type
description
[:cast event-id :uuid]
reservation-timelimit-seconds
[:cast seat-type :seat_type]]]})))

(defn insert-tickets [tickets price]
(defn insert-tickets [ticket-type-id tickets price]
(let [make-ticket (fn [ticket] [[:cast (:ticket-id ticket) :uuid]
(:name ticket)
price
[:cast AVAILABLE :ticket_status]])
[:cast AVAILABLE :ticket_status]
[:cast ticket-type-id :uuid]])

ticket-rows (map make-ticket tickets)]
(sql/format {:insert-into :ticket
:columns [:ticket_id :ticket_name :ticket_price :ticket_status]
:columns [:ticket_id :ticket_name :ticket_price :ticket_status :ticket_type_id]
:values ticket-rows})))

(defn lock-unbooked-tickets [ticket-ids ticket-type ticket-quantity]
Expand Down
2 changes: 1 addition & 1 deletion swift-ticketing/src/swift_ticketing/handlers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
tickets
(map (fn [m] (assoc m :ticket-id (java.util.UUID/randomUUID))) tickets-map)]
(jdbc/execute! db-spec (ticket/insert-ticket-type event-id ticket-type-id ticket-req))
(jdbc/execute! db-spec (ticket/insert-tickets tickets price))
(jdbc/execute! db-spec (ticket/insert-tickets ticket-type-id tickets price))
{:status 201
:headers {"Content-Type" "application/json"}
:body {"tickets" tickets}}))
Expand Down

0 comments on commit 813267c

Please sign in to comment.