Skip to content

Commit

Permalink
Merge branch 'feture' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
gurdl0525 committed Nov 11, 2023
2 parents 3a2f1b1 + e994f44 commit 5c235c2
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 182 deletions.
13 changes: 9 additions & 4 deletions src/main/kotlin/com/example/onui/domain/diary/entity/Diary.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ Diary(
user: User,
content: String?,
mood: Mood,
tag: MutableList<String>,
tagList: MutableList<String>,
createdAt: LocalDateTime,
image: String? = null,
id: UUID? = null
id: UUID? = null,
isPosted: Boolean = false
) : BaseTimeEntity(createdAt) {

@Id
Expand All @@ -42,13 +43,17 @@ Diary(

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "diary_tag", joinColumns = [JoinColumn(name = "diary_id", referencedColumnName = "id")])
var tag: MutableList<String> = tag
var tagList: MutableList<String> = tagList
protected set

@Column(name = "image", nullable = true)
var image: String? = image
protected set

@Column(name = "is_posted", nullable = false, columnDefinition = "BIT")
var isPosted: Boolean = isPosted
protected set

fun toResponse() = DiaryResponse(
this.id!!,
this.mood,
Expand All @@ -59,7 +64,7 @@ Diary(
this.id!!,
this.content,
this.mood,
this.tag,
this.tagList,
this.createdAt.toLocalDate(),
this.image
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import java.util.*
@Repository
interface DiaryRepository : JpaRepository<Diary, UUID?> {

fun findAllByUserAndYearAndMonth(user: User, year: Int, month: Int): MutableList<Diary>?
fun findAllByUserAndYearAndMonthOrderByCreatedAtAsc(user: User, year: Int, month: Int): MutableList<Diary>?

fun findByUserAndYearAndMonthAndDay(user: User, year: Int, month: Int, day: Int): Diary?

fun existsByIdAndIsPosted(id: UUID, isPosted: Boolean): Boolean
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.example.onui.domain.diary.repository

import com.example.onui.domain.diary.entity.Diary
import com.example.onui.domain.diary.entity.QDiary.diary
import com.example.onui.domain.diary.presentation.response.DiaryResponse
import com.example.onui.domain.user.entity.User
import com.querydsl.jpa.impl.JPAQueryFactory
import org.springframework.stereotype.Repository
Expand All @@ -17,24 +15,14 @@ class QDiaryRepositoryImpl(
private val queryFactory: JPAQueryFactory
) : QDiaryRepository {

override fun findThreeDayAgoByUser(user: User): Diary? {
override fun findThreeDayAgoByUser(user: User) = queryFactory.selectFrom(diary)
.orderBy(diary.createdAt.desc())
.where(diary.user.eq(user).and(diary.createdAt.after(LocalDateTime.now().minusDays(3))))
.limit(1)
.fetchOne()

val ago = LocalDateTime.now().minusDays(3)

return queryFactory.selectFrom(diary)
.orderBy(diary.createdAt.desc())
.where(diary.user.eq(user).and(diary.createdAt.after(ago)))
.limit(1)
.fetchOne()
}

override fun findSevenDayAgoByUser(user: User): MutableList<DiaryResponse> {

val ago = LocalDateTime.now().minusDays(7)

return queryFactory.selectFrom(diary)
.orderBy(diary.createdAt.desc())
.where(diary.user.eq(user).and(diary.createdAt.after(ago)))
.fetch().map { it.toResponse() }.toMutableList()
}
override fun findSevenDayAgoByUser(user: User) = queryFactory.selectFrom(diary)
.orderBy(diary.createdAt.desc())
.where(diary.user.eq(user).and(diary.createdAt.after(LocalDateTime.now().minusDays(7))))
.fetch().map { it.toResponse() }.toMutableList()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import com.example.onui.domain.diary.presentation.response.DiaryDetailResponse
import com.example.onui.domain.diary.presentation.response.DiaryListResponse
import com.example.onui.domain.diary.repository.DiaryRepository
import com.example.onui.domain.diary.repository.QDiaryRepository
import com.example.onui.domain.timeline.entity.Timeline
import com.example.onui.domain.timeline.repository.TimelineRepository
import com.example.onui.global.common.facade.UserFacade
import com.example.onui.global.config.error.exception.PermissionDeniedException
import org.springframework.data.repository.findByIdOrNull
Expand All @@ -23,7 +21,6 @@ import java.util.*
class DiaryServiceImpl(
private val diaryRepository: DiaryRepository,
private val userFacade: UserFacade,
private val timelineRepository: TimelineRepository,
private val qDiaryRepository: QDiaryRepository
) : DiaryService {

Expand All @@ -33,36 +30,28 @@ class DiaryServiceImpl(
val user = userFacade.getCurrentUser()
val now = LocalDateTime.now()

val diary = Diary(
user,
req.content,
req.mood!!,
req.tagList!!,
now,
req.image,
diaryRepository.findByUserAndYearAndMonthAndDay(
user,
now.year,
now.monthValue,
now.dayOfMonth
)?.id
)
val diary =
diaryRepository.findByUserAndYearAndMonthAndDay(user, now.year, now.monthValue, now.dayOfMonth)?.let {
Diary(
user, req.content, req.mood!!, req.tagList!!, it.createdAt, req.image, it.id, it.isPosted
)
} ?: Diary(
user, req.content, req.mood!!, req.tagList!!, now, req.image
)

diaryRepository.save(diary)
user.diaryList.add(diary)

return diary.toDetailResponse()
}

override fun getDiaryByMonth(year: Int, month: Int) = DiaryListResponse(
diaryRepository.findAllByUserAndYearAndMonth(userFacade.getCurrentUser(), year, month)?.map {
diaryRepository.findAllByUserAndYearAndMonthOrderByCreatedAtAsc(userFacade.getCurrentUser(), year, month)?.map {
it.toResponse()
}?.toMutableList()
)

override fun getDetailById(id: UUID): DiaryDetailResponse {
val diary = diaryRepository.findByIdOrNull(id)
?: throw DiaryNotFoundException
val diary = diaryRepository.findByIdOrNull(id) ?: throw DiaryNotFoundException

if (diary.user != userFacade.getCurrentUser()) throw PermissionDeniedException

Expand All @@ -73,36 +62,15 @@ class DiaryServiceImpl(
override fun update(req: UpdateDiaryRequest): DiaryDetailResponse {
val user = userFacade.getCurrentUser()

var diary = diaryRepository.findByIdOrNull(req.id)
?: throw DiaryNotFoundException
val diary = diaryRepository.findByIdOrNull(req.id) ?: throw DiaryNotFoundException

if (diary.user != user) throw PermissionDeniedException

diary = diaryRepository.save(
return diaryRepository.save(
Diary(
diary.user,
req.content,
req.mood!!,
req.tagList!!,
diary.createdAt,
req.image,
diary.id
diary.user, req.content, req.mood!!, req.tagList!!, diary.createdAt, req.image, diary.id
)
)

timelineRepository.findByIdOrNull(diary.id)
?.run {
timelineRepository.save(
Timeline(
diary,
diary.createdAt,
id,
true,
)
)
}

return diary.toDetailResponse()
).toDetailResponse()
}

override fun getSevenDaysAgo(): DiaryListResponse {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.onui.domain.timeline.exception

import com.example.onui.global.config.error.data.ErrorCode
import com.example.onui.global.config.error.exception.BusinessException

object InvalidDateFormatException : BusinessException(ErrorCode.INVALID_DATE_FORMAT)
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.example.onui.domain.timeline.presentation

import com.example.onui.domain.timeline.exception.InvalidDateFormatException
import com.example.onui.domain.timeline.service.TimeLineService
import org.springframework.http.HttpStatus
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import java.time.DayOfWeek
import java.util.UUID
import java.time.LocalDate
import java.time.format.DateTimeParseException
import java.util.*

@Validated
@RestController
Expand All @@ -17,17 +19,17 @@ class TimelineController(
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
fun createTimeline(
@RequestParam("id", required = true)
id: UUID
@RequestParam("id", required = true) id: UUID
) = timelineService.post(id)

@GetMapping
fun getByDayOfWeek(
@RequestParam("idx", required = true)
idx: Int = 0,
@RequestParam("size", required = true)
size: Int = 5,
@RequestParam("day_of_week", required = true)
dayOfWeek: DayOfWeek
) = timelineService.searchByDayOfWeek(idx, size, dayOfWeek)
fun getByDate(
@RequestParam("idx", required = true) idx: Int = 0,
@RequestParam("size", required = true) size: Int = 5,
@RequestParam("date", required = true) date: String
) = try {
timelineService.searchByDate(idx, size, LocalDate.parse(date))
} catch (e: DateTimeParseException) {
throw InvalidDateFormatException
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.onui.domain.timeline.repository

import com.example.onui.domain.diary.presentation.response.DiaryDetailResponse
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import java.time.LocalDate

interface QTimelineRepository {

fun findPageByDate(pageable: Pageable, date: LocalDate): Page<DiaryDetailResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.onui.domain.timeline.repository

import com.example.onui.domain.diary.entity.QDiary.diary
import com.example.onui.domain.diary.presentation.response.DiaryDetailResponse
import com.querydsl.jpa.impl.JPAQueryFactory
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageImpl
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import java.time.LocalDate

@Service
class QTimelineRepositoryImpl(
private val query: JPAQueryFactory
) : QTimelineRepository {

override fun findPageByDate(pageable: Pageable, date: LocalDate): Page<DiaryDetailResponse> {
val query = query.selectFrom(diary).where(
diary.isPosted.eq(true).and(
diary.day.eq(date.dayOfMonth).and(
diary.month.eq(date.monthValue).and(
diary.year.eq(date.year)
)
)
)
).orderBy(diary.createdAt.desc()).offset(pageable.offset).limit(pageable.pageSize.toLong())

val iterable = query.fetch().map { it.toDetailResponse() }

return PageImpl(iterable, pageable, iterable.size.toLong())
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.example.onui.domain.timeline.service

import com.example.onui.domain.timeline.presentation.dto.response.TimelineResponse
import com.example.onui.domain.diary.presentation.response.DiaryDetailResponse
import org.springframework.data.domain.Page
import java.time.DayOfWeek
import java.time.LocalDate
import java.util.*

interface TimeLineService {

fun post(id: UUID): TimelineResponse
fun post(id: UUID): DiaryDetailResponse

fun searchByDayOfWeek(idx: Int, size: Int, dayOfWeek: DayOfWeek): Page<TimelineResponse>
fun searchByDate(idx: Int, size: Int, date: LocalDate): Page<DiaryDetailResponse>
}
Loading

0 comments on commit 5c235c2

Please sign in to comment.