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

34 학생 활동 정보 추가 #39

Merged
merged 27 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f652018
add :: create student activity web request
KimTaeO Oct 19, 2023
52257dc
add :: create student activity request
KimTaeO Oct 19, 2023
df5787b
add :: create student activity mapper
KimTaeO Oct 19, 2023
9d8ce9e
update :: studentactivity 코드 새로운 student package로 이동
KimTaeO Oct 19, 2023
f942e4b
add :: token의 UUID로 User 반환 공통로직
KimTaeO Oct 19, 2023
ba05e86
add :: student not found exception
KimTaeO Oct 19, 2023
4d442d4
add :: teacher not found exception
KimTaeO Oct 19, 2023
63b9cc4
add :: create student activity service
KimTaeO Oct 19, 2023
e1d6011
add :: create student activity service
KimTaeO Oct 19, 2023
adc66a4
add :: create student activity에 Transaction annotation
KimTaeO Oct 19, 2023
038243a
add :: student activity mapper에 Component annotation
KimTaeO Oct 19, 2023
ee075ed
add :: create student activity controller
KimTaeO Oct 19, 2023
8f4b8c4
Merge branch 'master' of https://github.com/GSM-MSG/Bitgouel-Server i…
KimTaeO Oct 19, 2023
945b867
add :: security config 경로 추가
KimTaeO Oct 19, 2023
4ad8b41
update :: response status CREATED로 변경
KimTaeO Oct 19, 2023
006ec32
add :: create student activity security Config 접근 권한 설정
KimTaeO Oct 19, 2023
075de5b
refactor :: 유저 검색 코드 내부 함수로 분리
KimTaeO Oct 19, 2023
3a1bbd5
refactor :: repository 함수 변수로 분리
KimTaeO Oct 20, 2023
222483d
update :: mapper 함수병 created 추가
KimTaeO Oct 20, 2023
2bc9df3
delete :: 필요없는 override 삭제
KimTaeO Oct 20, 2023
527f193
add :: activity date field
KimTaeO Oct 20, 2023
504f38c
update :: createdAt이 아닌 activityDate로 변경
KimTaeO Oct 20, 2023
e12035a
update :: error message에 정보 출력
KimTaeO Oct 20, 2023
1c779ea
delete :: ADMIN 권한 삭제
KimTaeO Oct 20, 2023
aacda2f
update :: 에러 메시지에 info 설명
KimTaeO Oct 20, 2023
81dd9d2
chore :: create student activity 주석
KimTaeO Oct 20, 2023
9d02509
Merge branch 'master' of https://github.com/GSM-MSG/Bitgouel-Server i…
KimTaeO Oct 20, 2023
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
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 @@ -193,10 +193,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,16 @@
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 {
override fun createStudentActivityWebRequestToDto(webRequest: CreateStudentActivityWebRequest): CreateStudentActivityRequest =
CreateStudentActivityRequest(
title = webRequest.title,
content = webRequest.content,
credit = webRequest.credit,
activityDate = webRequest.activityDate
)
KimTaeO marked this conversation as resolved.
Show resolved Hide resolved
}
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 @@ -40,6 +45,9 @@ class SecurityConfig(
.mvcMatchers(HttpMethod.POST, "/auth/government").permitAll()
.mvcMatchers(HttpMethod.POST, "/auth/company-instructor").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?
}