Skip to content

Commit

Permalink
Merge pull request #517 from School-of-Company/514-update/university-…
Browse files Browse the repository at this point in the history
…requirements-reflection

🔀 :: 514 대학 요구사항 변경 반영
  • Loading branch information
KimTaeO authored Jul 10, 2024
2 parents 28f271f + 7087249 commit 7690f45
Show file tree
Hide file tree
Showing 16 changed files with 254 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package team.msg.domain.university.mapper

import team.msg.domain.university.presentation.data.request.CreateDepartmentRequest
import team.msg.domain.university.presentation.data.request.CreateUniversityRequest
import team.msg.domain.university.presentation.data.request.UpdateUniversityRequest
import team.msg.domain.university.presentation.data.web.CreateDepartmentWebRequest
import team.msg.domain.university.presentation.data.web.CreateUniversityWebRequest
import team.msg.domain.university.presentation.data.web.UpdateUniversityWebRequest

interface UniversityRequestMapper {
fun createUniversityWebRequestToDto(webRequest: CreateUniversityWebRequest): CreateUniversityRequest
fun updateUniversityWebRequestToDto(webRequest: UpdateUniversityWebRequest): UpdateUniversityRequest
fun createDepartmentWebRequestToDto(webRequest: CreateDepartmentWebRequest): CreateDepartmentRequest
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
package team.msg.domain.university.mapper

import org.springframework.stereotype.Component
import team.msg.domain.university.presentation.data.request.CreateDepartmentRequest
import team.msg.domain.university.presentation.data.request.CreateUniversityRequest
import team.msg.domain.university.presentation.data.request.UpdateUniversityRequest
import team.msg.domain.university.presentation.data.web.CreateDepartmentWebRequest
import team.msg.domain.university.presentation.data.web.CreateUniversityWebRequest
import team.msg.domain.university.presentation.data.web.UpdateUniversityWebRequest

@Component
class UniversityRequestRequestMapperImpl : UniversityRequestMapper {

override fun createUniversityWebRequestToDto(webRequest: CreateUniversityWebRequest) : CreateUniversityRequest =
CreateUniversityRequest(
department = webRequest.department,
universityName = webRequest.universityName
)

override fun updateUniversityWebRequestToDto(webRequest: UpdateUniversityWebRequest): UpdateUniversityRequest =
UpdateUniversityRequest(
universityName = webRequest.universityName
)

override fun createDepartmentWebRequestToDto(webRequest: CreateDepartmentWebRequest): CreateDepartmentRequest =
CreateDepartmentRequest(
department = webRequest.department
)
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package team.msg.domain.university.presentation

import javax.validation.Valid
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PatchMapping
import org.springframework.web.bind.annotation.PathVariable
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.RequestParam
import org.springframework.web.bind.annotation.RestController
import team.msg.domain.university.mapper.UniversityRequestMapper
import team.msg.domain.university.presentation.data.response.UniversitiesResponse
import team.msg.domain.university.presentation.data.web.CreateDepartmentWebRequest
import team.msg.domain.university.presentation.data.web.CreateUniversityWebRequest
import team.msg.domain.university.presentation.data.web.UpdateUniversityWebRequest
import team.msg.domain.university.service.UniversityService

@RestController
Expand All @@ -22,12 +27,19 @@ class UniversityController(
) {

@PostMapping
fun createUniversity(@RequestBody webRequest: CreateUniversityWebRequest): ResponseEntity<Unit> {
fun createUniversity(@RequestBody @Valid webRequest: CreateUniversityWebRequest): ResponseEntity<Unit> {
val request = universityRequestMapper.createUniversityWebRequestToDto(webRequest)
universityService.createUniversity(request)
return ResponseEntity.status(HttpStatus.CREATED).build()
}

@PatchMapping("/{id}")
fun updateUniversity(@PathVariable id: Long, @RequestBody @Valid webRequest: UpdateUniversityWebRequest): ResponseEntity<Unit> {
val request = universityRequestMapper.updateUniversityWebRequestToDto(webRequest)
universityService.updateUniversity(id, request)
return ResponseEntity.noContent().build()
}

@DeleteMapping("/{id}")
fun deleteUniversity(@PathVariable id: Long): ResponseEntity<Unit> {
universityService.deleteUniversity(id)
Expand All @@ -39,4 +51,17 @@ class UniversityController(
val response = universityService.queryUniversities()
return ResponseEntity.ok(response)
}

@PostMapping("/department/{id}")
fun createDepartment(@PathVariable id: Long, @RequestBody @Valid webRequest: CreateDepartmentWebRequest): ResponseEntity<Unit> {
val request = universityRequestMapper.createDepartmentWebRequestToDto(webRequest)
universityService.createDepartment(id, request)
return ResponseEntity.status(HttpStatus.CREATED).build()
}

@DeleteMapping("/department/{id}")
fun deleteDepartment(@PathVariable id: Long, @RequestParam department: String): ResponseEntity<Unit> {
universityService.deleteDepartment(id, department)
return ResponseEntity.noContent().build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package team.msg.domain.university.presentation.data.request

data class CreateDepartmentRequest(
val department: String
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package team.msg.domain.university.presentation.data.request

data class CreateUniversityRequest(
val department: String,
val universityName: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package team.msg.domain.university.presentation.data.request

data class UpdateUniversityRequest(
val universityName: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import team.msg.domain.university.model.University

data class UniversityResponse(
val id: Long,
val department: String,
val universityName: String
val universityName: String,
val departments: List<String>
) {
companion object {

fun of(university: University) =
UniversityResponse(
id = university.id,
department = university.department,
departments = university.departments,
universityName = university.name
)


fun listOf(universities: List<University>) =
universities.map { of(it) }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package team.msg.domain.university.presentation.data.web

import javax.validation.constraints.NotBlank

data class CreateDepartmentWebRequest(
@field:NotBlank
val department: String
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package team.msg.domain.university.presentation.data.web

import javax.validation.constraints.NotBlank

data class CreateUniversityWebRequest(
val department: String,
@field:NotBlank
val universityName: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package team.msg.domain.university.presentation.data.web

import javax.validation.constraints.NotBlank

data class UpdateUniversityWebRequest(
@field:NotBlank
val universityName: String
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package team.msg.domain.university.service

import team.msg.domain.university.presentation.data.response.UniversitiesResponse
import team.msg.domain.university.presentation.data.request.CreateDepartmentRequest
import team.msg.domain.university.presentation.data.request.CreateUniversityRequest
import java.util.*
import team.msg.domain.university.presentation.data.request.UpdateUniversityRequest
import team.msg.domain.university.presentation.data.response.UniversitiesResponse

interface UniversityService {
fun createUniversity(request: CreateUniversityRequest)
fun updateUniversity(id: Long, request: UpdateUniversityRequest)
fun deleteUniversity(id: Long)
fun queryUniversities(): UniversitiesResponse
fun createDepartment(id: Long, request: CreateDepartmentRequest)
fun deleteDepartment(id: Long, department: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import team.msg.domain.university.exception.AlreadyExistUniversityException
import team.msg.domain.university.exception.UniversityHasProfessorConstraintException
import team.msg.domain.university.exception.UniversityNotFoundException
import team.msg.domain.university.model.University
import team.msg.domain.university.presentation.data.request.CreateDepartmentRequest
import team.msg.domain.university.presentation.data.request.CreateUniversityRequest
import team.msg.domain.university.presentation.data.request.UpdateUniversityRequest
import team.msg.domain.university.presentation.data.response.UniversitiesResponse
import team.msg.domain.university.presentation.data.response.UniversityResponse
import team.msg.domain.university.repository.ProfessorRepository
Expand All @@ -31,15 +33,39 @@ class UniversityServiceImpl(
throw AlreadyExistUniversityException("이미 존재하는 대학입니다. info : [ universityName = ${request.universityName} ]")

val university = University(
department = request.department,
name = request.universityName
)

universityRepository.save(university)
}

/**
* 대학을 삭제하는 비지니스로직입니다.
* 대학을 수정하는 비지니스 로직입니다.
*
* @param id 수정할 대학의 식별자
* @param request 수정할 대학의 정보
*/
@Transactional(rollbackFor = [Exception::class])
override fun updateUniversity(id: Long, request: UpdateUniversityRequest) {
if(universityRepository.existsByName(request.universityName))
throw AlreadyExistUniversityException("이미 존재하는 대학입니다. info : [ universityName = ${request.universityName} ]")

val university = universityRepository.findByIdOrNull(id)
?: throw UniversityNotFoundException("존재하지 않는 대학입니다. info : [ universityId = $id ]")

val updatedUniversity = University(
id = university.id,
name = request.universityName,
departments = university.departments
)

universityRepository.save(updatedUniversity)
}

/**
* 대학을 삭제하는 비지니스 로직입니다.
* 삭제할 대학과 연관된 대학교수 엔티티가 존재한다면 예외를 반환합나다.
*
* @param id 삭제할 대학의 id
*/
@Transactional(rollbackFor = [Exception::class])
Expand All @@ -55,8 +81,10 @@ class UniversityServiceImpl(

/**
* 대학 리스트를 반환하는 비지니스 로직입니다.
*
* @return 대학 리스트
*/
@Transactional(readOnly = true)
override fun queryUniversities(): UniversitiesResponse {
val universities = universityRepository.findAll()

Expand All @@ -66,4 +94,49 @@ class UniversityServiceImpl(

return response
}

/**
* 학과를 추가하는 비지니스 로직입니다.
*
* @param id 학과를 추가할 대학의 식별자
* @param request 추가할 학과의 정보
*/
@Transactional(rollbackFor = [Exception::class])
override fun createDepartment(id: Long, request: CreateDepartmentRequest) {
val university = universityRepository.findByIdOrNull(id)
?: throw UniversityNotFoundException("존재하지 않는 대학입니다. info : [ universityId = $id ]")

val departments = buildList {
addAll(university.departments)
add(request.department)
}

val updatedUniversity = University(
id = university.id,
name = university.name,
departments = departments
)

universityRepository.save(updatedUniversity)
}

/**
* 학과를 삭제하는 비지니스 로직입니다.
*
* @param id 학과를 삭제할 대학의 식별자
* @param request 삭제할 학과의 정보
*/
@Transactional(rollbackFor = [Exception::class])
override fun deleteDepartment(id: Long, department: String) {
val university = universityRepository.findByIdOrNull(id)
?: throw UniversityNotFoundException("존재하지 않는 대학입니다. info : [ universityId = $id ]")

val updateUniversity = University(
id = university.id,
name = university.name,
departments = university.departments - department
)

universityRepository.save(updateUniversity)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ class SecurityConfig(
// university
.mvcMatchers(HttpMethod.GET, "/university").permitAll()
.mvcMatchers(HttpMethod.POST, "/university").hasRole(ADMIN)
.mvcMatchers(HttpMethod.PATCH, "/university/{id}").hasRole(ADMIN)
.mvcMatchers(HttpMethod.DELETE, "/university/{id}").hasRole(ADMIN)
.mvcMatchers(HttpMethod.POST, "/university/department/{id}").hasRole(ADMIN)
.mvcMatchers(HttpMethod.DELETE, "/university/department/{id}").hasRole(ADMIN)

// actuator
.mvcMatchers(HttpMethod.GET, "/actuator/prometheus").permitAll()
Expand Down
Loading

0 comments on commit 7690f45

Please sign in to comment.