Skip to content

Commit

Permalink
Merge pull request #8 from nilenso/feature/unit_tests
Browse files Browse the repository at this point in the history
Refactor handlers and unit tests
  • Loading branch information
zzat authored Jan 4, 2024
2 parents 8f13fb0 + f0c1697 commit e83e555
Show file tree
Hide file tree
Showing 22 changed files with 583 additions and 428 deletions.
6 changes: 4 additions & 2 deletions swift-ticketing/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@
[compojure "1.7.0"]
[ring/ring-defaults "0.3.2"]
[com.github.seancorfield/next.jdbc "1.3.894"]
[org.xerial/sqlite-jdbc "3.43.2.0"]
[com.github.seancorfield/honeysql "2.4.1066"]
[com.zaxxer/HikariCP "3.3.1"]
[org.postgresql/postgresql "42.6.0"]
[ring/ring-json "0.5.1"]
[ring/ring-mock "0.4.0"]
[clj-http "3.12.3"]
[org.clojure/core.async "1.6.681"]
[metosin/compojure-api "2.0.0-alpha31"]
[org.clojure/data.json "2.4.0"]
[com.taoensso/carmine "3.3.2"]
[ragtime "0.8.0"]
[com.stuartsierra/component "1.1.0"]]
[com.stuartsierra/component "1.1.0"]
[com.taoensso/timbre "6.3.1"]
[metosin/malli "0.13.0"]]
:main ^:skip-aot swift-ticketing.core
:target-path "target/%s"
:plugins [[lein-ring "0.12.6"]]
Expand Down
30 changes: 23 additions & 7 deletions swift-ticketing/src/swift_ticketing/app.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
[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]
[swift-ticketing.app :as app]))
[compojure.api.sweet :refer [defroutes GET POST]]
[camel-snake-kebab.core :as csk]
[camel-snake-kebab.extras :as cske]))

(defn init-routes [db-spec message-queue]
(defroutes app-routes
Expand All @@ -25,16 +25,32 @@
(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))
(handlers/post-payment-handler message-queue request))
(POST "/booking/:booking-id/cancel" request
(handlers/cancel-booking-handler db-spec message-queue request))
(handlers/cancel-booking-handler 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- wrap-response-kebab [handler]
(fn [request]
(let [response (handler request)]
(update response
:body
(partial cske/transform-keys csk/->snake_case_keyword)))))

(defn- wrap-request-kebab [handler]
(fn [request]
(handler
(update request
:body-params
(partial cske/transform-keys csk/->kebab-case-keyword)))))

(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
:bigdecimals? true})))
:bigdecimals? true})
wrap-request-kebab
wrap-response-kebab
wrap-json-response))
7 changes: 4 additions & 3 deletions swift-ticketing/src/swift_ticketing/components/db.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns swift-ticketing.components.db
(:require [com.stuartsierra.component :as component]
[next.jdbc.connection :as connection])
[next.jdbc.connection :as connection]
[taoensso.timbre :as log])
(:import (com.zaxxer.hikari HikariDataSource)))

(defn create-connection-pool ^HikariDataSource [database-config]
Expand All @@ -10,12 +11,12 @@
component/Lifecycle

(start [component]
(println ";; Init db connection pool")
(log/info "Init db connection pool")
(let [^HikariDataSource ds (create-connection-pool (into {} db-config))]
(assoc component :connection ds)))

(stop [component]
(println ";; Stopping db connection pool")
(log/info "Stopping db connection pool")
(.close (:connection component))
(assoc component :connection nil)))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
(ns swift-ticketing.components.http-server
(:require [com.stuartsierra.component :as component]
[ring.adapter.jetty :refer [run-jetty]]
[swift-ticketing.app :as app]))
[swift-ticketing.app :as app]
[taoensso.timbre :as log]))

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

