Skip to content

Commit

Permalink
Merge pull request #116 from GSM-MSG/115-accepted-user-list-api-not-f…
Browse files Browse the repository at this point in the history
…untion

회원가입 수락 유저 리스트 api
  • Loading branch information
KimTaeO authored Mar 27, 2023
2 parents f6f90b1 + 23008b6 commit 8d61bc2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.msg.gauth.domain.user.presentation.dto.response.MyProfileResDto
import com.msg.gauth.domain.user.presentation.dto.response.SingleAcceptedUserResDto
import com.msg.gauth.domain.user.presentation.dto.response.SinglePendingListResDto
import com.msg.gauth.domain.user.services.*
import org.springframework.data.domain.Pageable
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
Expand Down Expand Up @@ -42,8 +43,12 @@ class UserController(
}

@GetMapping("/user-list")
fun acceptedUserList(@RequestParam grade: Int, @RequestParam classNum: Int, @RequestParam keyword: String): ResponseEntity<List<SingleAcceptedUserResDto>> {
val result = acceptedUserService.execute(grade, classNum, keyword)
fun acceptedUserList(@RequestParam grade: Int,
@RequestParam classNum: Int,
@RequestParam keyword: String,
pageable: Pageable
): ResponseEntity<List<SingleAcceptedUserResDto>> {
val result = acceptedUserService.execute(grade, classNum, keyword, pageable)
return ResponseEntity.ok(result)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.msg.gauth.domain.user.presentation.dto.response

import com.msg.gauth.domain.user.User

data class SingleAcceptedUserResDto(
val id: Long,
val name: String,
Expand All @@ -8,4 +10,14 @@ data class SingleAcceptedUserResDto(
val classNum: Int,
val num: Int,
val profileUrl: String?
)
) {
constructor(user: User): this(
user.id,
user.name!!,
user.email,
user.grade!!,
user.classNum!!,
user.num!!,
user.profileUrl
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.msg.gauth.domain.user.repository
import com.msg.gauth.domain.user.User
import com.msg.gauth.domain.user.enums.UserRole
import com.msg.gauth.domain.user.enums.UserState
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.JpaSpecificationExecutor
import org.springframework.data.jpa.repository.Query
Expand All @@ -11,7 +12,17 @@ interface UserRepository: JpaRepository<User, Long>, JpaSpecificationExecutor<Us
fun findByEmail(email: String): User?
fun findByEmailIn(emailList: List<String>): List<User>
fun existsByEmail(email: String): Boolean
@Query("select user.email from User user")
fun findAllEmail(): List<String>
fun findAllByState(state: UserState): List<User>
fun findAllByStateOrderByGrade(state: UserState, pageable: Pageable): List<User>
fun findAllByStateAndNameContainingOrderByGrade(state: UserState, name: String, pageable: Pageable): List<User>
fun findAllByStateAndClassNumOrderByGrade(state: UserState, classNum: Int, pageable: Pageable): List<User>
fun findAllByStateAndGradeOrderByGrade(state: UserState, grade: Int, pageable: Pageable): List<User>
fun findAllByStateAndClassNumAndNameContainingOrderByGrade(state: UserState, classNum: Int, name: String, pageable: Pageable): List<User>
fun findAllByStateAndGradeAndNameContainingOrderByGrade(state: UserState, grade: Int, name: String, pageable: Pageable): List<User>
fun findAllByStateAndGradeAndClassNumOrderByGrade(state: UserState, grade: Int, classNum: Int, pageable: Pageable): List<User>
fun findAllByStateAndGradeAndClassNumAndNameContainingOrderByGrade(state: UserState, grade: Int, classNum: Int, name: String, pageable: Pageable): List<User>
@Query("select user from User user where user.id = :id and user.state = :state and user.roles = :roles")
fun findByIdAndStateAndRoles(id: Long, state: UserState, roles: MutableList<UserRole>): User?
}
Original file line number Diff line number Diff line change
@@ -1,60 +1,33 @@
package com.msg.gauth.domain.user.services

import com.msg.gauth.domain.user.User
import com.msg.gauth.domain.user.enums.UserRole
import com.msg.gauth.domain.user.enums.UserState
import com.msg.gauth.domain.user.presentation.dto.response.SingleAcceptedUserResDto
import com.msg.gauth.domain.user.repository.UserRepository
import com.msg.gauth.domain.user.specification.UserSpecification
import com.msg.gauth.global.annotation.service.ReadOnlyService
import org.springframework.data.jpa.domain.Specification
import javax.persistence.criteria.CriteriaBuilder
import javax.persistence.criteria.CriteriaQuery
import javax.persistence.criteria.Root
import org.springframework.data.domain.Pageable

@ReadOnlyService
class AcceptedUserService(
private val userRepository: UserRepository,
) {
fun execute(grade: Int, classNum: Int, keyword: String): List<SingleAcceptedUserResDto> {
fun execute(grade: Int, classNum: Int, name: String, pageable: Pageable): List<SingleAcceptedUserResDto> {
val userList: List<User>
= when {
grade == 0 && classNum == 0 && name == "" -> userRepository.findAllByStateOrderByGrade(UserState.CREATED, pageable)

val users: List<User>
= if(grade == 0 && classNum == 0 && keyword == "0")
listAcceptedUser()
else {
searchUser(grade, classNum, keyword)
}


return users.filter { user -> user.roles.equals(UserRole.ROLE_STUDENT) && user.state == UserState.CREATED }
.map { user ->
SingleAcceptedUserResDto(
user.id,
user.name!!,
user.email,
user.grade!!,
user.classNum!!,
user.num!!,
user.profileUrl
)
}

}
grade == 0 && classNum == 0 -> userRepository.findAllByStateAndNameContainingOrderByGrade(UserState.CREATED, name, pageable)
classNum == 0 && name == "" -> userRepository.findAllByStateAndGradeOrderByGrade(UserState.CREATED, classNum, pageable)
grade == 0 && name == ""-> userRepository.findAllByStateAndClassNumOrderByGrade(UserState.CREATED, grade, pageable)

private fun listAcceptedUser(): List<User> =
userRepository.findAllByState(UserState.CREATED)
grade == 0 -> userRepository.findAllByStateAndClassNumAndNameContainingOrderByGrade(UserState.CREATED, classNum, name, pageable)
classNum == 0 -> userRepository.findAllByStateAndGradeAndNameContainingOrderByGrade(UserState.CREATED, grade, name, pageable)
name == "" -> userRepository.findAllByStateAndGradeAndClassNumOrderByGrade(UserState.CREATED, grade, classNum, pageable)

private fun searchUser(grade: Int, classNum: Int, keyword: String): List<User> {
val spec: Specification<User> =
Specification { _: Root<User>?, _: CriteriaQuery<*>?, _: CriteriaBuilder? -> null }

if(grade != 0)
spec.and(UserSpecification.equalGrade(grade))
if(classNum != 0)
spec.and(UserSpecification.equalClassNum(classNum))
if(keyword != "0")
spec.and(UserSpecification.containKeyword(keyword))

return userRepository.findAll(spec)
else -> userRepository.findAllByStateAndGradeAndClassNumAndNameContainingOrderByGrade(UserState.CREATED, grade, classNum, name, pageable)
}
return userList.map {
SingleAcceptedUserResDto(it)
}
}
}

This file was deleted.

0 comments on commit 8d61bc2

Please sign in to comment.