Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

Commit

Permalink
refactor: [#80] Admin Domain 코드 Kotlin으로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Habist committed Jul 20, 2021
1 parent 157cf2c commit 04c4484
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 163 deletions.
42 changes: 0 additions & 42 deletions src/main/java/com/campool/controller/AdminController.java

This file was deleted.

34 changes: 34 additions & 0 deletions src/main/java/com/campool/controller/AdminController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.campool.controller

import com.campool.annotation.LoginValidation
import com.campool.enumeration.Role
import com.campool.model.AdminLoginRequest
import com.campool.model.AdminSignUpRequest
import com.campool.service.AdminService
import com.campool.service.AuthService
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RestController
import javax.validation.Valid

@RestController
class AdminController(
private val adminService: AdminService,
private val authService: AuthService
) {
@PostMapping("/admins")
fun signUpAdmin(@Valid adminSignUp: AdminSignUpRequest) {
adminService.add(adminSignUp)
}

@PostMapping("/admins/login")
fun loginAdmin(@Valid request: AdminLoginRequest) {
authService.authenticate(request)
}

@LoginValidation(role = Role.ADMIN)
@GetMapping("/admins/logout")
fun logoutAdmin() {
authService.deauthenticate()
}
}
46 changes: 31 additions & 15 deletions src/main/java/com/campool/mapper/AdminMapper.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
package com.campool.mapper;
package com.campool.mapper

import com.campool.model.AdminSignUp;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.campool.model.AdminInfo
import com.campool.model.AdminSignUpRequest
import org.apache.ibatis.annotations.Insert
import org.apache.ibatis.annotations.Mapper
import org.apache.ibatis.annotations.Select

@Mapper
public interface AdminMapper {
interface AdminMapper {
@Insert(
"""
INSERT INTO ADMIN(id, password, name, email, telephone)
VALUES(#{id}, #{password}, #{name}, #{email}, #{telephone})
"""
)
fun insertAdmin(adminSignUpRequest: AdminSignUpRequest)

@Insert("INSERT INTO ADMIN(id, password, name, email, telephone) VALUES(#{id}, #{password}, #{name}, #{email}, #{telephone})")
void insertAdmin(AdminSignUp adminSignUp);
@Select(
"""
SELECT id, name, email, telephone
FROM ADMIN
WHERE id = #{id}
"""
)
fun findById(id: String): AdminInfo?

@Select("SELECT id, password, name, email, telephone FROM ADMIN WHERE id = #{id}")
AdminSignUp findById(String id);

@Select("SELECT id, password, name, email, telephone FROM ADMIN WHERE id = #{id} AND password = #{password}")
AdminSignUp findByIdAndPassword(String id, String password);

}
@Select(
"""
SELECT id, name, email, telephone
FROM ADMIN
WHERE id = #{id} AND password = #{password}
"""
)
fun findByIdAndPassword(id: String, password: String): AdminInfo?
}
8 changes: 8 additions & 0 deletions src/main/java/com/campool/model/AdminInfo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.campool.model

data class AdminInfo(
val id: String,
val name: String,
val email: String,
val telephone: String
)
21 changes: 8 additions & 13 deletions src/main/java/com/campool/model/AdminLoginRequest.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package com.campool.model;
package com.campool.model

import com.campool.enumeration.Role;
import com.campool.enumeration.Role

public class AdminLoginRequest extends LoginRequest {

public AdminLoginRequest(String id, String password) {
super(id, password);
}

@Override
public Role getRole() {
return Role.ADMIN;
}
}
class AdminLoginRequest(
id: String,
password: String
) : LoginRequest(id, password) {
override val role = Role.ADMIN
}
77 changes: 34 additions & 43 deletions src/main/java/com/campool/model/AdminSignUpRequest.kt
Original file line number Diff line number Diff line change
@@ -1,43 +1,34 @@
package com.campool.model;

import com.campool.encrypt.Encryptor;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

@Getter
@RequiredArgsConstructor
@ToString
public class AdminSignUp {

@NotBlank(message = "아이디를 입력해주세요.")
@Size(max = 12, message = "최대 12자리까지 입력 가능합니다.")
private final String id;

@NotBlank(message = "비밀번호를 입력해주세요.")
@Size(max = 20, message = "최대 20자리까지 입력 가능합니다.")
private final String password;

@NotBlank(message = "이름을 입력해주세요.")
@Size(max = 20, message = "최대 20자리까지 입력 가능합니다.")
private final String name;

@NotBlank(message = "이메일을 입력해주세요.")
@Size(max = 50, message = "최대 50자리까지 입력 가능합니다.")
@Email(message = "올바른 이메일 형식이 아닙니다.")
private final String email;

@NotBlank(message = "휴대전화번호를 입력해주세요.")
@Pattern(regexp = "[0-9]{10,11}", message = "- 을 제외한 숫자 10자리 또는 11자리를 입력해주세요.")
private final String telephone;

public AdminSignUp getEncryptedPasswordUserSignUp(Encryptor encryptor) {
return new AdminSignUp(this.id, encryptor.encrypt(this.password),
this.name, this.email, this.telephone);
}

}
package com.campool.model

import com.campool.encrypt.Encryptor
import javax.validation.constraints.Email
import javax.validation.constraints.NotBlank
import javax.validation.constraints.Pattern
import javax.validation.constraints.Size

class AdminSignUpRequest(
@field:NotBlank(message = "아이디를 입력해주세요.")
@field:Size(max = 12, message = "최대 12자리까지 입력 가능합니다.")
val id: String,

@field:NotBlank(message = "비밀번호를 입력해주세요.")
@field:Size(max = 20, message = "최대 20자리까지 입력 가능합니다.")
val password: String,

@field:NotBlank(message = "이름을 입력해주세요.")
@field:Size(max = 20, message = "최대 20자리까지 입력 가능합니다.")
val name: String,

@field:NotBlank(message = "이메일을 입력해주세요.")
@field:Size(max = 50, message = "최대 50자리까지 입력 가능합니다.")
@field:Email(message = "올바른 이메일 형식이 아닙니다.")
val email: String,

@field:NotBlank(message = "휴대전화번호를 입력해주세요.")
@field:Pattern(regexp = "[0-9]{10,11}", message = "- 을 제외한 숫자 10자리 또는 11자리를 입력해주세요.")
val telephone: String
) {

fun getEncryptedPasswordUserSignUp(encryptor: Encryptor): AdminSignUpRequest =
AdminSignUpRequest(id, encryptor.encrypt(password), name, email, telephone)
}
59 changes: 21 additions & 38 deletions src/main/java/com/campool/service/AdminService.kt
Original file line number Diff line number Diff line change
@@ -1,46 +1,29 @@
package com.campool.service;
package com.campool.service

import com.campool.encrypt.Encryptor;
import com.campool.exception.NoSuchUserException;
import com.campool.mapper.AdminMapper;
import com.campool.model.AdminSignUp;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import com.campool.mapper.AdminMapper
import com.campool.encrypt.Encryptor
import com.campool.model.AdminSignUpRequest
import com.campool.exception.NoSuchUserException
import com.campool.model.AdminInfo
import org.springframework.dao.DuplicateKeyException
import org.springframework.stereotype.Service

@RequiredArgsConstructor
@Service
public class AdminService {

@NonNull
private final AdminMapper adminMapper;

@NonNull
private final Encryptor encryptor;

public void add(AdminSignUp adminSignUp) {
if (isDuplicate(adminSignUp.getId())) {
throw new DuplicateKeyException("중복된 아이디가 존재합니다.");
class AdminService(
private val adminMapper: AdminMapper,
private val encryptor: Encryptor
) {
fun add(adminSignUpRequest: AdminSignUpRequest) {
if (isDuplicate(adminSignUpRequest.id)) {
throw DuplicateKeyException("중복된 아이디가 존재합니다.")
}
adminMapper.insertAdmin(adminSignUp.getEncryptedPasswordUserSignUp(encryptor));
}

public boolean isDuplicate(String id) {
return adminMapper.findById(id) != null;
adminMapper.insertAdmin(adminSignUpRequest.getEncryptedPasswordUserSignUp(encryptor))
}

public AdminSignUp getByIdAndPw(String id, String password) {
AdminSignUp adminSignUp = adminMapper.findByIdAndPassword(id, encryptor.encrypt(password));
if (isValidAdmin(adminSignUp)) {
return adminSignUp;
} else {
throw new NoSuchUserException("해당하는 관리자 정보가 없습니다.");
}
}

private boolean isValidAdmin(AdminSignUp adminSignUp) {
return adminSignUp != null;
}
fun isDuplicate(id: String): Boolean = adminMapper.findById(id) != null

}
fun getByIdAndPw(id: String, password: String): AdminInfo =
adminMapper.findByIdAndPassword(id, encryptor.encrypt(password))
?: throw NoSuchUserException("해당하는 관리자 정보가 없습니다.")
}
8 changes: 4 additions & 4 deletions src/test/java/com/campool/controller/AdminControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.campool.controller;

import com.campool.model.AdminSignUp;
import com.campool.model.AdminSignUpRequest;
import com.campool.service.AdminService;
import com.campool.service.AuthService;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -27,18 +27,18 @@ class AdminControllerTest {
@MockBean
AuthService authService;

AdminSignUp correctAdmin, incorrectAdmin;
AdminSignUpRequest correctAdmin, incorrectAdmin;

@BeforeEach
void setUp() {
correctAdmin = new AdminSignUp(
correctAdmin = new AdminSignUpRequest(
"adminId",
"adminPassword",
"adminName",
"admin@email.co.kr",
"01012341234");

incorrectAdmin = new AdminSignUp(
incorrectAdmin = new AdminSignUpRequest(
"adminIdOver12Size",
"adminPasswordOver20Size",
"adminNameOver20Size@@@@",
Expand Down
Loading

0 comments on commit 04c4484

Please sign in to comment.