Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 53 additions & 7 deletions src/main/kotlin/com/beat_it/cal/controller/ScheduleController.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package com.beat_it.cal.controller

import com.beat_it.cal.dto.CalendarSchedulesResponse
import com.beat_it.cal.dto.DateSchedulesResponse
import com.beat_it.cal.dto.ScheduleCreateRequest
import com.beat_it.cal.dto.ScheduleCreateResponse
import com.beat_it.cal.dto.ScheduleDetailResponse
import com.beat_it.cal.dto.ScheduleUpdateRequest
import com.beat_it.cal.service.ScheduleService
import com.beat_it.global.error.BusinessException
import com.beat_it.global.error.ErrorCode
import com.beat_it.global.response.BasicResponse
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.web.bind.annotation.*

@Tag(name = "4. CALENDAR API", description = "일정 관련 로직")
@RestController
@RequestMapping("/calendar")
class ScheduleController(
Expand All @@ -18,11 +26,12 @@ class ScheduleController(

@PostMapping
fun createSchedule(
@RequestHeader("X-USER-ID") userId: Long,
@AuthenticationPrincipal userDetails: UserDetails?,
@RequestBody request: ScheduleCreateRequest
): ResponseEntity<BasicResponse<ScheduleCreateResponse>> {

val responseData = scheduleService.createSchedule(userId, request)
val currentUserId = userDetails?.username?.toLong()?: throw BusinessException(ErrorCode.UNAUTHORIZED)
val responseData = scheduleService.createSchedule(currentUserId, request)

return ResponseEntity
.status(HttpStatus.CREATED)
Expand All @@ -31,22 +40,26 @@ class ScheduleController(

@PatchMapping("/{scheduleId}")
fun updateSchedule(
@RequestHeader("X-USER-ID") userId: Long,
@AuthenticationPrincipal userDetails: UserDetails?,
@PathVariable scheduleId: Long,
@RequestBody request: ScheduleUpdateRequest
): ResponseEntity<BasicResponse<ScheduleCreateResponse>> {
val responseData = scheduleService.updateSchedule(scheduleId, userId, request)
val currentUserId = userDetails?.username?.toLong()
?: throw BusinessException(ErrorCode.UNAUTHORIZED)
val responseData = scheduleService.updateSchedule(scheduleId, currentUserId, request)
return ResponseEntity
.status(HttpStatus.OK)
.body(BasicResponse.success(responseData, HttpStatus.OK, "일정이 수정되었습니다."))
}

@DeleteMapping("/{scheduleId}")
fun deleteSchedule(
@RequestHeader("X-USER-ID") userId: Long,
@AuthenticationPrincipal userDetails: UserDetails?,
@PathVariable scheduleId: Long
): ResponseEntity<BasicResponse<Nothing>> {
scheduleService.deleteSchedule(scheduleId, userId)
val currentUserId = userDetails?.username?.toLong()
?: throw BusinessException(ErrorCode.UNAUTHORIZED)
scheduleService.deleteSchedule(scheduleId, currentUserId)
return ResponseEntity
.status(HttpStatus.OK)
.body(BasicResponse.success(HttpStatus.OK, "일정이 성공적으로 삭제되었습니다.")
Expand All @@ -55,12 +68,45 @@ class ScheduleController(

@GetMapping("/{scheduleId}")
fun getScheduleDetail(
@AuthenticationPrincipal userDetails: UserDetails?,
@PathVariable scheduleId: Long
): ResponseEntity<BasicResponse<ScheduleDetailResponse>> {
val responseData = scheduleService.getScheduleDetail(scheduleId)
val currentUserId = userDetails?.username?.toLong()
?: throw BusinessException(ErrorCode.UNAUTHORIZED)
val responseData = scheduleService.getScheduleDetail(scheduleId, currentUserId)
return ResponseEntity
.status(HttpStatus.OK)
.body(BasicResponse.success(responseData, HttpStatus.OK, "일정 상세 조회에 성공했습니다.")
)
}

@GetMapping("/month")
fun getCalendarSchedules(
@AuthenticationPrincipal userDetails: UserDetails?,
@RequestParam year: Int,
@RequestParam month: Int
): ResponseEntity<BasicResponse<CalendarSchedulesResponse>> {
val currentUserId = userDetails?.username?.toLong()
?: throw BusinessException(ErrorCode.UNAUTHORIZED)
val responseData = scheduleService.getCalendarSchedules(currentUserId, year, month)
return ResponseEntity
.status(HttpStatus.OK)
.body(BasicResponse.success(responseData, HttpStatus.OK, "공유 캘린더 범위 조회에 성공했습니다."))
}

@GetMapping("/date")
fun getDateSchedules(
@AuthenticationPrincipal userDetails: UserDetails?,
@RequestParam year: Int,
@RequestParam month: Int,
@RequestParam date: Int
): ResponseEntity<BasicResponse<DateSchedulesResponse>> {
val currentUserId = userDetails?.username?.toLong()
?: throw BusinessException(ErrorCode.UNAUTHORIZED)
val responseData = scheduleService.getDateSchedules(currentUserId, year, month, date)
return ResponseEntity
.status(HttpStatus.OK)
.body(BasicResponse.success(responseData, HttpStatus.OK, "선택 날짜 일정 조회에 성공했습니다."))
}

}
15 changes: 15 additions & 0 deletions src/main/kotlin/com/beat_it/cal/dto/CalendarSchedulesResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.beat_it.cal.dto

import com.fasterxml.jackson.annotation.JsonProperty
import java.time.OffsetDateTime

data class CalendarSchedulesResponse(
val items: List<CalendarSchedule>
)

data class CalendarSchedule(
val scheduleId: Long,
val title: String,
val startsAt: OffsetDateTime,
val endsAt: OffsetDateTime
)
17 changes: 17 additions & 0 deletions src/main/kotlin/com/beat_it/cal/dto/DateScheduleResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.beat_it.cal.dto

import com.fasterxml.jackson.annotation.JsonProperty
import java.time.OffsetDateTime

data class DateSchedulesResponse(
val items: List<DateSchedule>
)

data class DateSchedule(
val scheduleId: Long,
val title: String,
val content: String,
val startsAt: OffsetDateTime,
val endsAt: OffsetDateTime,
val locationId: Long?
)
9 changes: 0 additions & 9 deletions src/main/kotlin/com/beat_it/cal/dto/ScheduleCreateRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@ import com.fasterxml.jackson.annotation.JsonProperty
import java.time.OffsetDateTime

data class ScheduleCreateRequest(
@JsonProperty("location_id")
val locationId: Long?,

val title: String?,

val content: String?,

@JsonProperty("starts_at")
val startsAt: OffsetDateTime?,

@JsonProperty("ends_at")
val endsAt: OffsetDateTime?,

@JsonProperty("participant_user_ids")
val participantUserIds: List<Long> = emptyList()
)
8 changes: 0 additions & 8 deletions src/main/kotlin/com/beat_it/cal/dto/ScheduleCreateResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@ import com.fasterxml.jackson.annotation.JsonProperty
import java.time.OffsetDateTime

data class ScheduleCreateResponse(
@JsonProperty("schedule_id")
val scheduleId: Long,

val title: String,

@JsonProperty("starts_at")
val startsAt: OffsetDateTime,

@JsonProperty("ends_at")
val endsAt: OffsetDateTime,

@JsonProperty("created_at")
val createdAt: OffsetDateTime
)
20 changes: 10 additions & 10 deletions src/main/kotlin/com/beat_it/cal/dto/ScheduleDetailResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import com.fasterxml.jackson.annotation.JsonProperty
import java.time.OffsetDateTime

data class ScheduleDetailResponse(
@JsonProperty("schedule_id") val scheduleId: Long,
@JsonProperty("team_id") val teamId: Long,
@JsonProperty("user_id") val userId: Long,
@JsonProperty("location_id") val locationId: Long?,
val scheduleId: Long,
val teamId: Long,
val userId: Long,
val locationId: Long?,
val title: String,
val content: String?,
@JsonProperty("starts_at") val startsAt: OffsetDateTime,
@JsonProperty("ends_at") val endsAt: OffsetDateTime,
@JsonProperty("created_at") val createdAt: OffsetDateTime,
@JsonProperty("updated_at") val updatedAt: OffsetDateTime,
val startsAt: OffsetDateTime,
val endsAt: OffsetDateTime,
val createdAt: OffsetDateTime,
val updatedAt: OffsetDateTime,
val participants: List<ParticipantResponse>
)

data class ParticipantResponse(
@JsonProperty("schedule_participant_id") val scheduleParticipantId: Long,
@JsonProperty("user_id") val userId: Long
val scheduleParticipantId: Long,
val userId: Long
)
8 changes: 0 additions & 8 deletions src/main/kotlin/com/beat_it/cal/dto/ScheduledUpdateRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@ import com.fasterxml.jackson.annotation.JsonProperty
import java.time.OffsetDateTime

data class ScheduleUpdateRequest(
@JsonProperty("location_id")
val locationId: Long?,

val title: String?,
val content: String?,

@JsonProperty("starts_at")
val startsAt: OffsetDateTime?,

@JsonProperty("ends_at")
val endsAt: OffsetDateTime?,

@JsonProperty("participant_user_ids")
val participantUserIds: List<Long>? = null
)
28 changes: 28 additions & 0 deletions src/main/kotlin/com/beat_it/cal/repository/ScheduleRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,36 @@ package com.beat_it.cal.repository

import com.beat_it.cal.entity.Schedule
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
import org.springframework.stereotype.Repository
import java.time.OffsetDateTime

@Repository
interface ScheduleRepository : JpaRepository<Schedule, Long> {
@Query("""
SELECT s FROM Schedule s
WHERE s.userId = :userId
AND s.startsAt <= :endDateTime
AND s.endsAt >= :startDateTime
ORDER BY s.startsAt ASC
""")
fun findByUserIdAndMonthRange(
@Param("userId") userId: Long,
@Param("startDateTime") startDateTime: OffsetDateTime,
@Param("endDateTime") endDateTime: OffsetDateTime
): List<Schedule>

@Query("""
SELECT s FROM Schedule s
WHERE s.userId = :userId
AND s.startsAt <= :endDateTime
AND s.endsAt >= :startDateTime
ORDER BY s.startsAt ASC
""")
fun findByUserIdAndDailyRange(
@Param("userId") userId: Long,
@Param("startDateTime") startDateTime: OffsetDateTime,
@Param("endDateTime") endDateTime: OffsetDateTime
): List<Schedule>
}
Loading