diff --git a/swift-ticketing/src/swift_ticketing/db/event.clj b/swift-ticketing/src/swift_ticketing/db/event.clj index 4e04079..05bc55b 100644 --- a/swift-ticketing/src/swift_ticketing/db/event.clj +++ b/swift-ticketing/src/swift_ticketing/db/event.clj @@ -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 @@ -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]}))) diff --git a/swift-ticketing/src/swift_ticketing/db/ticket.clj b/swift-ticketing/src/swift_ticketing/db/ticket.clj index a1e691a..3e70593 100644 --- a/swift-ticketing/src/swift_ticketing/db/ticket.clj +++ b/swift-ticketing/src/swift_ticketing/db/ticket.clj @@ -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") @@ -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] diff --git a/swift-ticketing/src/swift_ticketing/handlers.clj b/swift-ticketing/src/swift_ticketing/handlers.clj index 91032c7..7a84169 100644 --- a/swift-ticketing/src/swift_ticketing/handlers.clj +++ b/swift-ticketing/src/swift_ticketing/handlers.clj @@ -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}}))