Skip to content

Commit

Permalink
marge :: (#14) 내가 쓴 주간 행사 일정 조회
Browse files Browse the repository at this point in the history
marge :: (#14) 내가 쓴 주간 행사 일정 조회
  • Loading branch information
geunoo authored Dec 12, 2023
2 parents 297ea33 + 2b6ac3a commit 56c6b2a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.kodomo.juganbbojjak.domain.event_schedule.dto.response

import com.kodomo.juganbbojjak.domain.event_schedule.model.EventSchedule
import java.time.LocalDate
import java.util.UUID

data class QueryEventScheduleDetailResponse(
val startDate: LocalDate,
Expand All @@ -10,6 +11,7 @@ data class QueryEventScheduleDetailResponse(
)

data class EventDetailResponse(
val eventScheduleId: UUID,
val date: LocalDate,
val name: String,
val place: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface EventSchedulePort : QueryEventSchedulePort, CommandEventSchedulePort {

interface QueryEventSchedulePort {
fun queryWeeklyEventScheduleById(weeklyEventScheduleId: UUID): WeeklyEventSchedule?
fun queryEventSchedulesByWeeklyEventScheduleId(weeklyEventScheduleId: UUID): List<EventSchedule>
fun queryEventSchedulesByWeeklyEventScheduleId(weeklyEventScheduleId: UUID, userId: UUID?): List<EventSchedule>
}

interface CommandEventSchedulePort {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
package com.kodomo.juganbbojjak.domain.event_schedule.usecase

import com.kodomo.juganbbojjak.common.annotation.UseCase
import com.kodomo.juganbbojjak.common.spi.SecurityPort
import com.kodomo.juganbbojjak.domain.event_schedule.dto.response.EventDetailResponse
import com.kodomo.juganbbojjak.domain.event_schedule.dto.response.QueryEventScheduleDetailResponse
import com.kodomo.juganbbojjak.domain.event_schedule.exception.WeeklyEventScheduleNotFoundException
import com.kodomo.juganbbojjak.domain.event_schedule.spi.QueryEventSchedulePort
import com.kodomo.juganbbojjak.domain.user.exception.UserNotFoundException
import com.kodomo.juganbbojjak.domain.user.model.Authority
import com.kodomo.juganbbojjak.domain.user.spi.QueryUserPort
import java.util.UUID

@UseCase
class QueryEventScheduleDetailUseCase(
private val queryEventSchedulePort: QueryEventSchedulePort,
private val queryUserPort: QueryUserPort,
private val securityPort: SecurityPort,
) {

fun execute(weeklyEventScheduleId: UUID): QueryEventScheduleDetailResponse {
val user = queryUserPort.queryUserById(securityPort.getCurrentUserId()) ?: throw UserNotFoundException
val weeklyEventSchedule = queryEventSchedulePort.queryWeeklyEventScheduleById(weeklyEventScheduleId)
?: throw WeeklyEventScheduleNotFoundException
val eventSchedules = queryEventSchedulePort.queryEventSchedulesByWeeklyEventScheduleId(weeklyEventSchedule.id)
val eventSchedules = when (user.authority) {
Authority.USER -> queryEventSchedulePort.queryEventSchedulesByWeeklyEventScheduleId(
weeklyEventScheduleId,
user.id,
)

Authority.ADMIN -> queryEventSchedulePort.queryEventSchedulesByWeeklyEventScheduleId(
weeklyEventScheduleId,
null,
)
}

return QueryEventScheduleDetailResponse(
startDate = weeklyEventSchedule.startDate,
endDate = weeklyEventSchedule.endDate,
eventSchedules = eventSchedules.stream()
.map {
EventDetailResponse(
eventScheduleId = it.id,
date = it.date,
name = it.name,
place = it.place,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.kodomo.juganbbojjak.domain.event_schedule.persistence.repository.Even
import com.kodomo.juganbbojjak.domain.event_schedule.persistence.repository.WeeklyEventScheduleJpaRepository
import com.kodomo.juganbbojjak.domain.event_schedule.spi.EventSchedulePort
import com.kodomo.juganbbojjak.global.annotation.Adapter
import com.querydsl.core.types.dsl.BooleanExpression
import com.querydsl.jpa.impl.JPAQueryFactory
import org.springframework.data.repository.findByIdOrNull
import java.util.UUID
Expand All @@ -28,11 +29,17 @@ class EvenSchedulePersistenceAdapter(
weeklyEventScheduleJpaRepository.findByIdOrNull(weeklyEventScheduleId)
)

override fun queryEventSchedulesByWeeklyEventScheduleId(weeklyEventScheduleId: UUID): List<EventSchedule> {
override fun queryEventSchedulesByWeeklyEventScheduleId(
weeklyEventScheduleId: UUID,
userId: UUID?
): List<EventSchedule> {
return queryFactory
.selectFrom(eventScheduleEntity)
.join(eventScheduleEntity.weeklyEventScheduleEntity, weeklyEventScheduleEntity)
.where(weeklyEventScheduleEntity.id.eq(weeklyEventScheduleId))
.where(
weeklyEventScheduleEntity.id.eq(weeklyEventScheduleId),
eqUserId(userId)
)
.orderBy(eventScheduleEntity.date.asc())
.fetch().stream()
.map { eventScheduleMapper.toDomain(it)!! }
Expand All @@ -42,4 +49,11 @@ class EvenSchedulePersistenceAdapter(
override fun saveAllEventSchedule(eventSchedule: List<EventSchedule>) {
eventScheduleJpaRepository.saveAll(eventSchedule.stream().map { eventScheduleMapper.toEntity(it) }.toList())
}

//==condition==//

private fun eqUserId(userId: UUID?): BooleanExpression? =
if (userId != null)
eventScheduleEntity.userEntity.id.eq(userId)
else null
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.kodomo.juganbbojjak.global.security

import com.kodomo.juganbbojjak.common.spi.SecurityPort
import com.kodomo.juganbbojjak.domain.user.model.Authority
import com.kodomo.juganbbojjak.global.annotation.Adapter
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.crypto.password.PasswordEncoder
import java.util.UUID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class SecurityConfig(
.authorizeHttpRequests {
it
.requestMatchers(HttpMethod.POST, "/event_schedules/{weekly-event-schedule-id}").hasAuthority(USER.name)
.requestMatchers(HttpMethod.GET, "/event_schedules/{weekly-event-schedule-id}").hasAuthority(ADMIN.name)
.requestMatchers(HttpMethod.GET, "/event_schedules/{weekly-event-schedule-id}").hasAnyAuthority(ADMIN.name, USER.name)

.requestMatchers(HttpMethod.POST, "/work_report/{weekly-work-report-id}").hasAuthority(USER.name)
.requestMatchers(HttpMethod.GET, "/work_report/{weekly-work-report-id}").hasAnyAuthority(USER.name, ADMIN.name)

Expand Down

0 comments on commit 56c6b2a

Please sign in to comment.