Skip to content

Commit

Permalink
Add cancel ticket api
Browse files Browse the repository at this point in the history
  • Loading branch information
tarun-nil committed Nov 24, 2023
1 parent 8a84570 commit 6a70d2e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
2 changes: 2 additions & 0 deletions swift-ticketing/src/swift_ticketing/app.clj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
(handlers/get-booking-status-handler db-spec (get-in cookies "uid" :value) (:booking-id route-params)))
(POST "/booking/:booking-id/payment" {:keys [cookies route-params]}
(handlers/post-payment-handler db-spec (:booking-id route-params)))
(POST "/booking/:booking-id/cancel" {:keys [cookies route-params]}
(handlers/cancel-booking-handler db-spec (:booking-id route-params)))
(route/not-found "Not Found")))

(defn swift-ticketing-app [db-spec]
Expand Down
1 change: 1 addition & 0 deletions swift-ticketing/src/swift_ticketing/db/booking.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
(defonce CONFIRMED "Confirmed")
(defonce PAYMENTPENDING "PaymentPending")
(defonce REJECTED "Rejected")
(defonce CANCELED "Canceled")

(defn insert-booking [uid booking-id]
(sql/format {:insert-into :booking
Expand Down
10 changes: 8 additions & 2 deletions swift-ticketing/src/swift_ticketing/db/ticket.clj
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,18 @@
:reservation_expiration_time reservation-expiration-time}
:where [:in :ticket_id ticket-ids]}))

(defn confirm-tickets [ticket-ids]
(defn reset-ticket-status [ticket-ids status]
(sql/format {:update :ticket
:set {:ticket_status [:cast BOOKED :ticket_status]
:set {:ticket_status [:cast status :ticket_status]
:reservation_expiration_time nil}
:where [:in :ticket_id ticket-ids]}))

(defn confirm-tickets [ticket-ids]
(reset-ticket-status ticket-ids BOOKED))

(defn cancel-tickets [ticket-ids]
(reset-ticket-status ticket-ids AVAILABLE))

(defn update-ticket-booking-id [ticket-ids booking-id]
(sql/format {:update :ticket
:set {:booking_id [:cast booking-id :uuid]}
Expand Down
15 changes: 13 additions & 2 deletions swift-ticketing/src/swift_ticketing/handlers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
[swift-ticketing.worker :as worker]))

(defn validate-req [req spec handler]
(println req)
(if (s/valid? spec req)
(handler)
{:status 400
Expand Down Expand Up @@ -57,7 +58,8 @@
(jdbc/execute! db-spec (ticket/insert-tickets ticket-type-id tickets price))
{:status 201
:headers {"Content-Type" "application/json"}
:body {"tickets" tickets}}))
:body {"ticket_type_id" ticket-type-id
"tickets" tickets}}))

(defn create-tickets-handler [db-spec uid event-id ticket-req]
(and
Expand All @@ -83,7 +85,7 @@
(defn book-ticket-handler [db-spec uid event-id booking-req]
(and
(s/valid? event-id ::specs/event-id)
(validate-req booking-req ::specs/create-booking-params #(book-ticket db-spec uid event-id booking-req))))
(validate-req booking-req ::specs/create-tickets-params #(book-ticket db-spec uid event-id booking-req))))

(defn post-payment [db-spec booking-id]
(worker/add-book-ticket-request-to-queue {:booking-id booking-id})
Expand All @@ -94,6 +96,15 @@
(defn post-payment-handler [db-spec booking-id]
(validate-req booking-id ::specs/booking-id #(post-payment db-spec booking-id)))

(defn cancel-booking [db-spec booking-id]
(worker/add-cancel-ticket-request-to-queue {:booking-id booking-id})
{:status 201
:headers {"Content-Type" "application/json"}
:body {"booking_id" booking-id}})

(defn cancel-booking-handler [db-spec booking-id]
(validate-req booking-id ::specs/booking-id #(cancel-booking db-spec booking-id)))

(defn get-booking-status [db-spec uid booking-id]
(let [result (:booking/booking_status (jdbc/execute-one! db-spec (booking/get-booking-status uid booking-id)))]
{:status 200
Expand Down
16 changes: 16 additions & 0 deletions swift-ticketing/src/swift_ticketing/worker.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

(def reserve-event "RESERVE")
(def book-event "BOOK")
(def cancel-event "CANCEL")

(defn add-ticket-request-to-queue [request]
(async/go (async/>! ticket-queue request)))
Expand All @@ -34,6 +35,12 @@
(add-ticket-request-to-queue {:event event-type
:data {:booking-id booking-id}})))

(defn add-cancel-ticket-request-to-queue [request]
(let [event-type cancel-event
booking-id (:booking-id request)]
(add-ticket-request-to-queue {:event event-type
:data {:booking-id booking-id}})))

(defn handle-reserve-event [db-spec request]
(let [booking-id (get-in request [:data :booking-id])
ticket-ids (get-in request [:data :ticket-ids])
Expand Down Expand Up @@ -65,6 +72,14 @@
(jdbc/execute! tx (ticket/confirm-tickets selected-ticket-ids))
(jdbc/execute! tx (booking/update-booking-status booking-id booking/CONFIRMED)))))

(defn handle-cancel-event [db-spec request]
(jdbc/with-transaction [tx db-spec]
(let [booking-id (get-in request [:data :booking-id])
selected-ticket-ids (->> (jdbc/execute! tx (ticket/lock-reserved-tickets booking-id))
(map #(:ticket/ticket_id %)))]
(jdbc/execute! tx (ticket/cancel-tickets selected-ticket-ids))
(jdbc/execute! tx (booking/update-booking-status booking-id booking/CANCELED)))))

(defn process-ticket-requests [thread-id db-spec]
(async/go
(while true
Expand All @@ -76,6 +91,7 @@
(cond
(= event-type reserve-event) (handle-reserve-event db-spec request)
(= event-type book-event) (handle-book-event db-spec request)
(= event-type cancel-event) (handle-cancel-event db-spec request)
:else (println "Worker: Unknown event")))
(catch Exception e
(println (str "Exception in thread #" thread-id " :" e)))))))
Expand Down

0 comments on commit 6a70d2e

Please sign in to comment.