Skip to content

Commit 0ef4f9c

Browse files
author
Roman Rudakov
authored
Merge pull request #14 from rrudakov/latest-featured-post-list
Latest featured post list
2 parents 417cd7d + 647e783 commit 0ef4f9c

File tree

4 files changed

+169
-105
lines changed

4 files changed

+169
-105
lines changed

src/education/database/articles.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@
105105
(first result)
106106
nil)))
107107

108+
(defn get-last-featured-articles
109+
[conn limit]
110+
(->> (hsql/build article-owerview-sql-map
111+
:where [:= :is_main_featured true]
112+
:order-by [[:updated_on :desc]]
113+
:limit limit
114+
:offset 1)
115+
hsql/format
116+
(sql/query conn)))
117+
108118
(defn delete-article
109119
"Delete article from database by `article-id`."
110120
[conn article-id]

src/education/http/endpoints/articles.clj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@
8484
(not-found {:message not-found-error-message})
8585
(ok (to-short-article-response mf-article)))))
8686

87+
(defn- get-last-featured-articles-handler
88+
"Get last featured articles handler."
89+
[db limit]
90+
(->> (articlesdb/get-last-featured-articles db limit)
91+
(map to-short-article-response)
92+
ok))
93+
8794
(defn- delete-article-handler
8895
"Delete article by `article-id` handler."
8996
[db article-id]
@@ -135,6 +142,11 @@
135142
:return ::specs/article-short
136143
:summary "Get main featured article"
137144
(get-last-main-featured-article-handler db))
145+
(GET "/articles/featured/latest" []
146+
:return ::specs/articles-short
147+
:query-params [limit :- ::specs/limit]
148+
:summary "Get latest featured articles list"
149+
(get-last-featured-articles-handler db limit))
138150
(DELETE "/articles/:id" []
139151
:middleware [[require-roles #{:moderator}]]
140152
:path-params [id :- ::specs/id]

test/education/database/articles_test.clj

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@
115115
(t/testing "Test get all articles default database query"
116116
(with-redefs [sql/query (fn [_ q] q)]
117117
(let [[query limit] (sut/get-all-articles nil)]
118-
(t/is (= "SELECT id, user_id, title, featured_image, updated_on, description FROM articles ORDER BY updated_on DESC LIMIT ?" query))
118+
(t/is (= (str "SELECT id, user_id, title, featured_image, updated_on, description "
119+
"FROM articles ORDER BY updated_on DESC LIMIT ?") query))
119120
(t/is (= 100 limit))))))
120121

121122
(t/deftest get-all-articles-test-query-with-limit
@@ -130,15 +131,18 @@
130131
(with-redefs [sql/query (fn [_ q] q)]
131132
(let [number-param 30
132133
[query number] (sut/get-latest-full-sized-articles nil number-param)]
133-
(t/is (= "SELECT id, user_id, title, body, featured_image, created_on, updated_on, description FROM articles ORDER BY updated_on DESC LIMIT ?" query))
134+
(t/is (= (str "SELECT id, user_id, title, body, featured_image, created_on, updated_on, description "
135+
"FROM articles ORDER BY updated_on DESC LIMIT ?") query))
134136
(t/is (= number-param number ))))))
135137

136138
(t/deftest get-user-articles-test-default-query
137139
(t/testing "Test get user articles default database query"
138140
(with-redefs [sql/query (fn [_ q] q)]
139141
(let [user-id-param 42
140142
[query user-id limit] (sut/get-user-articles nil user-id-param)]
141-
(t/is (= "SELECT id, user_id, title, featured_image, updated_on, description FROM articles WHERE user_id = ? ORDER BY updated_on DESC LIMIT ?" query))
143+
(t/is (= (str "SELECT id, user_id, title, featured_image, updated_on, description "
144+
"FROM articles "
145+
"WHERE user_id = ? ORDER BY updated_on DESC LIMIT ?") query))
142146
(t/is (= limit 100))
143147
(t/is (= user-id user-id-param))))))
144148

@@ -165,7 +169,10 @@
165169
(with-redefs [sql/query (spy/spy)]
166170
(sut/get-last-featured-article nil)
167171
(let [[[_ [query limit]]] (spy/calls sql/query)]
168-
(t/is (= "SELECT id, user_id, title, featured_image, updated_on, description FROM articles WHERE is_main_featured = TRUE ORDER BY updated_on DESC LIMIT ?" query))
172+
(t/is (= (str "SELECT id, user_id, title, featured_image, updated_on, description "
173+
"FROM articles "
174+
"WHERE is_main_featured = TRUE "
175+
"ORDER BY updated_on DESC LIMIT ?") query))
169176
(t/is (= 1 limit))))))
170177

171178
(t/deftest get-last-featured-article-test-no-results
@@ -179,6 +186,25 @@
179186
(with-redefs [sql/query (fn [_ _] res)]
180187
(t/is (= (first res) (sut/get-last-featured-article nil)))))))
181188

189+
(t/deftest get-last-featured-articles-test-query
190+
(t/testing "Get latest featured article list database query"
191+
(with-redefs [sql/query (spy/spy)]
192+
(let [limit-param 3
193+
_ (sut/get-last-featured-articles nil limit-param)
194+
[[_ [query limit offset]]] (spy/calls sql/query)]
195+
(t/is (= limit-param limit))
196+
(t/is (= 1 offset))
197+
(t/is (= (str "SELECT id, user_id, title, featured_image, updated_on, description "
198+
"FROM articles "
199+
"WHERE is_main_featured = TRUE "
200+
"ORDER BY updated_on DESC LIMIT ? OFFSET ?") query))))))
201+
202+
(t/deftest get-last-featured-articles-test-no-results
203+
(t/testing "Get latest featured article list no results from database"
204+
(with-redefs [sql/query (spy/mock (fn [_ _] []))]
205+
(let [res (sut/get-last-featured-articles nil 3)]
206+
(t/is (= [] res))))))
207+
182208
(t/deftest delete-article-test-table-name
183209
(t/testing "Delete article database table name"
184210
(with-redefs [sql/delete! (fn [_ t _] t)]

0 commit comments

Comments
 (0)