Skip to content

Commit

Permalink
Merge branch 'main' into feature/add_components_in_test
Browse files Browse the repository at this point in the history
  • Loading branch information
zzat authored Dec 21, 2023
2 parents 51a5af6 + 3484530 commit 5cd54f9
Show file tree
Hide file tree
Showing 11 changed files with 409 additions and 333 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ pom.xml.asc
.clj-kondo/
.lsp/
.DS_Store
.idea
clojure-sandbox/.idea/
clojure-sandbox/clojure-sandbox.iml
55 changes: 26 additions & 29 deletions swift-ticketing/src/swift_ticketing/app.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,33 @@
[compojure.api.sweet :refer :all]
[swift-ticketing.app :as app]))

(defn init-routes [db-spec]
(let [get-uid (fn [cookies]
(get-in cookies ["uid" :value]))]
(defroutes app-routes
(GET "/" [] "Hello World")
(GET "/event" [venue from to]
(handlers/get-events-handler db-spec venue from to))
(POST "/event" {:keys [body cookies]}
(handlers/create-event-handler db-spec (get-uid cookies) body))
(GET "/event/:event-id" {:keys [route-params]}
(handlers/get-event-handler db-spec (:event-id route-params)))
(POST "/event/:event-id/ticket" {:keys [body cookies route-params]}
(handlers/create-tickets-handler db-spec (get-uid cookies) (:event-id route-params) body))
(POST "/event/:event-id/booking" {:keys [body cookies route-params]}
(handlers/reserve-ticket-handler db-spec (get-uid cookies) (:event-id route-params) body))
(GET "/ticket" [ticket_type_id]
(handlers/get-tickets-handler db-spec ticket_type_id))
(GET "/booking/:booking-id/status" {:keys [cookies route-params]}
(handlers/get-booking-status-handler db-spec (get-uid cookies) (: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)))
(GET "/booking/:booking-id/ticket" {:keys [cookies route-params]}
(handlers/get-tickets-by-booking-id-handler db-spec (:booking-id route-params)))
(route/not-found "Not Found"))))
(defn init-routes [db-spec message-queue]
(defroutes app-routes
(GET "/event" request
(handlers/get-events-handler db-spec request))
(POST "/event" request
(handlers/create-event-handler db-spec request))
(GET "/event/:event-id" request
(handlers/get-event-handler db-spec request))
(POST "/event/:event-id/ticket" request
(handlers/create-tickets-handler db-spec request))
(POST "/event/:event-id/booking" request
(handlers/reserve-ticket-handler db-spec message-queue request))
(GET "/ticket" request
(handlers/get-tickets-handler db-spec request))
(GET "/booking/:booking-id/status" request
(handlers/get-booking-status-handler db-spec request))
(POST "/booking/:booking-id/payment" request
(handlers/post-payment-handler db-spec message-queue request))
(POST "/booking/:booking-id/cancel" request
(handlers/cancel-booking-handler db-spec message-queue request))
(GET "/booking/:booking-id/ticket" request
(handlers/get-tickets-by-booking-id-handler db-spec request))
(route/not-found "Not Found")))

(defn swift-ticketing-app [db-spec]
(-> (init-routes db-spec)
(defn swift-ticketing-app [db-spec message-queue]
(-> (init-routes db-spec message-queue)
(wrap-defaults (assoc-in site-defaults [:security :anti-forgery] false))
wrap-json-response
(wrap-json-body {:keywords? true
(wrap-json-body {:keywords? true
:bigdecimals? true})))
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
[ring.adapter.jetty :refer [run-jetty]]
[swift-ticketing.app :as app]))

(defrecord HTTPServer [port join? database]
(defrecord HTTPServer [port join? database worker]
component/Lifecycle

(start [component]
(println ";; Starting API Server")
(let [connection (:connection database)
message-queue (:message-queue worker)
server (run-jetty
(app/swift-ticketing-app connection)
(app/swift-ticketing-app connection message-queue)
{:port port
:join? join?})]
(assoc component :http-server server)))
Expand Down
14 changes: 8 additions & 6 deletions swift-ticketing/src/swift_ticketing/components/worker.clj
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
(ns swift-ticketing.components.worker
(:require [com.stuartsierra.component :as component]
[swift-ticketing.worker :as w]))
[swift-ticketing.worker :as w]
[clojure.core.async :as async]))

(defrecord Worker [total-workers redis-opts database]
component/Lifecycle

(start [component]
(println ";; Spawn workers")
(let [connection (:connection database)]
(let [connection (:connection database)
message-queue (async/chan)]
(dotimes [i total-workers]
(w/process-ticket-requests i connection redis-opts)))
component)
(w/process-ticket-requests i message-queue connection redis-opts))
(assoc component :message-queue message-queue)))

(stop [component]
(println ";; Stop workers")
; (async/close! w/ticket-queue)
component))
(async/close! (:message-queue component))
(dissoc component :message-queue)))

(defn new-worker [total-workers redis-opts]
(map->Worker {:total-workers total-workers
Expand Down
2 changes: 1 addition & 1 deletion swift-ticketing/src/swift_ticketing/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
:app (component/using
(new-http-server (get-in config [:server :port])
(get-in config [:server :join?]))
[:database])
[:database :worker])
:worker (component/using
(new-worker 5 redis-opts)
[:database]))))
Expand Down
39 changes: 24 additions & 15 deletions swift-ticketing/src/swift_ticketing/db/booking.clj
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
(ns swift-ticketing.db.booking
(:require [honey.sql :as sql]))
(:require [honey.sql :as sql]
[swift-ticketing.db.query :refer [run-query! run-query-one!]]))

