This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: [#80] Admin Domain 코드 Kotlin으로 변경
- Loading branch information
Showing
9 changed files
with
157 additions
and
163 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("해당하는 관리자 정보가 없습니다.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.