|
@@ -41,11 +41,12 @@
|
|
|
(database mysql-db))
|
|
|
|
|
|
|
|
|
-(defn random-world []
|
|
|
+(defn random-world
|
|
|
"Query a random World record from the database"
|
|
|
+ []
|
|
|
(let [id (inc (rand-int 9999))] ; Num between 1 and 10,000
|
|
|
(select world
|
|
|
- (where {:id id }))))
|
|
|
+ (where {:id id }))))
|
|
|
|
|
|
|
|
|
(defn run-queries
|
|
@@ -62,26 +63,27 @@
|
|
|
|
|
|
(defn sanitizeQueriesParam
|
|
|
"Sanitizes the `queries` parameter. Caps the value between 1 and 500.
|
|
|
-Invalid (stringy) values become 1"
|
|
|
+ Invalid (stringy) values become 1"
|
|
|
[request]
|
|
|
- (let [queries (-> request :params :queries)]
|
|
|
- (let [n
|
|
|
- (if (= (re-find #"\A-?\d+" queries) nil)
|
|
|
- 1
|
|
|
- (Integer/parseInt queries))]
|
|
|
- (cond
|
|
|
- (< n 1) 1
|
|
|
- (> n 500) 500
|
|
|
- :else n))))
|
|
|
+ (let [queries (-> request
|
|
|
+ :params
|
|
|
+ :queries)]
|
|
|
+ (if-let [n (if (= (re-find #"\A-?\d+" queries) nil)
|
|
|
+ 1
|
|
|
+ (Integer/parseInt queries))]
|
|
|
+ (cond
|
|
|
+ (< n 1) 1
|
|
|
+ (> n 500) 500
|
|
|
+ :else n))))
|
|
|
|
|
|
|
|
|
(defn multiple-query-test
|
|
|
"Test 3: Multiple database queries"
|
|
|
[request]
|
|
|
(-> request
|
|
|
- (sanitizeQueriesParam)
|
|
|
- (run-queries)
|
|
|
- (bootstrap/json-response)))
|
|
|
+ (sanitizeQueriesParam)
|
|
|
+ (run-queries)
|
|
|
+ (bootstrap/json-response)))
|
|
|
|
|
|
|
|
|
; Set up entity Fortune and the database representation
|
|
@@ -92,23 +94,26 @@ Invalid (stringy) values become 1"
|
|
|
(database mysql-db))
|
|
|
|
|
|
|
|
|
-(defn get-all-fortunes []
|
|
|
+(defn get-all-fortunes
|
|
|
"Query all Fortune records from the database."
|
|
|
+ []
|
|
|
(select fortune
|
|
|
- (fields :id :message)))
|
|
|
+ (fields :id :message)))
|
|
|
|
|
|
|
|
|
-(defn get-fortunes []
|
|
|
+(defn get-fortunes
|
|
|
"Fetch the full list of Fortunes from the database, sort them by the fortune
|
|
|
-message text, and then return the results."
|
|
|
+ message text, and then return the results."
|
|
|
+ []
|
|
|
(sort-by #(:message %)
|
|
|
(conj
|
|
|
(get-all-fortunes)
|
|
|
{ :id 0 :message "Additional fortune added at request time." })))
|
|
|
|
|
|
|
|
|
-(defn fortunes-hiccup [fortunes]
|
|
|
+(defn fortunes-hiccup
|
|
|
"Render the given fortunes to simple HTML using Hiccup."
|
|
|
+ [fortunes]
|
|
|
(html5
|
|
|
[:head
|
|
|
[:title "Fortunes"]]
|
|
@@ -124,8 +129,9 @@ message text, and then return the results."
|
|
|
]]))
|
|
|
|
|
|
|
|
|
-(defn fortune-test [request]
|
|
|
+(defn fortune-test
|
|
|
"Test 4: Fortunes"
|
|
|
+ [request]
|
|
|
(->
|
|
|
(get-fortunes)
|
|
|
(fortunes-hiccup)
|
|
@@ -136,23 +142,25 @@ message text, and then return the results."
|
|
|
|
|
|
(defn update-and-persist
|
|
|
"Changes the :randomNumber of a number of world entities.
|
|
|
-Persists the changes to sql then returns the updated entities"
|
|
|
+ Persists the changes to sql then returns the updated entities"
|
|
|
[request]
|
|
|
- (let [results (-> request (sanitizeQueriesParam) (run-queries))]
|
|
|
+ (let [results (-> request
|
|
|
+ (sanitizeQueriesParam)
|
|
|
+ (run-queries))]
|
|
|
(for [w results]
|
|
|
(update-in w [:randomNumber (inc (rand-int 9999))]
|
|
|
(update world
|
|
|
- (set-fields {:randomNumber (:randomNumber w)})
|
|
|
- (where {:id [:id w]}))))
|
|
|
- results))
|
|
|
+ (set-fields {:randomNumber (:randomNumber w)})
|
|
|
+ (where {:id [:id w]}))))
|
|
|
+ results))
|
|
|
|
|
|
|
|
|
(defn db-updates
|
|
|
"Test 5: Database updates"
|
|
|
[request]
|
|
|
(-> request
|
|
|
- (update-and-persist)
|
|
|
- (bootstrap/json-response)))
|
|
|
+ (update-and-persist)
|
|
|
+ (bootstrap/json-response)))
|
|
|
|
|
|
|
|
|
(defn plaintext
|