Skip to content

Commit

Permalink
Merge pull request #39 from GSM-MSG/34-feat/student-activity-informat…
Browse files Browse the repository at this point in the history
…ion-add

34 학생 활동 정보 추가
  • Loading branch information
KimTaeO authored Oct 20, 2023
2 parents 2a91dae + 9d02509 commit 292cfe5
Show file tree
Hide file tree
Showing 17 changed files with 221 additions and 3 deletions.
29 changes: 29 additions & 0 deletions bitgouel-api/src/main/kotlin/team/msg/common/util/UserUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package team.msg.common.util

import org.springframework.data.repository.findByIdOrNull
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.stereotype.Component
import team.msg.domain.user.exception.UserNotFoundException
import team.msg.domain.user.model.User
import team.msg.domain.user.repository.UserRepository
import team.msg.global.security.principal.AuthDetails
import java.util.*

@Component
class UserUtil(
private val userRepository: UserRepository
) {
fun queryCurrentUser(): User {
val principal = SecurityContextHolder.getContext().authentication.principal

val userId = UUID.fromString(if(principal is AuthDetails)
principal.username
else
principal.toString())

val user = userRepository.findByIdOrNull(userId)
?: throw UserNotFoundException("존재하지 않는 유저입니다. : [ id = $userId ]")

return user
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ class AuthServiceImpl(
*/
private fun queryClub(highSchool: HighSchool, clubName: String): Club {
val school = schoolRepository.findByHighSchool(highSchool)
?: throw SchoolNotFoundException("존재하지 않는 학교입니다. values : [ highSchool = $highSchool ]")
?: throw SchoolNotFoundException("존재하지 않는 학교입니다. info : [ highSchool = $highSchool ]")

val club = clubRepository.findByNameAndSchool(clubName, school)
?: throw ClubNotFoundException("존재하지 않는 동아리입니다. values : [ club = $clubName ]")
?: throw ClubNotFoundException("존재하지 않는 동아리입니다. info : [ club = $clubName ]")

return club
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package team.msg.domain.student.exception

import team.msg.domain.student.exception.constant.StudentErrorCode
import team.msg.global.error.exception.BitgouelException

class StudentNotFoundException(
message: String
) : BitgouelException(message, StudentErrorCode.STUDENT_NOT_FOUND.status)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package team.msg.domain.student.exception.constant

enum class StudentErrorCode(
val message: String,
val status: Int
) {
STUDENT_NOT_FOUND("학생을 찾을 수 없습니다.", 404)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package team.msg.domain.student.mapper

import team.msg.domain.student.presentation.data.request.CreateStudentActivityRequest
import team.msg.domain.student.presentation.data.web.CreateStudentActivityWebRequest

interface StudentActivityMapper {
fun createStudentActivityWebRequestToDto(webRequest: CreateStudentActivityWebRequest): CreateStudentActivityRequest
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package team.msg.domain.student.mapper

import org.springframework.stereotype.Component
import team.msg.domain.student.presentation.data.request.CreateStudentActivityRequest
import team.msg.domain.student.presentation.data.web.CreateStudentActivityWebRequest

@Component
class StudentActivityMapperImpl : StudentActivityMapper {

/**
* StudentActivity 생성 Web Request 를 애플리케이션 영역에서 사용될 Dto 로 매핑합니다.
*/
override fun createStudentActivityWebRequestToDto(webRequest: CreateStudentActivityWebRequest): CreateStudentActivityRequest =
CreateStudentActivityRequest(
title = webRequest.title,
content = webRequest.content,
credit = webRequest.credit,
activityDate = webRequest.activityDate
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package team.msg.domain.student.presentation

import javax.validation.Valid
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
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 team.msg.domain.student.mapper.StudentActivityMapper
import team.msg.domain.student.presentation.data.web.CreateStudentActivityWebRequest
import team.msg.domain.student.service.StudentActivityService

@RestController
@RequestMapping("/activity")
class StudentActivityController(
private val studentActivityService: StudentActivityService,
private val studentActivityMapper: StudentActivityMapper
) {
@PostMapping
fun createStudentActivity(@RequestBody @Valid request: CreateStudentActivityWebRequest): ResponseEntity<Void> {
studentActivityService.createStudentActivity(studentActivityMapper.createStudentActivityWebRequestToDto(request))
return ResponseEntity.status(HttpStatus.CREATED).build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package team.msg.domain.student.presentation.data.request

import java.time.LocalDateTime

data class CreateStudentActivityRequest(
val title: String,
val content: String,
val credit: Int,
val activityDate: LocalDateTime
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package team.msg.domain.student.presentation.data.web

import javax.validation.constraints.Max
import javax.validation.constraints.NotBlank
import javax.validation.constraints.NotNull
import java.time.LocalDateTime

data class CreateStudentActivityWebRequest(
@field:NotBlank
@field:Max(100)
val title: String,

@field:NotBlank
@field:Max(1000)
val content: String,

@field:NotNull
val credit: Int,

@field:NotNull
val activityDate: LocalDateTime
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package team.msg.domain.student.service

import team.msg.domain.student.presentation.data.request.CreateStudentActivityRequest

interface StudentActivityService {
fun createStudentActivity(request: CreateStudentActivityRequest)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package team.msg.domain.student.service

import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import team.msg.common.enum.ApproveStatus
import team.msg.common.util.UserUtil
import team.msg.domain.student.exception.StudentNotFoundException
import team.msg.domain.student.model.StudentActivity
import team.msg.domain.student.presentation.data.request.CreateStudentActivityRequest
import team.msg.domain.student.repository.StudentActivityRepository
import team.msg.domain.student.repository.StudentRepository
import team.msg.domain.teacher.exception.TeacherNotFoundException
import team.msg.domain.teacher.repository.TeacherRepository
import java.util.*

@Service
class StudentActivityServiceImpl(
private val userUtil: UserUtil,
private val studentRepository: StudentRepository,
private val teacherRepository: TeacherRepository,
private val studentActivityRepository: StudentActivityRepository
) : StudentActivityService {

/**
* 학생 활동을 생성하는 비지니스 로직입니다
* @param CreateStudentActivityRequest
*/
@Transactional(rollbackFor = [Exception::class])
override fun createStudentActivity(request: CreateStudentActivityRequest) {
val user = userUtil.queryCurrentUser()

val student = studentRepository.findByUser(user)
?: throw StudentNotFoundException("학생을 찾을 수 없습니다. info : [ name = ${user.name} ]")

val teacher = teacherRepository.findByClub(student.club)
?: throw TeacherNotFoundException("취업 동아리 선생님을 찾을 수 없습니다.")

val studentActivity = StudentActivity(
id = UUID.randomUUID(),
title = request.title,
content = request.content,
credit = request.credit,
activityDate = request.activityDate,
student = student,
teacher = teacher,
approveStatus = ApproveStatus.PENDING
)

studentActivityRepository.save(studentActivity)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package team.msg.domain.teacher.exception

import team.msg.domain.teacher.exception.constant.TeacherErrorCode
import team.msg.global.error.exception.BitgouelException

class TeacherNotFoundException(
message: String
) : BitgouelException(message, TeacherErrorCode.TEACHER_NOT_FOUND.status) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package team.msg.domain.teacher.exception.constant

enum class TeacherErrorCode(
val message: String,
val status: Int
) {
TEACHER_NOT_FOUND("취업 동아리 선생님을 찾을 수 없습니다.", 404)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import org.springframework.web.cors.CorsUtils
class SecurityConfig(
private val jwtTokenParser: JwtTokenParser
) {
companion object {
const val ADMIN = "ADMIN"
const val STUDENT = "STUDENT"
}

@Bean
protected fun filterChain(http: HttpSecurity): SecurityFilterChain =
http
Expand All @@ -42,6 +47,9 @@ class SecurityConfig(
.mvcMatchers(HttpMethod.POST, "/auth/login").permitAll()
.mvcMatchers(HttpMethod.PATCH, "/auth").permitAll()

// activity
.mvcMatchers(HttpMethod.POST, "/activity").hasRole(STUDENT)

.anyRequest().authenticated()
.and()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class StudentActivity(
@Column(columnDefinition = "VARCHAR(10)", nullable = false)
val approveStatus: ApproveStatus,

override val createdAt: LocalDateTime,
@Column(nullable = false, updatable = false, columnDefinition = "DATETIME(6)")
val activityDate: LocalDateTime,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "student_id", columnDefinition = "BINARY(16)", nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package team.msg.domain.student.repository

import org.springframework.data.repository.CrudRepository
import team.msg.domain.student.model.Student
import team.msg.domain.user.model.User
import java.util.UUID

interface StudentRepository : CrudRepository<Student, UUID> {
fun findByUser(user: User): Student?
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package team.msg.domain.teacher.repository

import org.springframework.data.repository.CrudRepository
import team.msg.domain.club.model.Club
import team.msg.domain.teacher.model.Teacher
import java.util.UUID

interface TeacherRepository : CrudRepository<Teacher, UUID> {
fun findByClub(club: Club): Teacher?
}

0 comments on commit 292cfe5

Please sign in to comment.