(defonce INPROCESS "InProcess")
(defonce CONFIRMED "Confirmed")
(defonce PAYMENTPENDING "PaymentPending")
(defonce REJECTED "Rejected")
(defonce CANCELED "Canceled")

(defn insert-booking [uid booking-id]
(sql/format {:insert-into :booking
:columns [:booking_id :user_id :booking_status]
:values [[booking-id
[:cast uid :uuid]
[:cast INPROCESS :booking_status]]]}))
(defn insert-booking [db-spec uid booking-id]
(run-query!
db-spec
(sql/format {:insert-into :booking
:columns [:booking_id :user_id :booking_status]
:values [[booking-id
[:cast uid :uuid]
[:cast INPROCESS :booking_status]]]})))

(defn get-booking-status [uid booking-id]
(sql/format {:select [:booking_status] :from :booking
:where [[:= :booking_id [:cast booking-id :uuid]]
]}))
(defn get-booking [db-spec booking-id]
(run-query-one!
db-spec
(sql/format {:select [:*] :from :booking
:where [[:= :booking_id [:cast booking-id :uuid]]]})))

(defn update-booking-status [booking-id booking-status]
(sql/format {:update :booking
:set {:booking_status [:cast booking-status :booking_status]}
:where [:= :booking_id [:cast booking-id :uuid]]}))
(defn get-booking-status [db-spec booking-id]
(:booking_status (get-booking db-spec booking-id)))

(defn update-booking-status [db-spec booking-id booking-status]
(run-query!
db-spec
(sql/format {:update :booking
:set {:booking_status [:cast booking-status :booking_status]}
:where [:= :booking_id [:cast booking-id :uuid]]})))
124 changes: 58 additions & 66 deletions swift-ticketing/src/swift_ticketing/db/event.clj
Original file line number Diff line number Diff line change
@@ -1,71 +1,63 @@
(ns swift-ticketing.db.event
(:require [honey.sql :as sql]
[next.jdbc :as jdbc]
[next.jdbc.result-set :as rs]
[swift-ticketing.db.query :refer [run-query!]]
[swift-ticketing.db.ticket :as ticket])
(:import [java.time Instant]))

(defn execute-query [query-fn db-spec args]
(jdbc/execute! db-spec (apply query-fn args) {:builder-fn rs/as-unqualified-maps}))

(defn insert-event-query [uid event_id event-req]
(sql/format {:insert-into :event
:columns [:event_id :event_name :event_description :event_date :organizer_id :venue]
:values [[event_id
(:name event-req)
(:description event-req)
[:cast (:date event-req) :date]
[:cast uid :uuid]
(:venue event-req)]]}))

(defn insert-event [db-spec & args]
(execute-query insert-event-query db-spec args))

(defn get-events-query [venue from to]
(sql/format {:select [:event_id :event_name :event_description :event_date :venue] :from :event
: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]])]}))

(defn get-events [db-spec & args]
(execute-query get-events-query db-spec args))

(defn get-event-query [event-id]
(sql/format {:select [:event_id :event_name :event_description :event_date :venue] :from :event
:where [:= :event_id [:cast event-id :uuid]]}))

(defn get-event [db-spec & args]
(execute-query get-event-query db-spec args))

(defn get-event-with-tickets-query [event-id]
(let [current-time (Instant/now)
reservation-expired [:and
[:= :ticket.ticket_status [:cast ticket/RESERVED :ticket_status]]
[:or
[:> current-time :ticket.reservation_expiration_time]
[:= :ticket.reservation-expiration-time nil]]]
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
:tt.ticket_type_id
:tt.seat_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]})))

(defn get-event-with-tickets [db-spec & args]
(execute-query get-event-with-tickets-query db-spec args))

(defn insert-event [db-spec uid event_id event-req]
(run-query!
db-spec
(sql/format {:insert-into :event
:columns [:event_id :event_name :event_description :event_date :organizer_id :venue]
:values [[event_id
(:name event-req)
(:description event-req)
[:cast (:date event-req) :date]
[:cast uid :uuid]
(:venue event-req)]]})))

(defn get-events [db-spec filters]
(let [{:keys [venue from to]} filters]
(run-query!
db-spec
(sql/format {:select [:event_id :event_name :event_description :event_date :venue] :from :event
: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]])]}))))

(defn get-event [db-spec event-id]
(run-query!
db-spec
(sql/format {:select [:event_id :event_name :event_description :event_date :venue] :from :event
:where [:= :event_id [:cast event-id :uuid]]})))

(defn get-event-with-tickets [db-spec event-id]
(run-query!
db-spec
(let [current-time [:cast (.toString (Instant/now)) :timestamptz]
reservation-expired [:and
[:= :ticket.ticket_status [:cast ticket/RESERVED :ticket_status]]
[:or
[:> current-time :ticket.reservation_expiration_time]
[:= :ticket.reservation-expiration-time nil]]]
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
:tt.ticket_type_id
:tt.seat_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]}))))
15 changes: 15 additions & 0 deletions swift-ticketing/src/swift_ticketing/db/query.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(ns swift-ticketing.db.query
(:require [next.jdbc :as jdbc]
[next.jdbc.result-set :as rs]))

(defn- run-query-with
[f db-spec query]
(f db-spec query {:builder-fn rs/as-unqualified-maps}))

(defn run-query!
[db-spec query]
(run-query-with jdbc/execute! db-spec query))

(defn run-query-one!
[db-spec query]
(run-query-with jdbc/execute-one! db-spec query))
Loading

0 comments on commit 5cd54f9

Please sign in to comment.