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

Commit

Permalink
- DB 관련 정보 삭제
Browse files Browse the repository at this point in the history
- controller의 login 메서드가 에러를 throw하지 않도록 변경

- 사장 정보 수정 시 Patch 방식을 이용하도록 변경

- 메일, 전화번호 수정 메서드에 비밀번호가 맞아야 수정할 수 있도록 변경

- 비밀번호 수정 시 기존 비밀번호와 새 비밀번호 NPE 방지 조건 수정

- 사장 업데이트  Response 클래스 하나로 통합

- OwnerService 로그인 메서드 사장 정보 조회 메서드로 변경
  • Loading branch information
binaryyoung committed Oct 17, 2019
1 parent 135495b commit 8b61187
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 97 deletions.
152 changes: 71 additions & 81 deletions src/main/java/com/delfood/controller/OwnerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -38,25 +39,25 @@ public class OwnerController {
@PostMapping("login")
public ResponseEntity<OwnerLoginResponse> login(@RequestBody OwnerLoginRequest loginRequest,
HttpSession session) {
OwnerDTO ownerInfo = ownerService.login(loginRequest.getId(), loginRequest.getPassword());
OwnerDTO ownerInfo = ownerService.getOwner(loginRequest.getId(), loginRequest.getPassword());
OwnerLoginResponse ownerLoginResponse;
ResponseEntity<OwnerLoginResponse> responseEntity;

if (ownerInfo == null) { // 아이디와 비밀번호가 일치하지 않거나, 회원정보가 없음
ownerLoginResponse = OwnerLoginResponse.FAIL;
responseEntity =
new ResponseEntity<OwnerLoginResponse>(ownerLoginResponse, HttpStatus.UNAUTHORIZED);
} else if (Status.DEFAULT.equals(ownerInfo.getStatus())) { // 성공
ownerLoginResponse = OwnerLoginResponse.success(ownerInfo);
session.setAttribute("LOGIN_OWNER_ID", ownerInfo.getId());
responseEntity = new ResponseEntity<OwnerLoginResponse>(ownerLoginResponse, HttpStatus.OK);
} else if (Status.DELETED.equals(ownerInfo.getStatus())) { // 삭제된 계정일 때
ownerLoginResponse = OwnerLoginResponse.DELETED;
responseEntity = new ResponseEntity<OwnerController.OwnerLoginResponse>(ownerLoginResponse,
HttpStatus.UNAUTHORIZED);
} else { // 예상치 못한 에러처리
log.error("login error {} ", loginRequest);
throw new RuntimeException("login error");
} else { // 회원 정보가 존재
Status ownerStatus = ownerInfo.getStatus();
if (ownerStatus == Status.DEFAULT) {
ownerLoginResponse = OwnerLoginResponse.success(ownerInfo);
session.setAttribute("LOGIN_OWNER_ID", ownerInfo.getId());
responseEntity = new ResponseEntity<OwnerLoginResponse>(ownerLoginResponse, HttpStatus.OK);
} else {
ownerLoginResponse = OwnerLoginResponse.DELETED;
responseEntity = new ResponseEntity<OwnerController.OwnerLoginResponse>(ownerLoginResponse,
HttpStatus.UNAUTHORIZED);
}
}
return responseEntity;
}
Expand Down Expand Up @@ -95,7 +96,7 @@ public ResponseEntity<OwnerDTO> ownerInfo(HttpSession session) {
if (id == null) {
responseEntity = new ResponseEntity<OwnerDTO>(HttpStatus.UNAUTHORIZED);
} else {
OwnerDTO ownerInfo = ownerService.ownerInfo(id);
OwnerDTO ownerInfo = ownerService.getOwner(id);
responseEntity = new ResponseEntity<OwnerDTO>(ownerInfo, HttpStatus.OK);
}
return responseEntity;
Expand All @@ -108,36 +109,38 @@ public ResponseEntity<OwnerDTO> ownerInfo(HttpSession session) {
* @param session 현재 사용자 세션
* @return
*/
@PutMapping
public ResponseEntity<UpdateOwnerMailAndTelResponse> updateOwnerInfo(
@PatchMapping
public ResponseEntity<UpdateOwnerResponse> updateOwnerInfo(
@RequestBody UpdateOwnerMailAndTelRequest updateRequest, HttpSession session) {

String mail = updateRequest.getMail();
String tel = updateRequest.getTel();
String password = updateRequest.getPassword();
String id = (String) session.getAttribute("LOGIN_OWNER_ID");
ResponseEntity<UpdateOwnerMailAndTelResponse> responseEntity;

if (mail == null) {
responseEntity = new ResponseEntity<OwnerController.UpdateOwnerMailAndTelResponse>(
UpdateOwnerMailAndTelResponse.EMPTY_MAIL, HttpStatus.BAD_REQUEST);
} else if (tel == null) {
responseEntity = new ResponseEntity<OwnerController.UpdateOwnerMailAndTelResponse>(
UpdateOwnerMailAndTelResponse.EMPTY_TEL, HttpStatus.BAD_REQUEST);
} else if (id == null) {
responseEntity = new ResponseEntity<OwnerController.UpdateOwnerMailAndTelResponse>(
UpdateOwnerMailAndTelResponse.NO_LOGIN, HttpStatus.UNAUTHORIZED);
} else {
DMLOperationError dmlOperationError = ownerService.updateOwnerMailAndTel(id, mail, tel);

if (dmlOperationError == DMLOperationError.SUCCESS) {
responseEntity = new ResponseEntity<OwnerController.UpdateOwnerMailAndTelResponse>(
UpdateOwnerMailAndTelResponse.SUCCESS, HttpStatus.OK);
} else {
log.error("Member mail and tel update ERROR : {}", updateRequest);
throw new RuntimeException("Member mail and tel update ERROR");
}
if (id == null) { // 로그인 상태가 아닌 경우
return new ResponseEntity<OwnerController.UpdateOwnerResponse>(
UpdateOwnerResponse.NO_LOGIN, HttpStatus.UNAUTHORIZED);
}

if (ownerService.getOwner(id, password) == null) {
return new ResponseEntity<OwnerController.UpdateOwnerResponse>(
UpdateOwnerResponse.PASSWORD_MISMATCH, HttpStatus.UNAUTHORIZED);
}

if (mail == null && tel == null) { // 변경하려는 정보가 둘 다 null일 경우
return new ResponseEntity<OwnerController.UpdateOwnerResponse>(
UpdateOwnerResponse.EMPTY_CONTENT, HttpStatus.BAD_REQUEST);
}

DMLOperationError dmlOperationError = ownerService.updateOwnerMailAndTel(id, mail, tel);
if (dmlOperationError == DMLOperationError.SUCCESS) {
return new ResponseEntity<OwnerController.UpdateOwnerResponse>(
UpdateOwnerResponse.SUCCESS, HttpStatus.OK);
} else {
log.error("Member mail and tel update ERROR : {}", updateRequest);
throw new RuntimeException("Member mail and tel update ERROR");
}
return responseEntity;
}

/**
Expand All @@ -147,38 +150,39 @@ public ResponseEntity<UpdateOwnerMailAndTelResponse> updateOwnerInfo(
* @param session 현재 사용자의 세션
* @return
*/
@PutMapping("password")
public ResponseEntity<UpdateOwnerPasswordResponse> updatePassword(
@PatchMapping("password")
public ResponseEntity<UpdateOwnerResponse> updatePassword(
@RequestBody UpdateOwnerPasswordRequest passwordResquest, HttpSession session) {
String id = (String) session.getAttribute("LOGIN_OWNER_ID");
String password = passwordResquest.getPassword();
String newPassword = passwordResquest.getNewPassword();

ResponseEntity<UpdateOwnerPasswordResponse> responseEntity;
ResponseEntity<UpdateOwnerResponse> responseEntity;


if (id == null) { // 비 로그인 상태
responseEntity = new ResponseEntity<OwnerController.UpdateOwnerPasswordResponse>(
UpdateOwnerPasswordResponse.NO_LOGIN, HttpStatus.UNAUTHORIZED);
} else if (ownerService.login(id, password) == null) { // 아이디와 비밀번호 불일치
responseEntity = new ResponseEntity<OwnerController.UpdateOwnerPasswordResponse>(
UpdateOwnerPasswordResponse.PASSWORD_MISMATCH, HttpStatus.BAD_REQUEST);
} else if (newPassword == null) {
responseEntity = new ResponseEntity<OwnerController.UpdateOwnerPasswordResponse>(
UpdateOwnerPasswordResponse.EMPTY_PASSOWRD, HttpStatus.BAD_REQUEST);
responseEntity = new ResponseEntity<OwnerController.UpdateOwnerResponse>(
UpdateOwnerResponse.NO_LOGIN, HttpStatus.UNAUTHORIZED);
} else if (password == null || newPassword == null) { // 비밀번호나 새 비밀번호를 입력하지 않은 경우
responseEntity = new ResponseEntity<OwnerController.UpdateOwnerResponse>(
UpdateOwnerResponse.EMPTY_PASSOWRD, HttpStatus.BAD_REQUEST);
} else if (ownerService.getOwner(id, password) == null) { // 아이디와 비밀번호 불일치
responseEntity = new ResponseEntity<OwnerController.UpdateOwnerResponse>(
UpdateOwnerResponse.PASSWORD_MISMATCH, HttpStatus.UNAUTHORIZED);
} else if (password.equals(newPassword)) { // 이전 패스워드와 동일한 경우
responseEntity = new ResponseEntity<OwnerController.UpdateOwnerPasswordResponse>(
UpdateOwnerPasswordResponse.PASSWORD_DUPLICATED, HttpStatus.CONFLICT);
responseEntity = new ResponseEntity<OwnerController.UpdateOwnerResponse>(
UpdateOwnerResponse.PASSWORD_DUPLICATED, HttpStatus.CONFLICT);
} else {
DMLOperationError dmlOperationError = ownerService.updateOwnerPassword(id, newPassword);

if (DMLOperationError.SUCCESS.equals(dmlOperationError)) {
responseEntity = new ResponseEntity<OwnerController.UpdateOwnerPasswordResponse>(
UpdateOwnerPasswordResponse.SUCCESS, HttpStatus.OK);
responseEntity = new ResponseEntity<OwnerController.UpdateOwnerResponse>(
UpdateOwnerResponse.SUCCESS, HttpStatus.OK);
} else {
log.error("Password Update Error {}", passwordResquest);
throw new RuntimeException("Password Update Error");
}

}
return responseEntity;
}
Expand All @@ -199,6 +203,8 @@ private static class OwnerLoginRequest {
@Setter
@Getter
private static class UpdateOwnerMailAndTelRequest {
@NonNull
private String password;
@NonNull
private String mail;
@NonNull
Expand Down Expand Up @@ -240,42 +246,26 @@ private static OwnerLoginResponse success(OwnerDTO ownerInfo) {

@Getter
@RequiredArgsConstructor
private static class UpdateOwnerMailAndTelResponse {
enum UpdateStatus {
SUCCESS, NO_LOGIN, EMPTY_CONTENT
}

@NonNull
private UpdateStatus result;

private static final UpdateOwnerMailAndTelResponse SUCCESS =
new UpdateOwnerMailAndTelResponse(UpdateStatus.SUCCESS);
private static final UpdateOwnerMailAndTelResponse NO_LOGIN =
new UpdateOwnerMailAndTelResponse(UpdateStatus.NO_LOGIN);
private static final UpdateOwnerMailAndTelResponse EMPTY_CONTENT =
new UpdateOwnerMailAndTelResponse(UpdateStatus.EMPTY_CONTENT);
}

@Getter
@RequiredArgsConstructor
private static class UpdateOwnerPasswordResponse {
private static class UpdateOwnerResponse {
enum UpdateStatus {
SUCCESS, NO_LOGIN, EMPTY_PASSOWRD, PASSWORD_MISMATCH, PASSWORD_DUPLICATED
SUCCESS, NO_LOGIN, EMPTY_CONTENT, EMPTY_PASSOWRD, PASSWORD_MISMATCH, PASSWORD_DUPLICATED
}

@NonNull
private UpdateStatus result;

private static final UpdateOwnerPasswordResponse SUCCESS =
new UpdateOwnerPasswordResponse(UpdateStatus.SUCCESS);
private static final UpdateOwnerPasswordResponse NO_LOGIN =
new UpdateOwnerPasswordResponse(UpdateStatus.NO_LOGIN);
private static final UpdateOwnerPasswordResponse EMPTY_PASSOWRD =
new UpdateOwnerPasswordResponse(UpdateStatus.EMPTY_PASSOWRD);
private static final UpdateOwnerPasswordResponse PASSWORD_MISMATCH =
new UpdateOwnerPasswordResponse(UpdateStatus.PASSWORD_MISMATCH);
private static final UpdateOwnerPasswordResponse PASSWORD_DUPLICATED =
new UpdateOwnerPasswordResponse(UpdateStatus.PASSWORD_DUPLICATED);
private static final UpdateOwnerResponse SUCCESS =
new UpdateOwnerResponse(UpdateStatus.SUCCESS);
private static final UpdateOwnerResponse NO_LOGIN =
new UpdateOwnerResponse(UpdateStatus.NO_LOGIN);
private static final UpdateOwnerResponse EMPTY_CONTENT =
new UpdateOwnerResponse(UpdateStatus.EMPTY_CONTENT);
private static final UpdateOwnerResponse EMPTY_PASSOWRD =
new UpdateOwnerResponse(UpdateStatus.EMPTY_PASSOWRD);
private static final UpdateOwnerResponse PASSWORD_MISMATCH =
new UpdateOwnerResponse(UpdateStatus.PASSWORD_MISMATCH);
private static final UpdateOwnerResponse PASSWORD_DUPLICATED =
new UpdateOwnerResponse(UpdateStatus.PASSWORD_DUPLICATED);
}


Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/delfood/service/OwnerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public class OwnerService {
private OwnerMapper ownerMapper;

/**
* 사장님 로그인.
* 사장 정보 조회.
*
* @param id 아이디
* @param password 패스워드
* @return
* @return id, name, mail, tel, createAt, updatedAt, status
*/
public OwnerDTO login(String id, String password) {
public OwnerDTO getOwner(String id, String password) {
String cryptoPassword = SHA256Util.encryptSHA256(password);
OwnerDTO ownerInfo = ownerMapper.findByIdAndPassword(id, cryptoPassword);
return ownerInfo;
Expand All @@ -33,9 +33,10 @@ public OwnerDTO login(String id, String password) {
/**
* 사장 정보 조회.
*
* @param id 아이디
* @return id, name, mail, tel, createAt, updatedAt, status
*/
public OwnerDTO ownerInfo(String id) {
public OwnerDTO getOwner(String id) {
return ownerMapper.findById(id);
}

Expand Down
13 changes: 1 addition & 12 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,4 @@ server.port=80
spring.profiles.active=local

# session
spring.session.store-type=redis

# DB
spring.datasource.url=jdbc:mariadb://yyy9942.cafe24.com:3306/yyy9942
spring.datasource.username=yyy9942
spring.datasource.password=wjdwns123
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver

# redis
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.session.store-type=redis

0 comments on commit 8b61187

Please sign in to comment.