Skip to content

Commit 880305c

Browse files
committed
article_hots
1 parent 53626f2 commit 880305c

File tree

7 files changed

+68
-57
lines changed

7 files changed

+68
-57
lines changed

src/main/kotlin/com/wafflestudio/team2server/article/ArticleException.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class ArticleBlankTitleException :
5252
httpStatusCode = HttpStatus.BAD_REQUEST,
5353
msg = "Title must not be blank",
5454
)
55+
5556
class StandardNotFoundException :
5657
ArticleException(
5758
errorCode = 0,

src/main/kotlin/com/wafflestudio/team2server/article/controller/ArticleController.kt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import com.wafflestudio.team2server.article.dto.response.ArticlePagingResponse
77
import com.wafflestudio.team2server.article.dto.response.CreateArticleResponse
88
import com.wafflestudio.team2server.article.service.ArticleService
99
import com.wafflestudio.team2server.hotstandard.dto.core.HotStandardDto
10-
import com.wafflestudio.team2server.hotstandard.model.HotStandard
1110
import io.swagger.v3.oas.annotations.Operation
1211
import io.swagger.v3.oas.annotations.Parameter
1312
import io.swagger.v3.oas.annotations.responses.ApiResponse
@@ -37,7 +36,7 @@ class ArticleController(
3736
value = [
3837
ApiResponse(responseCode = "200", description = "핫 게시판 조회 성공"),
3938
ApiResponse(responseCode = "404", description = "기준이 정해지지 않았습니다"),
40-
]
39+
],
4140
)
4241
@GetMapping("/hots")
4342
fun hots(
@@ -55,7 +54,7 @@ class ArticleController(
5554
description = "페이지당 게시글 수",
5655
example = "20",
5756
) @RequestParam(value = "limit", defaultValue = "20") limit: Int,
58-
): ResponseEntity<ArticlePagingResponse>{
57+
): ResponseEntity<ArticlePagingResponse> {
5958
val articlePagingResponse =
6059
articleService.pageByHots(
6160
keyword = keyword,
@@ -65,26 +64,30 @@ class ArticleController(
6564
)
6665
return ResponseEntity.ok(articlePagingResponse)
6766
}
67+
6868
@Operation(summary = "기준 바꾸기", description = "기준 바꿔주기")
6969
@ApiResponses(
70-
ApiResponse(responseCode = "200", description = "기준 바꾸기 성공")
70+
ApiResponse(responseCode = "200", description = "기준 바꾸기 성공"),
7171
)
7272
@PatchMapping("/hots")
7373
fun hotsUpdate(
7474
@Parameter(
7575
description = "총 점수 기준",
76-
example="10",
76+
example = "10",
7777
)@RequestParam(value = "hotScore", required = false) hotScore: Long?,
7878
@Parameter(
7979
description = "조회수 가중치",
80-
example="1.0",
80+
example = "1.0",
8181
)@RequestParam(value = "viewsWeight", required = false) viewsWeight: kotlin.Double?,
82-
): ResponseEntity<HotStandardDto>{
83-
val hotStandardDto = articleService.hotsUpdate(
84-
hotScore, viewsWeight
85-
)
82+
): ResponseEntity<HotStandardDto> {
83+
val hotStandardDto =
84+
articleService.hotsUpdate(
85+
hotScore,
86+
viewsWeight,
87+
)
8688
return ResponseEntity.ok(hotStandardDto)
8789
}
90+
8891
@Operation(summary = "게시글 생성", description = "게시판에 글이 작성되는지 확인.")
8992
@ApiResponses(
9093
value = [
@@ -162,6 +165,7 @@ class ArticleController(
162165
return ResponseEntity.ok(articlePagingResponse)
163166
}
164167

168+
// 정수만 입력 받도록 설정
165169
@Operation(summary = "특정 게시글 조회", description = "게시글 ID로 게시글 상세 정보 조회")
166170
@ApiResponses(
167171
value = [
@@ -170,7 +174,6 @@ class ArticleController(
170174
],
171175
)
172176
@GetMapping("/{articleId}")
173-
//정수만 입력 받도록 설정
174177
fun get(
175178
@Parameter(
176179
description = "게시글 ID",

src/main/kotlin/com/wafflestudio/team2server/article/repository/ArticleRepository.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ WHERE
134134
135135
ORDER BY a.published_at DESC, a.id DESC
136136
LIMIT :limit
137-
"""
137+
""",
138138
)
139139
fun findHotsWithCursor(
140140
@Param("keyword") keyword: String?,
@@ -144,5 +144,4 @@ LIMIT :limit
144144
@Param("hotScore") hotScore: Long,
145145
@Param("viewsWeight") viewsWeight: Double,
146146
): List<ArticleWithBoard>
147-
148147
}

src/main/kotlin/com/wafflestudio/team2server/article/service/ArticleService.kt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import com.wafflestudio.team2server.board.BoardNotFoundException
1818
import com.wafflestudio.team2server.board.repository.BoardRepository
1919
import com.wafflestudio.team2server.hotstandard.dto.core.HotStandardDto
2020
import com.wafflestudio.team2server.hotstandard.repository.HotStandardRepository
21-
import org.apache.catalina.core.StandardService
2221
import org.springframework.context.ApplicationEventPublisher
2322
import org.springframework.data.repository.findByIdOrNull
2423
import org.springframework.stereotype.Service
@@ -72,8 +71,9 @@ class ArticleService(
7271
nextId,
7372
queryLimit,
7473
)
75-
return paging(articleWithBoards,limit)
74+
return paging(articleWithBoards, limit)
7675
}
76+
7777
fun pageByHots(
7878
keyword: String?,
7979
nextPublishedAt: Instant?,
@@ -92,7 +92,7 @@ class ArticleService(
9292
hotScore = standard.hotScore,
9393
viewsWeight = standard.viewsWeight,
9494
)
95-
return paging(articleWithBoards,limit)
95+
return paging(articleWithBoards, limit)
9696
}
9797

9898
fun create(
@@ -175,6 +175,7 @@ class ArticleService(
175175
eventPublisher.publishEvent(ArticleCreatedEvent(savedArticle))
176176
return savedArticle
177177
}
178+
178179
fun paging(
179180
articleList: List<ArticleWithBoard>,
180181
limit: Int,
@@ -190,17 +191,24 @@ class ArticleService(
190191
ArticlePaging(newNextPublishedAt?.toEpochMilli(), newNextId, hasNext),
191192
)
192193
}
194+
193195
fun hotsUpdate(
194196
hotScore: Long?,
195197
viewsWeight: Double?,
196-
): HotStandardDto{
197-
val standard = hotStandardRepository.findByIdOrNull(1L) ?: throw StandardNotFoundException()
198+
): HotStandardDto {
199+
val standard =
200+
hotStandardRepository.findByIdOrNull(1L)
201+
?: throw StandardNotFoundException()
202+
198203
hotScore?.let { standard.hotScore = it }
199-
viewsWeight?.let {standard.viewsWeight = it}
204+
viewsWeight?.let { standard.viewsWeight = it }
205+
206+
val saved = hotStandardRepository.save(standard)
207+
200208
return HotStandardDto(
201-
id = standard.id!!,
202-
hotScore = hotScore!!,
203-
viewsWeight = viewsWeight!!,
209+
id = saved.id!!,
210+
hotScore = saved.hotScore,
211+
viewsWeight = saved.viewsWeight,
204212
)
205213
}
206214
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.wafflestudio.team2server.hotstandard.dto.core
22

3-
class HotStandardDto (
3+
class HotStandardDto(
44
val id: Long,
55
val hotScore: Long,
66
val viewsWeight: Double,
7-
)
7+
)

src/main/resources/db/migration/V11_add_hots_standard.sql renamed to src/main/resources/db/migration/V11__add_hots_standard.sql

File renamed without changes.

src/test/kotlin/com/wafflestudio/team2server/ArticleIntegrationTests.kt

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -279,43 +279,43 @@ class ArticleIntegrationTests
279279
jsonPath("$.data[?(@.id == ${article.id})].views").value(4),
280280
)
281281
}
282-
@Test
283-
fun `should promote article to hot board based on view count`(){
284-
val article = dataGenerator.generateArticle()
285-
286-
mvc.perform(
287-
get("/api/v1/articles/hots")
288-
.param("limit", "20")
289-
.contentType(MediaType.APPLICATION_JSON),
290-
)
291-
.andExpect(status().isOk)
292-
.andExpect(jsonPath("$.data[?(@.id == ${article.id})]").isEmpty)
293-
294-
repeat(5) {
295-
mvc.perform(
296-
get("/api/v1/articles/{articleId}", article.id)
297-
.contentType(MediaType.APPLICATION_JSON),
298-
).andExpect(status().isOk)
299-
}
300282

301-
mvc.perform(
302-
patch("/api/v1/articles/hots")
303-
.param("hotScore", "4")
304-
.contentType(MediaType.APPLICATION_JSON),
305-
)
306-
.andExpect(status().isOk)
307-
.andExpect(jsonPath("$.hotScore").value(4))
283+
@Test
284+
fun `should promote article to hot board based on view count`() {
285+
val article = dataGenerator.generateArticle()
308286

309-
mvc.perform(
310-
get("/api/v1/articles/hots")
311-
.param("limit", "20")
312-
.contentType(MediaType.APPLICATION_JSON),
313-
)
314-
.andExpect(status().isOk)
315-
.andExpect(jsonPath("$.data[?(@.id == ${article.id})]").isNotEmpty)
287+
mvc
288+
.perform(
289+
get("/api/v1/articles/hots")
290+
.param("limit", "20")
291+
.contentType(MediaType.APPLICATION_JSON),
292+
).andExpect(status().isOk)
293+
.andExpect(jsonPath("$.data[?(@.id == ${article.id})]").isEmpty)
294+
295+
repeat(5) {
296+
mvc
297+
.perform(
298+
get("/api/v1/articles/{articleId}", article.id)
299+
.contentType(MediaType.APPLICATION_JSON),
300+
).andExpect(status().isOk)
301+
}
316302

303+
mvc
304+
.perform(
305+
patch("/api/v1/articles/hots")
306+
.param("hotScore", "4")
307+
.contentType(MediaType.APPLICATION_JSON),
308+
).andExpect(status().isOk)
309+
.andExpect(jsonPath("$.hotScore").value(4))
317310

318-
}
311+
mvc
312+
.perform(
313+
get("/api/v1/articles/hots")
314+
.param("limit", "20")
315+
.contentType(MediaType.APPLICATION_JSON),
316+
).andExpect(status().isOk)
317+
.andExpect(jsonPath("$.data[?(@.id == ${article.id})]").isNotEmpty)
318+
}
319319

320320
private fun assertArticlesAreSorted(articles: List<ArticleDto>) {
321321
if (articles.size <= 1) return

0 commit comments

Comments
 (0)