-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoardService.kt
70 lines (63 loc) · 3.22 KB
/
BoardService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.ddd.sonnypolabobe.domain.board.service
import com.ddd.sonnypolabobe.domain.board.controller.dto.BoardCreateRequest
import com.ddd.sonnypolabobe.domain.board.controller.dto.BoardGetResponse
import com.ddd.sonnypolabobe.domain.board.repository.BoardJooqRepository
import com.ddd.sonnypolabobe.domain.polaroid.dto.PolaroidGetResponse
import com.ddd.sonnypolabobe.domain.polaroid.enumerate.PolaroidOption
import com.ddd.sonnypolabobe.domain.user.dto.UserDto
import com.ddd.sonnypolabobe.global.exception.ApplicationException
import com.ddd.sonnypolabobe.global.exception.CustomErrorCode
import com.ddd.sonnypolabobe.global.security.AuthenticatedMember
import com.ddd.sonnypolabobe.global.util.S3Util
import com.ddd.sonnypolabobe.global.util.UuidConverter
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import java.util.*
@Service
class BoardService(
private val boardJooqRepository: BoardJooqRepository,
private val s3Util: S3Util,
@Value("\${limit.count}")
private val limit: Int
) {
fun create(request: BoardCreateRequest): UUID =
this.boardJooqRepository.insertOne(request)?.let { UuidConverter.byteArrayToUUID(it) }
?: throw ApplicationException(CustomErrorCode.BOARD_CREATED_FAILED)
fun getById(id: String, user: AuthenticatedMember?): List<BoardGetResponse> {
return id.run {
val queryResult =
boardJooqRepository.selectOneById(UuidConverter.stringToUUID(this@run))
if(queryResult.isEmpty()) throw ApplicationException(CustomErrorCode.BOARD_NOT_FOUND)
val groupByTitle = queryResult.groupBy { it.title }
groupByTitle.map { entry ->
val title = entry.key
val polaroids = entry.value.map {
PolaroidGetResponse(
id = it.polaroidId ?: 0L,
imageUrl = it.imageKey?.let { it1 -> s3Util.getImgUrl(it1) } ?: "",
oneLineMessage = it.oneLineMessage ?: "폴라보와의 추억 한 줄",
userId = it.userId ?: 0L,
nickname = it.nickname ?: "",
isMine = it.userId == user?.id?.toLong(),
createdAt = it.createdAt,
options = it.options?.let{ ObjectMapper().readValue(it, object : TypeReference<Map<PolaroidOption, String>>() {})}
)
}.filter { it.id != 0L }.distinctBy { it.id }
BoardGetResponse(title = title ?: "", items = polaroids, isMine = queryResult.first().ownerId == user?.id?.toLong())
}
}
}
fun getTotalCount(): Long = this.boardJooqRepository.selectTotalCount()
fun createAvailable(): Long {
return this.boardJooqRepository.selectTodayTotalCount().let {
if (it > limit) 0 else limit - it
}
}
fun recommendTitle(user: UserDto.Companion.Res): List<String> {
val userBirthYear = user.birthDt
val userGender = user.gender
return this.boardJooqRepository.selectRecommendTitle(userBirth = userBirthYear, userGender)
}
}