Skip to content

Commit

Permalink
Add ticket booking flow to app
Browse files Browse the repository at this point in the history
  • Loading branch information
tarun-nil committed Nov 10, 2023
1 parent b777f68 commit a0dcad3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
19 changes: 14 additions & 5 deletions swift-ticketing/src/swift_ticketing/app.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
(ns swift-ticketing.app
(:require [compojure.core :refer :all]
[compojure.route :as route]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
[ring.middleware.json :refer [wrap-json-response wrap-json-body]]
[swift-ticketing.handlers :as handlers]))
(:require ; [compojure.core :refer :all]
[compojure.route :as route]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
[ring.middleware.json :refer [wrap-json-response wrap-json-body]]
[swift-ticketing.handlers :as handlers]
[compojure.api.sweet :as compojure]
[compojure.api.sweet :refer :all]
[schema.core :as s]
; [swift-ticketing.models :as models]
))

(defn init-routes [db-spec]
(defroutes app-routes
Expand All @@ -14,6 +19,10 @@
(handlers/create-event db-spec (get-in cookies ["uid" :value]) body))
(POST "/event/:event-id/ticket" {:keys [body cookies route-params]}
(handlers/create-tickets db-spec (get-in cookies ["uid" :value]) (:event-id route-params) body))
(POST "/event/:event-id/booking" {:keys [body cookies route-params]}
(handlers/book-ticket db-spec (get-in cookies ["uid" :value]) (:event-id route-params) body))
(GET "/booking-status/:booking-id" {:keys [cookies route-params]}
(handlers/get-booking-status db-spec (get-in cookies "uid" :value) (:booking-id route-params)))
(route/not-found "Not Found")))

(defn swift-ticketing-app [db-spec]
Expand Down
7 changes: 5 additions & 2 deletions swift-ticketing/src/swift_ticketing/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
[next.jdbc :as jdbc]
[next.jdbc.connection :as connection]
[ring.adapter.jetty :refer [run-jetty]]
[swift-ticketing.app :as app])
[swift-ticketing.app :as app]
[swift-ticketing.worker :as worker])
(:import (com.zaxxer.hikari HikariDataSource))
(:gen-class))

Expand All @@ -22,5 +23,7 @@
(with-open [^HikariDataSource ds (create-connection-pool (:database config))]
(.close (jdbc/get-connection ds))
; (jdbc/execute! ds ["select * from swift_ticketing.event"])
(worker/workers ds 5)
(run-jetty (app/swift-ticketing-app ds) {:port (get-in config [:server :port])
:join? true}))))
:join? true})
)))
19 changes: 18 additions & 1 deletion swift-ticketing/src/swift_ticketing/handlers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
[next.jdbc.result-set :as rs]
; [ring.util.response :as response]
[swift-ticketing.db.event :as event]
[swift-ticketing.db.ticket :as ticket]))
[swift-ticketing.db.ticket :as ticket]
[swift-ticketing.db.booking :as booking]
[swift-ticketing.worker :as worker]))

(defn get-event [db-spec venue from to]
(let [events (jdbc/execute! db-spec (event/get-event venue from to) {:builder-fn rs/as-unqualified-maps})]
Expand All @@ -22,3 +24,18 @@
(jdbc/execute! db-spec (ticket/insert-tickets uid event-id ticket-ids ticket-req))
{:status 201
:body {"tickets" ticket-ids}}))

(defn book-ticket [db-spec uid event-id booking-req]
(let [booking-id (java.util.UUID/randomUUID)
quantity (:quantity booking-req)]
(jdbc/execute! db-spec (booking/insert-booking uid booking-id))
(worker/add-ticket-request-to-queue event-id {:booking-id booking-id
:ticket-type (:ticket_name booking-req)
:quantity quantity})
{:status 201
:body {"booking_id" 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
:body {"booking_status" result}}))

0 comments on commit a0dcad3

Please sign in to comment.