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

Commit

Permalink
Merge pull request #28 from f-lab-edu/feature/3
Browse files Browse the repository at this point in the history
Feature/3
  • Loading branch information
yyy9942 authored Nov 7, 2019
2 parents 1334e2d + ad07e20 commit a4302b9
Show file tree
Hide file tree
Showing 14 changed files with 372 additions and 191 deletions.
127 changes: 74 additions & 53 deletions src/main/java/com/delfood/controller/MemberController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.delfood.controller;

import com.delfood.dto.MemberDTO;
import com.delfood.mapper.DMLOperationResult;
import com.delfood.service.MemberService;
import com.delfood.utils.SessionUtil;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
Expand All @@ -16,6 +16,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
Expand All @@ -41,6 +42,7 @@
* 스레드 환경 로깅이 필요하다면 Log4j2를 사용하는 것이 성능면에서 유리하다. 사용자 정의 로그레벨과 람다 표현식을 지원한다. Log4j2 자체적으로 직접 사용할 수는
* 있지만 일반적으로는 SLF4J와 함께 사용한다.
* </p>
*
* @Log @Slf4j @Log4j2 등 어노테이션 적용시 자동으로 log 필드를 만들고 해당 클래스의 이름으로 로거 객체를 생성하여 할당한다.
*
* @author 정준
Expand All @@ -62,7 +64,7 @@ public class MemberController {
@GetMapping("myInfo")
public ResponseEntity<MemberDTO> memberInfo(HttpSession session) {
ResponseEntity<MemberDTO> responseEntity = null;
String id = (String) session.getAttribute("LOGIN_MEMBER_ID");
String id = SessionUtil.getLoginMemberId(session);
if (id == null) {
responseEntity = new ResponseEntity<MemberDTO>(HttpStatus.UNAUTHORIZED);
} else {
Expand Down Expand Up @@ -130,7 +132,7 @@ public ResponseEntity<SignUpResponse> signUp(@RequestBody @NotNull MemberDTO mem
return responseEntity;
}

/*
/**
* 회원 로그인을 진행한다. Login 요청시 id, password가 NULL일 경우 NullPointerException을 throw한다.
*/
@PostMapping("login")
Expand All @@ -150,7 +152,7 @@ public ResponseEntity<LoginResponse> login(@RequestBody @NonNull MemberLoginRequ
} else if (MemberDTO.Status.DEFAULT.equals(memberInfo.getStatus())) {
// 성공시 세션에 ID를 저장
loginResponse = LoginResponse.success(memberInfo);
session.setAttribute("LOGIN_MEMBER_ID", memberInfo.getId());
SessionUtil.setLoginMemberId(session, id);
responseEntity = new ResponseEntity<LoginResponse>(loginResponse, HttpStatus.OK);
} else if (MemberDTO.Status.DELETED.equals(memberInfo.getStatus())) {
// 삭제된 경우
Expand All @@ -166,17 +168,39 @@ public ResponseEntity<LoginResponse> login(@RequestBody @NonNull MemberLoginRequ
}

/**
* 회원 비밀번호 변경
* 회원 로그아웃 메서드.
*
* @author jun
* @param session 현제 접속한 세션
* @return 로그인 하지 않았을 시 401코드를 반환하고 result:NO_LOGIN 반환 로그아웃 성공시 200 코드를 반환하고 result:SUCCESS 반환
*/
@GetMapping("logout")
public ResponseEntity<LogoutResponse> logout(HttpSession session) {
String memberId = (String) session.getAttribute("LOGIN_MEMBER_ID");
if (memberId == null) {
return new ResponseEntity<MemberController.LogoutResponse>(LogoutResponse.NO_LOGIN,
HttpStatus.UNAUTHORIZED);
} else {
SessionUtil.logoutMember(session);
}


return new ResponseEntity<MemberController.LogoutResponse>(LogoutResponse.SUCCESS,
HttpStatus.OK);
}

/**
* 회원 비밀번호 변경.
*
* @param session 현재 로그인한 사용자의 세션
* @return
*/
@PutMapping("password")
@PatchMapping("password")
public ResponseEntity<UpdateMemberPasswordResponse> updateMemberInfo(HttpSession session,
@RequestBody @NotNull UpdateMemberPasswordRequest passwordRequest) {
String password = passwordRequest.getPassword();
String newPassword = passwordRequest.getNewPassword();
String id = (String) session.getAttribute("LOGIN_MEMBER_ID");
String id = SessionUtil.getLoginMemberId(session);
ResponseEntity<UpdateMemberPasswordResponse> responseEntity = null;
UpdateMemberPasswordResponse updateResponse;

Expand All @@ -199,16 +223,10 @@ public ResponseEntity<UpdateMemberPasswordResponse> updateMemberInfo(HttpSession
new ResponseEntity<UpdateMemberPasswordResponse>(updateResponse, HttpStatus.BAD_REQUEST);
} else {
// 성공시
DMLOperationResult dmlResponse = memberService.updateMemberPassword(id, newPassword);
if (dmlResponse == DMLOperationResult.SUCCESS) {
updateResponse = UpdateMemberPasswordResponse.SUCCESS;
responseEntity =
new ResponseEntity<UpdateMemberPasswordResponse>(updateResponse, HttpStatus.OK);
} else {
// 정상적인 상황에서는 발생하지 않는 에러
log.error("Member Password Update - ERROR!");
throw new RuntimeException("Password update ERROR");
}
memberService.updateMemberPassword(id, newPassword);
updateResponse = UpdateMemberPasswordResponse.SUCCESS;
responseEntity =
new ResponseEntity<UpdateMemberPasswordResponse>(updateResponse, HttpStatus.OK);
}

return responseEntity;
Expand All @@ -225,40 +243,36 @@ public ResponseEntity<UpdateMemberPasswordResponse> updateMemberInfo(HttpSession
public ResponseEntity<DeleteMemberResponse> deleteMemberInfo(HttpSession session) {
ResponseEntity<DeleteMemberResponse> responseEntity = null;
DeleteMemberResponse deleteResponse;
String id = (String) session.getAttribute("LOGIN_MEMBER_ID");
String id = SessionUtil.getLoginMemberId(session);
if (id == null) {
deleteResponse = DeleteMemberResponse.NO_LOGIN;
responseEntity =
new ResponseEntity<DeleteMemberResponse>(deleteResponse, HttpStatus.UNAUTHORIZED);
} else {
DMLOperationResult dmlResponse = memberService.deleteMember(id);
if (dmlResponse == DMLOperationResult.SUCCESS) {
deleteResponse = DeleteMemberResponse.SUCCESS;
// 회원 탈퇴시 로그아웃 시켜야 하기 때문에 세션 정보를 날린다
session.invalidate();
responseEntity = new ResponseEntity<DeleteMemberResponse>(deleteResponse, HttpStatus.OK);
} else {
// 정상적인 상황에서는 발생하지 않는 에러
log.error("Member Delete ERROR! \n" + responseEntity);
throw new RuntimeException("Member Delete ERROR! " + responseEntity);
}
memberService.deleteMember(id);
deleteResponse = DeleteMemberResponse.SUCCESS;

// 회원 탈퇴시 로그아웃 시켜야 하기 때문에 세션 정보를 날린다
SessionUtil.logoutAll(session);
responseEntity = new ResponseEntity<DeleteMemberResponse>(deleteResponse, HttpStatus.OK);

}
return responseEntity;
}

/**
* 회원 주소 변경
* 회원 주소 변경.
*
* @param memberInfo 회원 주소 정보
* @param session 현재 로그인한 고객의 세션
*/
@PutMapping("address")
@PatchMapping("address")
public ResponseEntity<UpdateMemberAddressResponse> updateMemberAddress(
@RequestBody @NotNull UpdateMemberAddressRequest memberInfo, HttpSession session) {
ResponseEntity<UpdateMemberAddressResponse> responseEntity = null;
String address = memberInfo.getAddress();
String addressDetail = memberInfo.getAddressDetail();
String id = (String) session.getAttribute("LOGIN_MEMBER_ID");
String id = SessionUtil.getLoginMemberId(session);

if (address == null || addressDetail == null) {
// 요청한 주소가 null일 때
Expand All @@ -274,21 +288,10 @@ public ResponseEntity<UpdateMemberAddressResponse> updateMemberAddress(
responseEntity = new ResponseEntity<MemberController.UpdateMemberAddressResponse>(
UpdateMemberAddressResponse.NO_LOGIN, HttpStatus.UNAUTHORIZED);
} else {
DMLOperationResult dmlResponse = memberService.updateMemberAddress(id, address, addressDetail);
if (dmlResponse == DMLOperationResult.SUCCESS) {
// 성공시
responseEntity = new ResponseEntity<MemberController.UpdateMemberAddressResponse>(
UpdateMemberAddressResponse.SUCCESS, HttpStatus.OK);
} else if (dmlResponse == DMLOperationResult.NONE_CHANGED) {
// 주소 변경이 되지 않았을 때
log.error("Member Address Update ERROR " + dmlResponse);
throw new RuntimeException("Member Address Update ERROR");
} else {
// 너무 많은 주소가 변경되었을 때. 정상적인 상황에서는 발생하지 않는다.
log.error("Member Address Update ERROR " + dmlResponse);
throw new RuntimeException("Member Address Update ERROR");
}

// 모든 조건을 충족할 때
memberService.updateMemberAddress(id, address, addressDetail);
responseEntity = new ResponseEntity<MemberController.UpdateMemberAddressResponse>(
UpdateMemberAddressResponse.SUCCESS, HttpStatus.OK);
}

return responseEntity;
Expand All @@ -303,24 +306,42 @@ public ResponseEntity<UpdateMemberAddressResponse> updateMemberAddress(
@AllArgsConstructor
@RequiredArgsConstructor
private static class LoginResponse {
enum LogInStatus {
enum LoginStatus {
SUCCESS, FAIL, DELETED, ERROR
}

@NonNull
private LogInStatus result;
private LoginStatus result;
private MemberDTO memberInfo;

// success의 경우 memberInfo의 값을 set해줘야 하기 때문에 new 하도록 해준다.

private static final LoginResponse FAIL = new LoginResponse(LogInStatus.FAIL);
private static final LoginResponse DELETED = new LoginResponse(LogInStatus.DELETED);
private static final LoginResponse FAIL = new LoginResponse(LoginStatus.FAIL);
private static final LoginResponse DELETED = new LoginResponse(LoginStatus.DELETED);

private static LoginResponse success(MemberDTO memberInfo) {
return new LoginResponse(LogInStatus.SUCCESS, memberInfo);
return new LoginResponse(LoginStatus.SUCCESS, memberInfo);
}


}

@Getter
@RequiredArgsConstructor
private static class LogoutResponse {
enum LogoutStatus {
SUCCESS, NO_LOGIN, ERROR
}

@NonNull
private LogoutStatus result;


private static final LogoutResponse NO_LOGIN = new LogoutResponse(LogoutStatus.NO_LOGIN);
private static final LogoutResponse SUCCESS = new LogoutResponse(LogoutStatus.SUCCESS);



}

@Getter
Expand Down Expand Up @@ -436,7 +457,7 @@ private static class MemberLoginRequest {

@Setter
@Getter
private class UpdateMemberAddressRequest {
private static class UpdateMemberAddressRequest {
@NonNull
private String address;
@NonNull
Expand Down
54 changes: 21 additions & 33 deletions src/main/java/com/delfood/controller/OwnerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.delfood.dto.OwnerDTO;
import com.delfood.dto.OwnerDTO.Status;
import com.delfood.mapper.DMLOperationResult;
import com.delfood.service.OwnerService;
import com.delfood.utils.SessionUtil;
import javax.servlet.http.HttpSession;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down Expand Up @@ -93,7 +93,7 @@ public ResponseEntity<OwnerLoginResponse> login(@RequestBody OwnerLoginRequest l
Status ownerStatus = ownerInfo.getStatus();
if (ownerStatus == Status.DEFAULT) {
ownerLoginResponse = OwnerLoginResponse.success(ownerInfo);
session.setAttribute("LOGIN_OWNER_ID", ownerInfo.getId());
SessionUtil.setLoginOwnerId(session, loginRequest.getId());
responseEntity = new ResponseEntity<OwnerLoginResponse>(ownerLoginResponse, HttpStatus.OK);
} else {
ownerLoginResponse = OwnerLoginResponse.DELETED;
Expand All @@ -112,14 +112,14 @@ public ResponseEntity<OwnerLoginResponse> login(@RequestBody OwnerLoginRequest l
* @return
*/
@GetMapping("logout")
public ResponseEntity<logoutResponse> logout(HttpSession session) {
String id = (String) session.getAttribute("LOGIN_OWNER_ID");
public ResponseEntity<LogoutResponse> logout(HttpSession session) {
String id = SessionUtil.getLoginOwnerId(session);
if (id != null) {
session.invalidate();
return new ResponseEntity<OwnerController.logoutResponse>(logoutResponse.SUCCESS,
SessionUtil.logoutOwner(session);
return new ResponseEntity<OwnerController.LogoutResponse>(LogoutResponse.SUCCESS,
HttpStatus.OK);
} else {
return new ResponseEntity<OwnerController.logoutResponse>(logoutResponse.NO_LOGIN,
return new ResponseEntity<OwnerController.LogoutResponse>(LogoutResponse.NO_LOGIN,
HttpStatus.UNAUTHORIZED);
}
}
Expand All @@ -134,7 +134,7 @@ public ResponseEntity<logoutResponse> logout(HttpSession session) {
@GetMapping("myInfo")
public ResponseEntity<OwnerDTO> ownerInfo(HttpSession session) {
ResponseEntity<OwnerDTO> responseEntity = null;
String id = (String) session.getAttribute("LOGIN_OWNER_ID");
String id = SessionUtil.getLoginOwnerId(session);
if (id == null) {
responseEntity = new ResponseEntity<OwnerDTO>(HttpStatus.UNAUTHORIZED);
} else {
Expand All @@ -158,7 +158,7 @@ public ResponseEntity<UpdateOwnerResponse> updateOwnerInfo(
String mail = updateRequest.getMail();
String tel = updateRequest.getTel();
String password = updateRequest.getPassword();
String id = (String) session.getAttribute("LOGIN_OWNER_ID");
String id = SessionUtil.getLoginOwnerId(session);

if (id == null) { // 로그인 상태가 아닌 경우
return new ResponseEntity<OwnerController.UpdateOwnerResponse>(
Expand All @@ -175,14 +175,9 @@ public ResponseEntity<UpdateOwnerResponse> updateOwnerInfo(
UpdateOwnerResponse.EMPTY_CONTENT, HttpStatus.BAD_REQUEST);
}

DMLOperationResult dmlOperationError = ownerService.updateOwnerMailAndTel(id, mail, tel);
if (dmlOperationError == DMLOperationResult.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");
}
ownerService.updateOwnerMailAndTel(id, mail, tel);
return new ResponseEntity<OwnerController.UpdateOwnerResponse>(
UpdateOwnerResponse.SUCCESS, HttpStatus.OK);
}

/**
Expand All @@ -195,7 +190,7 @@ public ResponseEntity<UpdateOwnerResponse> updateOwnerInfo(
@PatchMapping("password")
public ResponseEntity<UpdateOwnerResponse> updatePassword(
@RequestBody UpdateOwnerPasswordRequest passwordResquest, HttpSession session) {
String id = (String) session.getAttribute("LOGIN_OWNER_ID");
String id = SessionUtil.getLoginOwnerId(session);
String password = passwordResquest.getPassword();
String newPassword = passwordResquest.getNewPassword();

Expand All @@ -215,16 +210,9 @@ public ResponseEntity<UpdateOwnerResponse> updatePassword(
responseEntity = new ResponseEntity<OwnerController.UpdateOwnerResponse>(
UpdateOwnerResponse.PASSWORD_DUPLICATED, HttpStatus.CONFLICT);
} else {
DMLOperationResult dmlOperationError = ownerService.updateOwnerPassword(id, newPassword);

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

ownerService.updateOwnerPassword(id, newPassword);
responseEntity = new ResponseEntity<OwnerController.UpdateOwnerResponse>(
UpdateOwnerResponse.SUCCESS, HttpStatus.OK);
}
return responseEntity;
}
Expand Down Expand Up @@ -344,16 +332,16 @@ enum UpdateStatus {

@Getter
@RequiredArgsConstructor
private static class logoutResponse {
enum logoutStatus {
private static class LogoutResponse {
enum LogoutStatus {
SUCCESS, NO_LOGIN
}

@NonNull
private logoutStatus result;
private LogoutStatus result;

private static final logoutResponse SUCCESS = new logoutResponse(logoutStatus.SUCCESS);
private static final logoutResponse NO_LOGIN = new logoutResponse(logoutStatus.NO_LOGIN);
private static final LogoutResponse SUCCESS = new LogoutResponse(LogoutStatus.SUCCESS);
private static final LogoutResponse NO_LOGIN = new LogoutResponse(LogoutStatus.NO_LOGIN);

}

Expand Down
Loading

0 comments on commit a4302b9

Please sign in to comment.