diff --git a/swift-ticketing/src/swift_ticketing/app.clj b/swift-ticketing/src/swift_ticketing/app.clj index 5155ade..e7a08c8 100644 --- a/swift-ticketing/src/swift_ticketing/app.clj +++ b/swift-ticketing/src/swift_ticketing/app.clj @@ -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 @@ -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] diff --git a/swift-ticketing/src/swift_ticketing/core.clj b/swift-ticketing/src/swift_ticketing/core.clj index 1a952a7..6e53889 100644 --- a/swift-ticketing/src/swift_ticketing/core.clj +++ b/swift-ticketing/src/swift_ticketing/core.clj @@ -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)) @@ -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}) + ))) diff --git a/swift-ticketing/src/swift_ticketing/handlers.clj b/swift-ticketing/src/swift_ticketing/handlers.clj index c1cc356..fc72d9c 100644 --- a/swift-ticketing/src/swift_ticketing/handlers.clj +++ b/swift-ticketing/src/swift_ticketing/handlers.clj @@ -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})] @@ -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}}))