1
1
package com.ddd.sonnypolabobe.domain.board.repository
2
2
3
3
import com.ddd.sonnypolabobe.domain.board.controller.dto.BoardCreateRequest
4
- import com.ddd.sonnypolabobe.domain.board.controller.dto.BoardGetResponse
5
4
import com.ddd.sonnypolabobe.domain.board.my.dto.MyBoardDto
6
5
import com.ddd.sonnypolabobe.domain.board.repository.vo.BoardGetOneVo
7
6
import com.ddd.sonnypolabobe.domain.user.dto.GenderType
@@ -10,14 +9,14 @@ import com.ddd.sonnypolabobe.global.util.UuidConverter
10
9
import com.ddd.sonnypolabobe.global.util.UuidGenerator
11
10
import com.ddd.sonnypolabobe.jooq.polabo.enums.UserGender
12
11
import com.ddd.sonnypolabobe.jooq.polabo.tables.Board
13
- import com.ddd.sonnypolabobe.jooq.polabo.tables.BoardSticker
14
12
import com.ddd.sonnypolabobe.jooq.polabo.tables.Polaroid
15
13
import com.ddd.sonnypolabobe.jooq.polabo.tables.User
16
- import org.jooq.*
14
+ import com.fasterxml.jackson.databind.ObjectMapper
15
+ import org.jooq.Condition
16
+ import org.jooq.DSLContext
17
17
import org.jooq.impl.DSL
18
18
import org.jooq.impl.DSL.*
19
19
import org.springframework.stereotype.Repository
20
- import java.sql.Timestamp
21
20
import java.time.LocalDate
22
21
import java.time.LocalDateTime
23
22
import java.time.temporal.ChronoUnit
@@ -37,6 +36,7 @@ class BoardJooqRepositoryImpl(
37
36
this .yn = 1
38
37
this .activeyn = 1
39
38
this .userId = request.userId
39
+ this .options = request.options?.let { ObjectMapper ().writeValueAsString(it) }
40
40
}
41
41
val result = this .dslContext.insertInto(jBoard)
42
42
.set(insertValue)
@@ -51,16 +51,17 @@ class BoardJooqRepositoryImpl(
51
51
52
52
return this .dslContext
53
53
.select(
54
- jBoard.ID .convertFrom { it?.let { UuidConverter .byteArrayToUUID(it) } },
54
+ jBoard.ID .convertFrom { it?.let { UuidConverter .byteArrayToUUID(it) } },
55
55
jBoard.TITLE ,
56
+ jBoard.OPTIONS .`as `(BoardGetOneVo ::options.name),
56
57
jBoard.USER_ID .`as `(BoardGetOneVo ::ownerId.name),
57
58
jPolaroid.ID .`as `(BoardGetOneVo ::polaroidId.name),
58
59
jPolaroid.IMAGE_KEY ,
59
60
jPolaroid.ONE_LINE_MESSAGE ,
60
61
jPolaroid.CREATED_AT ,
61
62
jPolaroid.USER_ID ,
62
63
jPolaroid.NICKNAME ,
63
- jPolaroid.OPTIONS
64
+ jPolaroid.OPTIONS .` as `( BoardGetOneVo ::polaroidOptions.name)
64
65
)
65
66
.from(jBoard)
66
67
.leftJoin(jPolaroid).on(
@@ -90,11 +91,21 @@ class BoardJooqRepositoryImpl(
90
91
.selectCount()
91
92
.from(jBoard)
92
93
.where(
93
- jBoard.CREATED_AT .greaterOrEqual(DateConverter .convertToKst(LocalDateTime .now().withHour(0 ).withMinute(0 ).withSecond(0 )))
94
- .and (jBoard.CREATED_AT .lessThan(DateConverter .convertToKst(LocalDateTime .now().withHour(23 ).withMinute(59 ).withSecond(59 )))
95
- .and (jBoard.YN .eq(1 ))
96
- .and (jBoard.ACTIVEYN .eq(1 ))
97
- ))
94
+ jBoard.CREATED_AT .greaterOrEqual(
95
+ DateConverter .convertToKst(
96
+ LocalDateTime .now().withHour(0 ).withMinute(0 ).withSecond(0 )
97
+ )
98
+ )
99
+ .and (
100
+ jBoard.CREATED_AT .lessThan(
101
+ DateConverter .convertToKst(
102
+ LocalDateTime .now().withHour(23 ).withMinute(59 ).withSecond(59 )
103
+ )
104
+ )
105
+ .and (jBoard.YN .eq(1 ))
106
+ .and (jBoard.ACTIVEYN .eq(1 ))
107
+ )
108
+ )
98
109
.fetchOne(0 , Long ::class .java) ? : 0L
99
110
}
100
111
@@ -143,7 +154,7 @@ class BoardJooqRepositoryImpl(
143
154
.where(jBoard.USER_ID .eq(userId).and (jBoard.YN .eq(1 )).and (jBoard.ACTIVEYN .eq(1 )))
144
155
.orderBy(jBoard.CREATED_AT .desc())
145
156
.limit(size)
146
- .offset(page)
157
+ .offset(page * size )
147
158
.fetch()
148
159
149
160
return data.map {
@@ -173,9 +184,9 @@ class BoardJooqRepositoryImpl(
173
184
val jPolaroid = Polaroid .POLAROID
174
185
val boardSubQuery =
175
186
this .dslContext.select(jBoard.ID .`as `(" id" ))
176
- .from(jBoard)
177
- .where(jBoard.USER_ID .eq(userId).and (jBoard.YN .eq(1 )).and (jBoard.ACTIVEYN .eq(1 )))
178
- .asTable()
187
+ .from(jBoard)
188
+ .where(jBoard.USER_ID .eq(userId).and (jBoard.YN .eq(1 )).and (jBoard.ACTIVEYN .eq(1 )))
189
+ .asTable()
179
190
180
191
val data = this .dslContext.select(
181
192
jBoard.ID ,
@@ -187,22 +198,24 @@ class BoardJooqRepositoryImpl(
187
198
jBoard.ID .eq(jPolaroid.BOARD_ID ).and (jPolaroid.USER_ID .eq(userId))
188
199
.and (jPolaroid.YN .eq(1 )).and (jPolaroid.ACTIVEYN .eq(1 ))
189
200
)
190
- .where(jBoard.ID .notIn(
191
- this .dslContext.select(boardSubQuery.field(" id" , ByteArray ::class .java))
192
- .from(boardSubQuery)
193
- ))
201
+ .where(
202
+ jBoard.ID .notIn(
203
+ this .dslContext.select(boardSubQuery.field(" id" , ByteArray ::class .java))
204
+ .from(boardSubQuery)
205
+ )
206
+ )
194
207
.orderBy(jBoard.CREATED_AT .desc())
195
208
.limit(size)
196
209
.offset(page)
197
210
198
211
return data
199
212
.map {
200
- MyBoardDto .Companion .PageListRes (
201
- id = UuidConverter .byteArrayToUUID(it.get(" id" , ByteArray ::class .java)!! ),
202
- title = it.get(" title" , String ::class .java)!! ,
203
- createdAt = it.get(" created_at" , LocalDateTime ::class .java)!! ,
204
- )
205
- }.distinct()
213
+ MyBoardDto .Companion .PageListRes (
214
+ id = UuidConverter .byteArrayToUUID(it.get(" id" , ByteArray ::class .java)!! ),
215
+ title = it.get(" title" , String ::class .java)!! ,
216
+ createdAt = it.get(" created_at" , LocalDateTime ::class .java)!! ,
217
+ )
218
+ }.distinct()
206
219
}
207
220
208
221
override fun selectTotalCountByParticipant (userId : Long ): Long {
@@ -220,10 +233,12 @@ class BoardJooqRepositoryImpl(
220
233
jBoard.ID .eq(jPolaroid.BOARD_ID ).and (jPolaroid.USER_ID .eq(userId))
221
234
.and (jPolaroid.YN .eq(1 )).and (jPolaroid.ACTIVEYN .eq(1 ))
222
235
)
223
- .where(jBoard.ID .notIn(
224
- this .dslContext.select(boardSubQuery.field(" id" , ByteArray ::class .java))
225
- .from(boardSubQuery)
226
- ))
236
+ .where(
237
+ jBoard.ID .notIn(
238
+ this .dslContext.select(boardSubQuery.field(" id" , ByteArray ::class .java))
239
+ .from(boardSubQuery)
240
+ )
241
+ )
227
242
.fetchOne(0 , Long ::class .java)
228
243
? : 0L
229
244
}
@@ -233,7 +248,7 @@ class BoardJooqRepositoryImpl(
233
248
val jUser = User .USER
234
249
val jPolaroid = Polaroid .POLAROID
235
250
// 현재 날짜 기준으로 연령대를 계산하는 로직
236
- var userAgeGroup : String = " 20-29세"
251
+ var userAgeGroup: String = " 20-29세"
237
252
if (userBirth != null ) {
238
253
val age = ChronoUnit .YEARS .between(userBirth, LocalDate .now())
239
254
userAgeGroup = if (age < 15 ) {
@@ -264,37 +279,43 @@ class BoardJooqRepositoryImpl(
264
279
.leftJoin(
265
280
this .dslContext.select(jPolaroid.BOARD_ID , count().`as `(" polaroid_count" ))
266
281
.from(jPolaroid)
267
- .where(jPolaroid.YN .eq(1 )
268
- .and (jPolaroid.ACTIVEYN .eq(1 )))
282
+ .where(
283
+ jPolaroid.YN .eq(1 )
284
+ .and (jPolaroid.ACTIVEYN .eq(1 ))
285
+ )
269
286
.groupBy(jPolaroid.BOARD_ID )
270
287
.asTable(" sub_query" )
271
288
)
272
289
.on(jBoard.ID .eq(field(name(" sub_query" , " board_id" ), jBoard.ID .dataType)))
273
- .where(jBoard.YN .eq(1 )
274
- .and (jBoard.ACTIVEYN .eq(1 ))
275
- .and (jBoard.CREATED_AT .greaterOrEqual(thirtyDaysAgo))
276
- .and (genderAndAgeGroupMatch(userGender, userAgeGroup))
277
- )
278
- .orderBy(field(" sub_query.polaroid_count" , Int ::class .java).desc(), jBoard.CREATED_AT .desc())
290
+ .where(
291
+ jBoard.YN .eq(1 )
292
+ .and (jBoard.ACTIVEYN .eq(1 ))
293
+ .and (jBoard.CREATED_AT .greaterOrEqual(thirtyDaysAgo))
294
+ .and (genderAndAgeGroupMatch(userGender, userAgeGroup))
295
+ )
296
+ .orderBy(
297
+ field(" sub_query.polaroid_count" , Int ::class .java).desc(),
298
+ jBoard.CREATED_AT .desc()
299
+ )
279
300
.limit(16 )
280
301
.fetchInto(String ::class .java)
281
302
}
282
303
283
304
// 성별 및 연령대 일치 조건을 위한 메서드
284
- private fun genderAndAgeGroupMatch ( userGender : GenderType , userAgeGroup : String? ): Condition {
305
+ private fun genderAndAgeGroupMatch (userGender : GenderType , userAgeGroup : String? ): Condition {
285
306
return User .USER .GENDER .eq(UserGender .valueOf(userGender.name))
286
307
.or (User .USER .BIRTH_DT .isNotNull().and (ageGroupCondition(userAgeGroup)))
287
308
}
288
309
289
310
// 연령대 계산 로직에 따른 조건을 처리하는 메서드
290
- private fun ageGroupCondition (ageGroup : String? ) : Condition {
311
+ private fun ageGroupCondition (ageGroup : String? ): Condition {
291
312
return `when `(User .USER .BIRTH_DT .ge(LocalDate .now().minusYears(15 )), " 15세 미만" )
292
- .`when `(User .USER .BIRTH_DT .ge(LocalDate .now().minusYears(19 )), " 15-19세" )
293
- .`when `(User .USER .BIRTH_DT .ge(LocalDate .now().minusYears(29 )), " 20-29세" )
294
- .`when `(User .USER .BIRTH_DT .ge(LocalDate .now().minusYears(39 )), " 30-39세" )
295
- .`when `(User .USER .BIRTH_DT .ge(LocalDate .now().minusYears(49 )), " 40-49세" )
296
- .`when `(User .USER .BIRTH_DT .ge(LocalDate .now().minusYears(59 )), " 50-59세" )
297
- .otherwise(" 60대 이상" ).eq(ageGroup);
313
+ .`when `(User .USER .BIRTH_DT .ge(LocalDate .now().minusYears(19 )), " 15-19세" )
314
+ .`when `(User .USER .BIRTH_DT .ge(LocalDate .now().minusYears(29 )), " 20-29세" )
315
+ .`when `(User .USER .BIRTH_DT .ge(LocalDate .now().minusYears(39 )), " 30-39세" )
316
+ .`when `(User .USER .BIRTH_DT .ge(LocalDate .now().minusYears(49 )), " 40-49세" )
317
+ .`when `(User .USER .BIRTH_DT .ge(LocalDate .now().minusYears(59 )), " 50-59세" )
318
+ .otherwise(" 60대 이상" ).eq(ageGroup);
298
319
}
299
320
300
321
}
0 commit comments