(start [component]
(println ";; Starting API Server")
(log/info "Starting API Server")
(let [connection (:connection database)
message-queue (:message-queue worker)
server (run-jetty
Expand All @@ -17,7 +18,7 @@
(assoc component :http-server server)))

(stop [component]
(println ";; Stopping API Server")
(log/info "Stopping API Server")
(.stop (:http-server component))
(assoc component :http-server nil)))

Expand Down
18 changes: 12 additions & 6 deletions swift-ticketing/src/swift_ticketing/components/worker.clj
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
(ns swift-ticketing.components.worker
(:require [com.stuartsierra.component :as component]
[swift-ticketing.worker :as w]
[clojure.core.async :as async]))
[clojure.core.async :as async]
[taoensso.timbre :as log]))

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

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

(stop [component]
(println ";; Stop workers")
(log/info "Stop workers")
(dotimes [i total-workers]
(async/put! (:worker-exit-ch component) true))
(async/close! (:message-queue component))
(dissoc component :message-queue)))

Expand Down
2 changes: 0 additions & 2 deletions swift-ticketing/src/swift_ticketing/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@

(defn read-app-config [] (read-config "config.edn"))
(defn read-test-config [] (read-config "config.test.edn"))

(defrecord DBConfig [dbtype dbname username password host port schema])
4 changes: 2 additions & 2 deletions swift-ticketing/src/swift_ticketing/db/booking.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
(defonce REJECTED "Rejected")
(defonce CANCELED "Canceled")

(defn insert-booking [db-spec uid booking-id]
(defn insert-booking [db-spec uid booking-id booking-status]
(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]]]})))
[:cast booking-status :booking_status]]]})))

(defn get-booking [db-spec booking-id]
(run-query-one!
Expand Down
79 changes: 0 additions & 79 deletions swift-ticketing/src/swift_ticketing/db/ddl.clj

This file was deleted.

10 changes: 5 additions & 5 deletions swift-ticketing/src/swift_ticketing/db/event.clj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
(ns swift-ticketing.db.event
(:require [honey.sql :as sql]
[swift-ticketing.db.query :refer [run-query!]]
[swift-ticketing.db.query :refer [run-query! run-query-one!]]
[swift-ticketing.db.ticket :as ticket])
(:import [java.time Instant]))

(defn insert-event [db-spec uid event_id event-req]
(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
:values [[event-id
(:name event-req)
(:description event-req)
[:cast (:date event-req) :date]
Expand All @@ -27,9 +27,9 @@
(if (nil? to) [true] [:<= :event_date [:cast to :date]])]}))))

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

(defn get-event-with-tickets [db-spec event-id]
Expand Down
17 changes: 12 additions & 5 deletions swift-ticketing/src/swift_ticketing/db/ticket.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns swift-ticketing.db.ticket
(:require [honey.sql :as sql]
[swift-ticketing.db.query :refer [run-query!]])
[swift-ticketing.db.query :refer [run-query! run-query-one!]])
(:import [java.time Instant]))

;; ticket_status
Expand All @@ -15,10 +15,10 @@
(defn insert-ticket-type [db-spec event-id ticket-type-id ticket-req]
(run-query!
db-spec
(let [ticket-type (:ticket_type ticket-req)
(let [ticket-type (:ticket-type ticket-req)
description (:description ticket-req)
reservation-timelimit-seconds (:reservation_limit_in_seconds ticket-req)
seat-type (:seat_type ticket-req)]
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
Expand All @@ -33,6 +33,13 @@
reservation-timelimit-seconds
[:cast seat-type :seat_type]]]}))))

(defn get-ticket-type [db-spec ticket-type-id]
(run-query-one!
db-spec
(sql/format {:select [:*]
:from :ticket_type
:where [:= :ticket_type_id [:cast ticket-type-id :uuid]]})))

(defn insert-tickets [db-spec ticket-type-id tickets price]
(run-query!
db-spec
Expand Down Expand Up @@ -99,7 +106,7 @@
(sql/format {:update :ticket
:set {:booking_id [:cast booking-id :uuid]
:ticket_status [:cast RESERVED :ticket_status]
:reservation_expiration_time reservation-expiration-time}
:reservation_expiration_time [:cast (.toString reservation-expiration-time) :timestamptz]}
:where [:in :ticket_id ticket-ids]})))

(defn reset-ticket-status [db-spec ticket-ids status]
Expand Down
Loading

0 comments on commit e83e555

Please sign in to comment.