Skip to content

Commit

Permalink
Merge pull request #331 from apeun-gidaechi/#330
Browse files Browse the repository at this point in the history
Feature :: 시간표 CRUD 생성 #330
  • Loading branch information
yeseong0412 authored Oct 21, 2024
2 parents ebcb2eb + 1c35278 commit 65d1bba
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TimetableRepositoryCustomImpl(
return jpaQueryFactory
.selectFrom(timetableEntity)
.where(timetableEntity.workspaceId.eq(workspaceId), timetableEntity.date.eq(date))
.orderBy(timetableEntity.date.asc())
.fetch() ?: emptyList()
}

Expand All @@ -32,6 +33,7 @@ class TimetableRepositoryCustomImpl(
return jpaQueryFactory
.selectFrom(timetableEntity)
.where(timetableEntity.workspaceId.eq(workspaceId))
.orderBy(timetableEntity.date.asc())
.fetch() ?: emptyList()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.seugi.api.domain.timetable.domain.mapper

import com.seugi.api.domain.timetable.domain.TimetableEntity
import com.seugi.api.domain.timetable.domain.model.Timetable
import com.seugi.api.domain.timetable.presentation.dto.request.CreateTimetableRequest
import com.seugi.api.global.common.Mapper
import com.seugi.api.global.infra.nice.school.SchoolDateConvertor
import com.seugi.api.global.infra.nice.school.timetable.Row
Expand Down Expand Up @@ -46,4 +47,17 @@ class TimetableMapper : Mapper<Timetable, TimetableEntity> {
date = SchoolDateConvertor.dateFormat(niceData.allTiYmd)
)
}

