Skip to content

Commit

Permalink
[KAN-38] 유저 조회 API 개발 (#23)
Browse files Browse the repository at this point in the history
* [KAN-34] 유저 - 이메일인증 메일 전송 API 개발

* [KAN-34] 유저 - 이메일인증 메일 전송 API 개발 (패스워드 재설정 기능 추가)

* [KAN-34] 유저 - 이메일인증 메일 전송 API 개발 (패스워드 재설정 기능 추가)

* [KAN-34] 유저 - 이메일인증 메일 전송 API 개발 (네이밍 변경

* [KAN-34] 유저 - 이메일인증 메일 전송 API 개발 (네이밍 변경

* [KAN-34] 유저 - 이메일인증 메일 전송 API 개발 (네이밍 변경

* [KAN-34] 유저 - 이메일인증 메일 전송 API 개발 (네이밍 변경 (ktlint)

* [KAN-36] 유저 - 패스워드 업데이트 API 개발

* [KAN-38] 유저 조회 API 개발

* [KAN-38] 유저 조회 API 개발 - conflict
  • Loading branch information
sinkyoungdeok authored May 8, 2024
1 parent cbf29a4 commit 29440b0
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ data class InvalidUserResetPasswordStateException(
data class NotEqualTokenException(
override val message: String = "토큰이 일치 하지 않습니다."
) : ServerException(400, message)

data class NotFoundUserException(
override val message: String = "존재 하지 않는 유저 입니다."
) : ServerException(400, message)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.restaurant.be.user.domain.service

import com.restaurant.be.common.exception.NotFoundUserException
import com.restaurant.be.user.presentation.dto.GetUserResponse
import com.restaurant.be.user.repository.UserRepository
import org.springframework.stereotype.Service

@Service
class GetUserService(
private val userRepository: UserRepository
) {

fun getUser(userId: Long): GetUserResponse {
val user = userRepository.findById(userId).orElseThrow {
NotFoundUserException()
}

return GetUserResponse(user = user)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.restaurant.be.user.domain.service

import com.restaurant.be.common.exception.InvalidEmailCodeException
import com.restaurant.be.common.exception.SendEmailException
import com.restaurant.be.common.exception.SkkuEmailException
import com.restaurant.be.common.redis.RedisRepository
import com.restaurant.be.user.presentation.dto.SendEmailRequest
import com.restaurant.be.user.presentation.dto.ValidateEmailRequest
import com.restaurant.be.user.presentation.dto.ValidateEmailResponse
import com.restaurant.be.user.presentation.dto.common.EmailSendType
import com.restaurant.be.user.repository.EmailRepository
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import java.util.*
import java.util.concurrent.TimeUnit

@Service
Expand All @@ -29,7 +33,6 @@ class ValidateEmailService(
if (request.email.split("@")[1] != "g.skku.edu") {
throw SkkuEmailException()
}

try {
val code = emailRepository.generateRandomCode()
val email = request.email
Expand All @@ -51,7 +54,12 @@ class ValidateEmailService(
}
}.build()

redisRepository.setValue("user:$email:signUpCode", code, 3, TimeUnit.MINUTES)
redisRepository.setValue(
"user:$email:${request.sendType.name.lowercase(Locale.getDefault())}_code",
code,
3,
TimeUnit.MINUTES
)
emailRepository.sendEmail(message)
} else {
val message = software.amazon.awssdk.services.ses.model.SendEmailRequest
Expand All @@ -71,11 +79,37 @@ class ValidateEmailService(
}
}.build()

redisRepository.setValue("user:$email:resetPasswordCode", code, 3, TimeUnit.MINUTES)
redisRepository.setValue(
"user:$email:${request.sendType.name.lowercase(Locale.getDefault())}_code",
code,
3,
TimeUnit.MINUTES
)
emailRepository.sendEmail(message)
}
} catch (e: Exception) {
throw SendEmailException()
}
}

fun validateEmail(request: ValidateEmailRequest): ValidateEmailResponse {
if (!isValidCode(request.email, request.code, request.sendType)) {
throw InvalidEmailCodeException()
}

val token = (UUID.randomUUID().toString() + UUID.randomUUID().toString()).replace("-", "")
redisRepository.setValue(
"user:${request.email}:${request.sendType.name.lowercase(Locale.getDefault())}_token",
token,
3,
TimeUnit.MINUTES
)

return ValidateEmailResponse(token)
}

private fun isValidCode(email: String, code: String, sendType: EmailSendType): Boolean {
val key = "user:$email:${sendType.name.lowercase(Locale.getDefault())}_code"
return redisRepository.getValue(key) == code
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.restaurant.be.user.presentation.controller

import com.restaurant.be.common.response.CommonResponse
import com.restaurant.be.user.domain.service.GetUserService
import com.restaurant.be.user.presentation.dto.GetUserResponse
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
Expand All @@ -16,7 +17,9 @@ import org.springframework.web.bind.annotation.RestController
@Api(tags = ["01. User Info"], description = "유저 서비스")
@RestController
@RequestMapping("/api/v1/users")
class GetUserController {
class GetUserController(
private val getUserService: GetUserService
) {

@GetMapping("/{userId}")
@PreAuthorize("hasRole('USER')")
Expand All @@ -26,7 +29,8 @@ class GetUserController {
description = "성공",
content = [Content(schema = Schema(implementation = GetUserResponse::class))]
)
fun getUser(@PathVariable userId: String): CommonResponse<GetUserResponse> {
return CommonResponse.success()
fun getUser(@PathVariable userId: Long): CommonResponse<GetUserResponse> {
val response = getUserService.getUser(userId)
return CommonResponse.success(response)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

package com.restaurant.be.user.presentation.dto

import com.restaurant.be.user.domain.entity.User
import com.restaurant.be.user.presentation.dto.common.UserDto
import io.swagger.v3.oas.annotations.media.Schema

data class GetUserResponse(
@Schema(description = "유저 정보")
val userDto: UserDto
)
) {
constructor(user: User) : this(
userDto = UserDto(
id = user.id ?: 0,
email = user.email,
nickname = user.nickname,
profileImageUrl = user.profileImageUrl
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ data class SignInUserResponse(
constructor(user: User, token: Token) : this(
userDto = UserDto(
email = user.email,
nickname = user.nickname
nickname = user.nickname,
profileImageUrl = user.profileImageUrl
),
token = token
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ data class UserDto(
val id: Long = 0,

@ApiModelProperty(value = "이메일 아이디", example = "[email protected]", required = true)
val email: String = "",
val email: String,

@ApiModelProperty(value = "비밀번호", example = "test12!@", required = true)
val password: String = "",

@ApiModelProperty(value = "닉네임", example = "닉네임", required = true)
val nickname: String = "",
val nickname: String,

@ApiModelProperty(
value = "프로필 이미지 URL",
example = "https://test.com/test.jpg",
required = true
)
val profileImageUrl: String = ""
val profileImageUrl: String
)

0 comments on commit 29440b0

Please sign in to comment.