Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(apply): refactor controller #570

Closed
Closed
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
79a2238
refactor(Term): change response of create api
summerlunaa Jul 16, 2022
7e48dac
refactor(Recruitment): change response of create api
summerlunaa Jul 16, 2022
7ad2dd3
refactor(Mission): change response of create api
summerlunaa Jul 16, 2022
822824a
refactor(Evaluation): change response of create api
summerlunaa Jul 16, 2022
c1174ad
refactor(Cheater): change response of create api
summerlunaa Jul 16, 2022
244653e
refactor(Assignment): change response of create api
summerlunaa Jul 16, 2022
4cf81ae
refactor(Mail): change response of create api
summerlunaa Jul 16, 2022
33b545f
refactor: change variable name
summerlunaa Jul 16, 2022
7142a04
refactor: change variable name
summerlunaa Jul 16, 2022
2956f78
feat(Mission): getById api
summerlunaa Jul 16, 2022
c5f8d95
feat(Cheater): getById api
summerlunaa Jul 16, 2022
228c989
refactor(Term): add administrator authorization
summerlunaa Jul 16, 2022
4c320b9
refactor(Term): add entity to response of create api
summerlunaa Jul 18, 2022
2605e0c
refactor(Recruitment): add entity to response of create api
summerlunaa Jul 18, 2022
e4f4836
refactor(Term): change response type of create api
summerlunaa Jul 18, 2022
c509827
refactor(Mission): add entity to response of create api
summerlunaa Jul 18, 2022
ee60c5c
refactor(Evaluation): add entity to response of create api
summerlunaa Jul 18, 2022
2ef049d
refactor(Cheater): add entity to response of create api
summerlunaa Jul 19, 2022
b2ddcdf
refactor(MailHistory): add entity to response of create api
summerlunaa Jul 19, 2022
f171257
refactor(Assignment): add entity to response of create api
summerlunaa Jul 19, 2022
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
6 changes: 4 additions & 2 deletions src/main/kotlin/apply/application/AssignmentDtos.kt
Original file line number Diff line number Diff line change
@@ -27,12 +27,14 @@ data class AssignmentRequest(
data class AssignmentData(
val githubUsername: String,
val pullRequestUrl: String,
val note: String
val note: String,
val id: Long? = 0
) {
constructor(assignment: Assignment?) : this(
assignment?.githubUsername.orEmpty(),
assignment?.pullRequestUrl.orEmpty(),
assignment?.note.orEmpty()
assignment?.note.orEmpty(),
assignment?.id
)
}

5 changes: 3 additions & 2 deletions src/main/kotlin/apply/application/AssignmentService.kt
Original file line number Diff line number Diff line change
@@ -17,14 +17,15 @@ class AssignmentService(
private val missionRepository: MissionRepository,
private val evaluationTargetRepository: EvaluationTargetRepository
) {
fun create(missionId: Long, userId: Long, request: AssignmentRequest) {
fun create(missionId: Long, userId: Long, request: AssignmentRequest): AssignmentData {
check(!assignmentRepository.existsByUserIdAndMissionId(userId, missionId)) { "이미 제출한 과제 제출물이 존재합니다." }
val mission = missionRepository.getById(missionId)
check(mission.isSubmitting) { "제출 불가능한 과제입니다." }
findEvaluationTargetOf(mission.evaluationId, userId).passIfBeforeEvaluation()
assignmentRepository.save(
val assignment = assignmentRepository.save(
Assignment(userId, missionId, request.githubUsername, request.pullRequestUrl, request.note)
)
return AssignmentData(assignment)
}

fun update(missionId: Long, userId: Long, request: AssignmentRequest) {
10 changes: 8 additions & 2 deletions src/main/kotlin/apply/application/CheaterService.kt
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package apply.application

import apply.domain.cheater.Cheater
import apply.domain.cheater.CheaterRepository
import apply.domain.cheater.getById
import apply.domain.user.UserRepository
import apply.domain.user.findByEmail
import org.springframework.stereotype.Service
@@ -18,12 +19,17 @@ class CheaterService(
CheaterResponse(it, user)
}

fun save(request: CheaterData) {
fun save(request: CheaterData): CheaterResponse {
val email = request.email
require(!cheaterRepository.existsByEmail(email)) {
"이미 등록된 부정 행위자입니다."
}
cheaterRepository.save(Cheater(email, request.description))
val cheater = cheaterRepository.save(Cheater(email, request.description))
return CheaterResponse(cheater, userRepository.findByEmail(cheater.email))
}

fun getById(id: Long): Cheater {
return cheaterRepository.getById(id)
}

fun deleteById(id: Long) {
4 changes: 3 additions & 1 deletion src/main/kotlin/apply/application/EvaluationService.kt
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ class EvaluationService(
private val evaluationItemRepository: EvaluationItemRepository,
private val recruitmentRepository: RecruitmentRepository
) {
fun save(request: EvaluationData) {
fun save(request: EvaluationData): EvaluationData {
val evaluation = evaluationRepository.save(
Evaluation(
request.title,
@@ -34,6 +34,8 @@ class EvaluationService(
EvaluationItem(it.title, it.description, it.maximumScore, it.position, evaluation.id, it.id)
}
)
request.id = evaluation.id
return request
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

매개변수로 받은 request 에 저장하면서 생긴 evaluation.id 를 set 해주고 반환하는데 혹시 헷갈릴 수도 있지않을까요?

다른 서비스 클래스에서 save 메서드를 고쳤을 때는 새로운 data 객체를 생성했는데 이것만 안그런거 같아서 다시한번 보게 되네요!

}

private fun findEvaluationItemsToDelete(evaluationId: Long, excludedItemIds: List<Long>): List<EvaluationItem> {
5 changes: 3 additions & 2 deletions src/main/kotlin/apply/application/MailHistoryService.kt
Original file line number Diff line number Diff line change
@@ -12,8 +12,8 @@ import javax.transaction.Transactional
class MailHistoryService(
private val mailHistoryRepository: MailHistoryRepository
) {
fun save(request: MailData) {
mailHistoryRepository.save(
fun save(request: MailData): MailData {
val mailHistory = mailHistoryRepository.save(
MailHistory(
request.subject,
request.body,
@@ -22,6 +22,7 @@ class MailHistoryService(
request.sentTime
)
)
return MailData(mailHistory)
}

fun findAll(): List<MailData> {
10 changes: 8 additions & 2 deletions src/main/kotlin/apply/application/MissionService.kt
Original file line number Diff line number Diff line change
@@ -18,9 +18,9 @@ class MissionService(
private val evaluationTargetRepository: EvaluationTargetRepository,
private val assignmentRepository: AssignmentRepository
) {
fun save(request: MissionData) {
fun save(request: MissionData): MissionData {
validate(request)
missionRepository.save(
val mission = missionRepository.save(
request.let {
Mission(
it.title,
@@ -34,6 +34,8 @@ class MissionService(
)
}
)
request.id = mission.id
return request
}

private fun validate(request: MissionData) {
@@ -55,6 +57,10 @@ class MissionService(
return request.id == 0L || !missionRepository.existsById(request.id)
}

fun getById(id: Long): Mission {
return missionRepository.getById(id)
}

fun getDataById(id: Long): MissionData {
val mission = missionRepository.getById(id)
val evaluation = evaluationRepository.getById(mission.evaluationId)
4 changes: 3 additions & 1 deletion src/main/kotlin/apply/application/RecruitmentService.kt
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ class RecruitmentService(
private val recruitmentItemRepository: RecruitmentItemRepository,
private val termRepository: TermRepository
) {
fun save(request: RecruitmentData) {
fun save(request: RecruitmentData): RecruitmentData {
val recruitment = recruitmentRepository.save(
Recruitment(
request.title,
@@ -37,6 +37,8 @@ class RecruitmentService(
RecruitmentItem(recruitment.id, it.title, it.position, it.maximumLength, it.description, it.id)
}
)
request.id = recruitment.id
return request
}

private fun findRecruitmentItemsToDelete(recruitmentId: Long, excludedItemIds: List<Long>): List<RecruitmentItem> {
10 changes: 8 additions & 2 deletions src/main/kotlin/apply/application/TermService.kt
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ package apply.application
import apply.domain.recruitment.RecruitmentRepository
import apply.domain.term.Term
import apply.domain.term.TermRepository
import apply.domain.term.getById
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@@ -12,10 +13,15 @@ class TermService(
private val termRepository: TermRepository,
private val recruitmentRepository: RecruitmentRepository
) {
fun save(request: TermData) {
fun save(request: TermData): TermData {
check(request.name != Term.SINGLE.name) { "기수명은 ${Term.SINGLE.name}일 수 없습니다." }
check(!termRepository.existsByName(request.name)) { "이미 등록된 기수명입니다." }
termRepository.save(request.toEntity())
request.id = termRepository.save(request.toEntity()).id
return request
}

fun getById(id: Long): Term {
return termRepository.getById(id)
}

fun findAll(): List<TermResponse> {
4 changes: 4 additions & 0 deletions src/main/kotlin/apply/domain/cheater/CheaterRepository.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package apply.domain.cheater

import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.repository.findByIdOrNull

fun CheaterRepository.getById(id: Long): Cheater = findByIdOrNull(id)
?: throw NoSuchElementException("부정행위자가 존재하지 않습니다. id: $id")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리소스가 존재하지 않는 예외를 repository 에서 발생시키도록 구현했네요!
저도 이렇게 하는게 좋은것 같습니다! :)


interface CheaterRepository : JpaRepository<Cheater, Long> {
fun existsByEmail(email: String): Boolean
9 changes: 6 additions & 3 deletions src/main/kotlin/apply/ui/api/AssignmentRestController.kt
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.net.URI
import javax.validation.Valid

@RestController
@@ -27,9 +28,11 @@ class AssignmentRestController(
@PathVariable missionId: Long,
@RequestBody @Valid request: AssignmentRequest,
@LoginUser user: User
): ResponseEntity<Unit> {
assignmentService.create(missionId, user.id, request)
return ResponseEntity.ok().build()
): ResponseEntity<ApiResponse<AssignmentData>> {
val assignment = assignmentService.create(missionId, user.id, request)
return ResponseEntity
.created(URI.create("/api/recruitments/$recruitmentId/missions/$missionId/assignments/${assignment.id}"))
.body(ApiResponse.success(assignment))
}

@GetMapping("/missions/{missionId}/assignments/me")
18 changes: 15 additions & 3 deletions src/main/kotlin/apply/ui/api/CheaterRestController.kt
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ package apply.ui.api
import apply.application.CheaterData
import apply.application.CheaterResponse
import apply.application.CheaterService
import apply.domain.cheater.Cheater
import apply.domain.user.User
import apply.security.LoginUser
import org.springframework.http.ResponseEntity
@@ -13,6 +14,7 @@ import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.net.URI

@RestController
@RequestMapping("/api/cheaters")
@@ -31,9 +33,19 @@ class CheaterRestController(
fun save(
@RequestBody request: CheaterData,
@LoginUser(administrator = true) user: User
): ResponseEntity<Unit> {
cheaterService.save(request)
return ResponseEntity.ok().build()
): ResponseEntity<ApiResponse<CheaterResponse>> {
val cheaterResponse = cheaterService.save(request)
return ResponseEntity.created(URI.create("/api/cheaters/${cheaterResponse.id}"))
.body(ApiResponse.success(cheaterResponse))
}

@GetMapping("/{cheaterId}")
fun findById(
@PathVariable cheaterId: Long,
@LoginUser(administrator = true) user: User
): ResponseEntity<ApiResponse<Cheater>> {
val cheater = cheaterService.getById(cheaterId)
return ResponseEntity.ok(ApiResponse.success(cheater))
}

@DeleteMapping("/{cheaterId}")
8 changes: 5 additions & 3 deletions src/main/kotlin/apply/ui/api/EvaluationRestController.kt
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.net.URI

@RestController
@RequestMapping("/api/recruitments/{recruitmentId}/evaluations")
@@ -24,9 +25,10 @@ class EvaluationRestController(
@PathVariable recruitmentId: Long,
@RequestBody evaluationData: EvaluationData,
@LoginUser(administrator = true) user: User
): ResponseEntity<Unit> {
evaluationService.save(evaluationData)
return ResponseEntity.ok().build()
): ResponseEntity<ApiResponse<EvaluationData>> {
val evaluation = evaluationService.save(evaluationData)
return ResponseEntity.created(URI.create("/api/recruitments/$recruitmentId/evaluations/${evaluation.id}"))
.body(ApiResponse.success(evaluation))
}

@GetMapping("/{evaluationId}")
8 changes: 5 additions & 3 deletions src/main/kotlin/apply/ui/api/MailHistoryRestController.kt
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.net.URI
import javax.validation.Valid

@RestController
@@ -22,10 +23,11 @@ class MailHistoryRestController(
fun save(
@RequestBody @Valid request: MailData,
@LoginUser(administrator = true) user: User
): ResponseEntity<Unit> {
): ResponseEntity<ApiResponse<MailData>> {
// todo: 파일 첨부하여 보내는 로직 필요
mailHistoryService.save(request)
return ResponseEntity.ok().build()
val mailData = mailHistoryService.save(request)
return ResponseEntity.created(URI.create("/api/mail-history/${mailData.id}"))
.body(ApiResponse.success(mailData))
}

@GetMapping("/{mailHistoryId}")
19 changes: 16 additions & 3 deletions src/main/kotlin/apply/ui/api/MissionRestController.kt
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import apply.application.MissionAndEvaluationResponse
import apply.application.MissionData
import apply.application.MissionResponse
import apply.application.MissionService
import apply.domain.mission.Mission
import apply.domain.user.User
import apply.security.LoginUser
import org.springframework.http.ResponseEntity
@@ -14,6 +15,7 @@ import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.net.URI

@RestController
@RequestMapping("/api/recruitments/{recruitmentId}")
@@ -25,9 +27,20 @@ class MissionRestController(
@PathVariable recruitmentId: Long,
@RequestBody missionData: MissionData,
@LoginUser(administrator = true) user: User
): ResponseEntity<Unit> {
missionService.save(missionData)
return ResponseEntity.ok().build()
): ResponseEntity<ApiResponse<MissionData>> {
val mission = missionService.save(missionData)
return ResponseEntity.created(URI.create("/api/recruitments/$recruitmentId/missions/${mission.id}"))
.body(ApiResponse.success(mission))
}

@GetMapping("/missions/{missionId}")
fun getById(
@PathVariable recruitmentId: Long,
@PathVariable missionId: Long,
@LoginUser(administrator = true) user: User
): ResponseEntity<ApiResponse<Mission>> {
val mission = missionService.getById(missionId)
return ResponseEntity.ok(ApiResponse.success(mission))
}

@GetMapping("/missions")
8 changes: 5 additions & 3 deletions src/main/kotlin/apply/ui/api/RecruitmentRestController.kt
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.net.URI

@RestController
@RequestMapping("/api/recruitments")
@@ -47,9 +48,10 @@ class RecruitmentRestController(
fun save(
@RequestBody request: RecruitmentData,
@LoginUser(administrator = true) user: User
): ResponseEntity<Unit> {
recruitmentService.save(request)
return ResponseEntity.ok().build()
): ResponseEntity<ApiResponse<RecruitmentData>> {
val recruitmentData = recruitmentService.save(request)
return ResponseEntity.created(URI.create("/api/recruitments/${recruitmentData.id}"))
.body(ApiResponse.success(recruitmentData))
}

@DeleteMapping("/{recruitmentId}")
Loading