fun requestToEntity(createTimetableRequest: CreateTimetableRequest): TimetableEntity {
return TimetableEntity(
workspaceId = createTimetableRequest.workspaceId,
grade = createTimetableRequest.grade,
classNum = createTimetableRequest.classNum,
time = createTimetableRequest.time,
subject = createTimetableRequest.subject,
date = createTimetableRequest.date,
updatedAt = LocalDateTime.now()
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ enum class TimetableException(
override val message: String,
) : CustomErrorCode {

FORBIDDEN(HttpStatus.FORBIDDEN, "FORBIDDEN", "워크스페이스에 대한 권한이 없습니다.")
FORBIDDEN(HttpStatus.FORBIDDEN, "FORBIDDEN", "워크스페이스에 대한 권한이 없습니다."),
NOT_FOUND(HttpStatus.NOT_FOUND, "NOT_FOUND", "시간표를 찾을 수 없습니다.")

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.seugi.api.domain.timetable.presentation

import com.seugi.api.domain.timetable.domain.model.Timetable
import com.seugi.api.domain.timetable.presentation.dto.request.CreateTimetableRequest
import com.seugi.api.domain.timetable.presentation.dto.request.FixTimetableRequest
import com.seugi.api.domain.timetable.service.TimetableService
import com.seugi.api.global.common.annotation.GetAuthenticatedId
import com.seugi.api.global.response.BaseResponse
Expand All @@ -12,6 +14,31 @@ class TimetableController(
private val timetableService: TimetableService,
) {

@PostMapping(path = ["", "/"])
fun createTimetable(
@GetAuthenticatedId userId: Long,
@RequestBody timetable: CreateTimetableRequest,
): BaseResponse<Unit> {
return timetableService.createTimetable(timetable, userId)
}

@PatchMapping(path = ["", "/"])
fun fixTimetable(
@GetAuthenticatedId userId: Long,
@RequestBody fixTimetableRequest: FixTimetableRequest,
): BaseResponse<Unit> {
return timetableService.fixTimetable(userId, fixTimetableRequest)
}

@DeleteMapping("/{id}")
fun deleteTimetable(
@GetAuthenticatedId userId: Long,
@PathVariable id: Long,
): BaseResponse<Unit> {
return timetableService.deleteTimetable(userId, id)
}


@PostMapping("/reset")
fun resetTimetable(
@RequestParam("workspaceId") workspaceId: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.seugi.api.domain.timetable.presentation.dto.request

data class CreateTimetableRequest(
val workspaceId: String,
val grade: String,
val classNum: String,
val time: String,
val subject: String,
val date: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.seugi.api.domain.timetable.presentation.dto.request

data class FixTimetableRequest(
val id: Long,
val subject: String,
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.seugi.api.domain.timetable.service

import com.seugi.api.domain.timetable.domain.model.Timetable
import com.seugi.api.domain.timetable.presentation.dto.request.CreateTimetableRequest
import com.seugi.api.domain.timetable.presentation.dto.request.FixTimetableRequest
import com.seugi.api.global.response.BaseResponse

interface TimetableService {
fun createTimetable(createTimetableRequest: CreateTimetableRequest, userId: Long): BaseResponse<Unit>
fun fixTimetable(userId: Long, fixTimetableRequest: FixTimetableRequest): BaseResponse<Unit>
fun deleteTimetable(userId: Long, id: Long): BaseResponse<Unit>
fun resetTimetable(workspaceId: String, userId: Long)
fun getWeekendTimetableByUserInfo(workspaceId: String, userId: Long): List<Timetable>
fun getDayTimetableByUserInfo(workspaceId: String, userId: Long): List<Timetable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import com.seugi.api.domain.timetable.domain.TimetableRepository
import com.seugi.api.domain.timetable.domain.mapper.TimetableMapper
import com.seugi.api.domain.timetable.domain.model.Timetable
import com.seugi.api.domain.timetable.exception.TimetableException
import com.seugi.api.domain.timetable.presentation.dto.request.CreateTimetableRequest
import com.seugi.api.domain.timetable.presentation.dto.request.FixTimetableRequest
import com.seugi.api.domain.workspace.domain.entity.WorkspaceEntity
import com.seugi.api.domain.workspace.domain.model.SchoolInfo
import com.seugi.api.domain.workspace.service.WorkspaceService
import com.seugi.api.global.exception.CustomException
import com.seugi.api.global.infra.nice.school.NiceSchoolService
import com.seugi.api.global.response.BaseResponse
import jakarta.transaction.Transactional
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service
Expand Down Expand Up @@ -74,6 +77,62 @@ class TimetableServiceImpl(
?: emptyList()
}

private fun checkRole(workspaceId: String, userId: Long) {
workspaceService.findWorkspaceById(workspaceId).let {
if (it.workspaceAdmin != userId &&
!it.middleAdmin.contains(userId) &&
!it.teacher.contains(userId)
) throw CustomException(
TimetableException.FORBIDDEN
)
}
}

@Transactional
override fun createTimetable(createTimetableRequest: CreateTimetableRequest, userId: Long): BaseResponse<Unit> {

checkRole(createTimetableRequest.workspaceId, userId)
val timetableEntity = timetableMapper.requestToEntity(createTimetableRequest)

timetableRepository.save(timetableEntity)

return BaseResponse(
message = "시간표 저장 성공!"
)

}

@Transactional
override fun fixTimetable(userId: Long, fixTimetableRequest: FixTimetableRequest): BaseResponse<Unit> {
val timetable = timetableRepository.findById(fixTimetableRequest.id)
.orElseThrow { CustomException(TimetableException.NOT_FOUND) }

checkRole(timetable.workspaceId, userId)

timetable.updateSubject(fixTimetableRequest.subject)

timetableRepository.save(timetable)

return BaseResponse(
message = "시간표 수정 성공!"
)

}

@Transactional
override fun deleteTimetable(userId: Long, id: Long): BaseResponse<Unit> {
val timetable = timetableRepository.findById(id).orElseThrow { CustomException(TimetableException.NOT_FOUND) }

checkRole(timetable.workspaceId, userId)

timetableRepository.delete(timetable)

return BaseResponse(
message = "시간표 삭제 성공!"
)

}

@Transactional
override fun resetTimetable(workspaceId: String, userId: Long) {
val workspace = workspaceService.findWorkspaceById(workspaceId)
Expand Down

0 comments on commit 65d1bba

Please sign in to comment.