From 46b58d46b09565fcd0d8a63be0581d7aa834191d Mon Sep 17 00:00:00 2001 From: binaryyoung Date: Tue, 15 Oct 2019 16:47:22 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=EC=82=AC=EC=9E=A5=EB=8B=98=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 로그인, 로그아웃 - 사장님 정보 조회 - 사장님 이메일, 전화번호 수정 - 사장님 비밀번호 수정 --- .../delfood/controller/OwnerController.java | 291 ++++++++++++++++++ src/main/java/com/delfood/dto/OwnerDTO.java | 8 +- .../java/com/delfood/mapper/OwnerMapper.java | 11 + .../com/delfood/service/OwnerService.java | 72 +++++ src/main/resources/application.properties | 22 +- src/main/resources/mybatis/mapper/owner.xml | 29 +- 6 files changed, 417 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/delfood/controller/OwnerController.java b/src/main/java/com/delfood/controller/OwnerController.java index f9e9f72..a2a693e 100644 --- a/src/main/java/com/delfood/controller/OwnerController.java +++ b/src/main/java/com/delfood/controller/OwnerController.java @@ -1,10 +1,301 @@ package com.delfood.controller; +import com.delfood.dto.OwnerDTO; +import com.delfood.dto.OwnerDTO.Status; +import com.delfood.mapper.DMLOperationError; +import com.delfood.service.OwnerService; +import javax.servlet.http.HttpSession; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import lombok.extern.log4j.Log4j2; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/owners/") +@Log4j2 public class OwnerController { + @Autowired + private OwnerService ownerService; + + /** + * 회원 로그인 기능 수행. + * + * @param loginRequest 로그인 요청 ( id, password ) + * @return + */ + @PostMapping("login") + public ResponseEntity login(@RequestBody OwnerLoginRequest loginRequest, + HttpSession session) { + OwnerDTO ownerInfo = ownerService.login(loginRequest.getId(), loginRequest.getPassword()); + OwnerLoginResponse ownerLoginResponse; + ResponseEntity responseEntity; + + if (ownerInfo == null) { // 아이디와 비밀번호가 일치하지 않거나, 회원정보가 없음 + ownerLoginResponse = OwnerLoginResponse.FAIL; + responseEntity = + new ResponseEntity(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, HttpStatus.OK); + } else if (Status.DELETED.equals(ownerInfo.getStatus())) { // 삭제된 계정일 때 + ownerLoginResponse = OwnerLoginResponse.DELETED; + responseEntity = new ResponseEntity(ownerLoginResponse, + HttpStatus.UNAUTHORIZED); + } else { // 예상치 못한 에러처리 + log.error("login error {} ", loginRequest); + throw new RuntimeException("login error"); + } + return responseEntity; + } + + + /** + * 사장님 로그아웃. + * + * @param session 현재 사용자 세션 + * @return + */ + @PostMapping("logout") + public ResponseEntity logout(HttpSession session) { + String id = (String) session.getAttribute("LOGIN_OWNER_ID"); + if (id != null) { + session.invalidate(); + return new ResponseEntity( + logoutResponse.SUCCESS, HttpStatus.OK); + } else { + return new ResponseEntity( + logoutResponse.NO_LOGIN, HttpStatus.UNAUTHORIZED); + } + } + + + /** + * 로그인한 사장의 정보를 조회. + * + * @param session 현재 사용자 세션 + * @return + */ + @GetMapping("myInfo") + public ResponseEntity ownerInfo(HttpSession session) { + ResponseEntity responseEntity = null; + String id = (String) session.getAttribute("LOGIN_OWNER_ID"); + if (id == null) { + responseEntity = new ResponseEntity(HttpStatus.UNAUTHORIZED); + } else { + OwnerDTO ownerInfo = ownerService.ownerInfo(id); + responseEntity = new ResponseEntity(ownerInfo, HttpStatus.OK); + } + return responseEntity; + } + + /** + * 사장 이메일, 전화번호 변경. + * + * @param updateRequest 이메일, 전화번호를 포함한 update 객체 + * @param session 현재 사용자 세션 + * @return + */ + @PutMapping + public ResponseEntity updateOwnerInfo( + @RequestBody UpdateOwnerMailAndTelRequest updateRequest, HttpSession session) { + + String mail = updateRequest.getMail(); + String tel = updateRequest.getTel(); + String id = (String) session.getAttribute("LOGIN_OWNER_ID"); + ResponseEntity responseEntity; + + if (mail == null) { + responseEntity = new ResponseEntity( + UpdateOwnerMailAndTelResponse.EMPTY_MAIL, HttpStatus.BAD_REQUEST); + } else if (tel == null) { + responseEntity = new ResponseEntity( + UpdateOwnerMailAndTelResponse.EMPTY_TEL, HttpStatus.BAD_REQUEST); + } else if (id == null) { + responseEntity = new ResponseEntity( + UpdateOwnerMailAndTelResponse.NO_LOGIN, HttpStatus.UNAUTHORIZED); + } else { + DMLOperationError dmlOperationError = ownerService.updateOwnerMailAndTel(id, mail, tel); + + if (dmlOperationError == DMLOperationError.SUCCESS) { + responseEntity = new ResponseEntity( + UpdateOwnerMailAndTelResponse.SUCCESS, HttpStatus.OK); + } else { + log.error("Member mail and tel update ERROR : {}", updateRequest); + throw new RuntimeException("Member mail and tel update ERROR"); + } + } + return responseEntity; + } + + /** + * 사장 패스워드 변경. + * + * @param passwordResquest 변경전 패스워드, 변경할 패스워드을 담은 요청 객체 + * @param session 현재 사용자의 세션 + * @return + */ + @PutMapping("password") + public ResponseEntity updatePassword( + @RequestBody UpdateOwnerPasswordRequest passwordResquest, HttpSession session) { + String id = (String) session.getAttribute("LOGIN_OWNER_ID"); + String password = passwordResquest.getPassword(); + String newPassword = passwordResquest.getNewPassword(); + + ResponseEntity responseEntity; + + + if (id == null) { // 비 로그인 상태 + responseEntity = new ResponseEntity( + UpdateOwnerPasswordResponse.NO_LOGIN, HttpStatus.UNAUTHORIZED); + } else if (ownerService.login(id, password) == null) { // 아이디와 비밀번호 불일치 + responseEntity = new ResponseEntity( + UpdateOwnerPasswordResponse.PASSWORD_MISMATCH, HttpStatus.BAD_REQUEST); + } else if (newPassword == null) { + responseEntity = new ResponseEntity( + UpdateOwnerPasswordResponse.EMPTY_PASSOWRD, HttpStatus.BAD_REQUEST); + } else if (password.equals(newPassword)) { // 이전 패스워드와 동일한 경우 + responseEntity = new ResponseEntity( + UpdateOwnerPasswordResponse.PASSWORD_DUPLICATED, HttpStatus.CONFLICT); + } else { + DMLOperationError dmlOperationError = ownerService.updateOwnerPassword(id, newPassword); + + if (DMLOperationError.SUCCESS.equals(dmlOperationError)) { + responseEntity = new ResponseEntity( + UpdateOwnerPasswordResponse.SUCCESS, HttpStatus.OK); + } else { + log.error("Password Update Error {}", passwordResquest); + throw new RuntimeException("Password Update Error"); + } + } + return responseEntity; + } + + + + // ============= Requset 객체 ================ + + @Setter + @Getter + private static class OwnerLoginRequest { + @NonNull + private String id; + @NonNull + private String password; + } + + @Setter + @Getter + private static class UpdateOwnerMailAndTelRequest { + @NonNull + private String mail; + @NonNull + private String tel; + } + + @Setter + @Getter + private static class UpdateOwnerPasswordRequest { + @NonNull + private String password; + @NonNull + private String newPassword; + } + + + // ============ resopnse 객체 ===================== + + @Getter + @AllArgsConstructor + @RequiredArgsConstructor + private static class OwnerLoginResponse { + enum LoginStatus { + SUCCESS, FAIL, DELETED, ERROR + } + + @NonNull + private LoginStatus result; + private OwnerDTO ownerInfo; + + private static final OwnerLoginResponse FAIL = new OwnerLoginResponse(LoginStatus.FAIL); + private static final OwnerLoginResponse DELETED = new OwnerLoginResponse(LoginStatus.DELETED); + + private static OwnerLoginResponse success(OwnerDTO ownerInfo) { + return new OwnerLoginResponse(LoginStatus.SUCCESS, ownerInfo); + } + + } + + @Getter + @RequiredArgsConstructor + private static class UpdateOwnerMailAndTelResponse { + enum UpdateStatus { + SUCCESS, NO_LOGIN, EMPTY_MAIL, EMPTY_TEL + } + + @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_MAIL = + new UpdateOwnerMailAndTelResponse(UpdateStatus.EMPTY_MAIL); + private static final UpdateOwnerMailAndTelResponse EMPTY_TEL = + new UpdateOwnerMailAndTelResponse(UpdateStatus.EMPTY_TEL); + } + + @Getter + @RequiredArgsConstructor + private static class UpdateOwnerPasswordResponse { + enum UpdateStatus { + SUCCESS, NO_LOGIN, 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); + } + + + @Getter + @RequiredArgsConstructor + private static class logoutResponse { + enum logoutStatus { + SUCCESS, NO_LOGIN + } + + @NonNull + private logoutStatus result; + + private static final logoutResponse SUCCESS = new logoutResponse(logoutStatus.SUCCESS); + private static final logoutResponse NO_LOGIN = new logoutResponse(logoutStatus.NO_LOGIN); + + } + } + + diff --git a/src/main/java/com/delfood/dto/OwnerDTO.java b/src/main/java/com/delfood/dto/OwnerDTO.java index 329fd9c..8328e09 100644 --- a/src/main/java/com/delfood/dto/OwnerDTO.java +++ b/src/main/java/com/delfood/dto/OwnerDTO.java @@ -6,14 +6,14 @@ import lombok.Setter; import lombok.ToString; -@Getter +@Getter @Setter @ToString public class OwnerDTO { public enum Status { DEFAULT, DELETED } - + @NonNull private String id; @NonNull @@ -27,6 +27,6 @@ public enum Status { private LocalDateTime createdAt; private LocalDateTime updatedAt; private Status status; - - + + } diff --git a/src/main/java/com/delfood/mapper/OwnerMapper.java b/src/main/java/com/delfood/mapper/OwnerMapper.java index 89dfb74..ae44d52 100644 --- a/src/main/java/com/delfood/mapper/OwnerMapper.java +++ b/src/main/java/com/delfood/mapper/OwnerMapper.java @@ -1,8 +1,19 @@ package com.delfood.mapper; +import com.delfood.dto.OwnerDTO; + import org.springframework.stereotype.Repository; @Repository public interface OwnerMapper { + OwnerDTO findByIdAndPassword(String id, String password); + + OwnerDTO findById(String id); + + int updatePassword(String id, String password); + + int updateMailAndTel(String id, String mail, String tel); + + } diff --git a/src/main/java/com/delfood/service/OwnerService.java b/src/main/java/com/delfood/service/OwnerService.java index d7810cf..045f660 100644 --- a/src/main/java/com/delfood/service/OwnerService.java +++ b/src/main/java/com/delfood/service/OwnerService.java @@ -1,8 +1,80 @@ package com.delfood.service; +import com.delfood.dto.OwnerDTO; +import com.delfood.mapper.DMLOperationError; +import com.delfood.mapper.OwnerMapper; +import com.delfood.utils.SHA256Util; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; + @Service public class OwnerService { + @Autowired + private OwnerMapper ownerMapper; + + /** + * 사장님 로그인. + * + * @param id 아이디 + * @param password 패스워드 + * @return + */ + public OwnerDTO login(String id, String password) { + String cryptoPassword = SHA256Util.encryptSHA256(password); + OwnerDTO ownerInfo = ownerMapper.findByIdAndPassword(id, cryptoPassword); + return ownerInfo; + } + + /** + * 사장 정보 조회. + * + * @return id, name, mail, tel, createAt, updatedAt, status + */ + public OwnerDTO ownerInfo(String id) { + return ownerMapper.findById(id); + } + + /** + * 사장 이메일, 전화번호 수정. + * + * @param id 아이디 + * @param mail 변경할 이메일 + * @param tel 변경할 전화번호 + * + * @return + */ + public DMLOperationError updateOwnerMailAndTel(String id, String mail, String tel) { + int result = ownerMapper.updateMailAndTel(id, mail, tel); + if (result == 1) { + return DMLOperationError.SUCCESS; // 정상 수행 + } else if (result == 0) { + return DMLOperationError.NONE_CHANGED; // 데이터가 변경되지 않음 + } else { + return DMLOperationError.TOO_MANY_CHANGED; // 반복된 요청 + } + } + + /** + * 사장 비밀번호 수정. + * + * @param id 아이디 + * @param password 변경할 비밀번호 + * @return + */ + public DMLOperationError updateOwnerPassword(String id, String password) { + String cryptoPassword = SHA256Util.encryptSHA256(password); + int result = ownerMapper.updatePassword(id, cryptoPassword); + if (result == 1) { + return DMLOperationError.SUCCESS; + } else if (result == 0) { + return DMLOperationError.NONE_CHANGED; + } else { + return DMLOperationError.TOO_MANY_CHANGED; + } + + } + + } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b48434..80f1f71 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,19 +1,19 @@ # Server server.port=80 -# DB -spring.datasource.driver-class-name=org.mariadb.jdbc.Driver -mybatis.configuration.map-underscore-to-camel-case=true - # profile 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.lettuce.pool.max-active=10 -spring.redis.lettuce.pool.max-idle=10 -spring.redis.lettuce.pool.min-idle=2 +spring.redis.host=localhost spring.redis.port=6379 -spring.redis.host=127.0.0.1 - -# session -spring.session.store-type=redis \ No newline at end of file +spring.redis.password= \ No newline at end of file diff --git a/src/main/resources/mybatis/mapper/owner.xml b/src/main/resources/mybatis/mapper/owner.xml index ee5747b..03857f5 100644 --- a/src/main/resources/mybatis/mapper/owner.xml +++ b/src/main/resources/mybatis/mapper/owner.xml @@ -1,5 +1,32 @@ - + + + + + + + UPDATE OWNER + SET password = #{password} + WHERE id = #{id} + + + + UPDATE OWNER + SET mail = #{mail}, + tel = #{tel}, + updated_at = NOW() + WHERE id = #{id} + + \ No newline at end of file From 135495b64ed8aa7cc19ccade524b4891c2336d19 Mon Sep 17 00:00:00 2001 From: binaryyoung Date: Wed, 16 Oct 2019 20:52:10 +0900 Subject: [PATCH 2/3] =?UTF-8?q?-=20@Alias=20=EC=96=B4=EB=85=B8=ED=85=8C?= =?UTF-8?q?=EC=9D=B4=EC=85=98=EC=9D=84=20=EC=9D=B4=EC=9A=A9=ED=95=B4=20typ?= =?UTF-8?q?eAlias=EB=A5=BC=20=EC=82=AC=EC=9A=A9=ED=95=A0=20=EC=88=98=20?= =?UTF-8?q?=EC=9E=88=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD=ED=95=98?= =?UTF-8?q?=EC=98=80=EC=8A=B5=EB=8B=88=EB=8B=A4.=20-=20=EB=B9=84=EB=B0=80?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=EB=B3=80=EA=B2=BD=20=EC=8B=9C=20=EC=95=84?= =?UTF-8?q?=EC=9D=B4=EB=94=94=EC=99=80=20=EA=B8=B0=EC=A1=B4=EB=B9=84?= =?UTF-8?q?=EB=B0=80=EB=B2=88=ED=98=B8=EA=B0=80=20=EC=9D=BC=EC=B9=98?= =?UTF-8?q?=ED=95=98=EC=A7=80=20=EC=95=8A=EC=9C=BC=EB=A9=B4=20UNATHORIZED?= =?UTF-8?q?=EB=A5=BC=20=EB=B3=B4=EB=82=B4=EB=8F=84=EB=A1=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=ED=95=98=EC=98=80=EC=8A=B5=EB=8B=88=EB=8B=A4.=20-=20?= =?UTF-8?q?=EC=9D=B4=EB=A9=94=EC=9D=BC,=EC=A3=BC=EC=86=8C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20=EC=8B=9C=20=ED=95=98=EB=82=98=EB=A7=8C=20null?= =?UTF-8?q?=EC=9D=B8=EA=B2=BD=EC=9A=B0=EC=97=90=EB=8A=94=20=ED=95=98?= =?UTF-8?q?=EB=82=98=EB=A7=8C=20=EC=88=98=EC=A0=95=EC=9D=B4=20=EB=90=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD=ED=95=98=EC=98=80?= =?UTF-8?q?=EC=8A=B5=EB=8B=88=EB=8B=A4.=20-=20=EC=88=98=EC=A0=95=EC=9E=91?= =?UTF-8?q?=EC=97=85=EC=9D=B4=20=EC=A0=95=EC=83=81=EC=A0=81=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=A7=84=ED=96=89=EB=90=98=EA=B1=B0=EB=82=98=20?= =?UTF-8?q?=EC=95=84=EC=98=88=20=EC=9D=BC=EC=96=B4=EB=82=98=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EC=9D=80=20=EA=B2=BD=EC=9A=B0=EB=A5=BC=20=EC=A0=9C?= =?UTF-8?q?=EC=99=B8=ED=95=98=EA=B3=A0=EB=8A=94=20=EB=A1=A4=EB=B0=B1?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=EB=90=98=EB=8F=84=EB=A1=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=ED=95=98=EC=98=80=EC=8A=B5=EB=8B=88=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/delfood/config/DatabaseConfig.java | 2 ++ .../com/delfood/controller/OwnerController.java | 16 +++++++--------- src/main/java/com/delfood/dto/OwnerDTO.java | 6 +++++- .../java/com/delfood/mapper/MemberMapper.java | 1 + .../java/com/delfood/service/OwnerService.java | 9 +++++++-- src/main/resources/mybatis/mapper/owner.xml | 11 ++++++++--- 6 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/delfood/config/DatabaseConfig.java b/src/main/java/com/delfood/config/DatabaseConfig.java index 7270e28..b7fd830 100644 --- a/src/main/java/com/delfood/config/DatabaseConfig.java +++ b/src/main/java/com/delfood/config/DatabaseConfig.java @@ -19,6 +19,8 @@ public class DatabaseConfig { public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); + // TypeAlias로 설정할 클래스들이 있는 패키지를 설정하면 DTO에 @Alias("aliasName")으로 typeAlias를 설정 가능 + sessionFactory.setTypeAliasesPackage("com.delfood.dto."); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sessionFactory.setMapperLocations(resolver.getResources("classpath:mybatis/mapper/*.xml")); return sessionFactory.getObject(); diff --git a/src/main/java/com/delfood/controller/OwnerController.java b/src/main/java/com/delfood/controller/OwnerController.java index a2a693e..d21c1b1 100644 --- a/src/main/java/com/delfood/controller/OwnerController.java +++ b/src/main/java/com/delfood/controller/OwnerController.java @@ -73,11 +73,11 @@ public ResponseEntity logout(HttpSession session) { String id = (String) session.getAttribute("LOGIN_OWNER_ID"); if (id != null) { session.invalidate(); - return new ResponseEntity( - logoutResponse.SUCCESS, HttpStatus.OK); + return new ResponseEntity(logoutResponse.SUCCESS, + HttpStatus.OK); } else { - return new ResponseEntity( - logoutResponse.NO_LOGIN, HttpStatus.UNAUTHORIZED); + return new ResponseEntity(logoutResponse.NO_LOGIN, + HttpStatus.UNAUTHORIZED); } } @@ -242,7 +242,7 @@ private static OwnerLoginResponse success(OwnerDTO ownerInfo) { @RequiredArgsConstructor private static class UpdateOwnerMailAndTelResponse { enum UpdateStatus { - SUCCESS, NO_LOGIN, EMPTY_MAIL, EMPTY_TEL + SUCCESS, NO_LOGIN, EMPTY_CONTENT } @NonNull @@ -252,10 +252,8 @@ enum UpdateStatus { new UpdateOwnerMailAndTelResponse(UpdateStatus.SUCCESS); private static final UpdateOwnerMailAndTelResponse NO_LOGIN = new UpdateOwnerMailAndTelResponse(UpdateStatus.NO_LOGIN); - private static final UpdateOwnerMailAndTelResponse EMPTY_MAIL = - new UpdateOwnerMailAndTelResponse(UpdateStatus.EMPTY_MAIL); - private static final UpdateOwnerMailAndTelResponse EMPTY_TEL = - new UpdateOwnerMailAndTelResponse(UpdateStatus.EMPTY_TEL); + private static final UpdateOwnerMailAndTelResponse EMPTY_CONTENT = + new UpdateOwnerMailAndTelResponse(UpdateStatus.EMPTY_CONTENT); } @Getter diff --git a/src/main/java/com/delfood/dto/OwnerDTO.java b/src/main/java/com/delfood/dto/OwnerDTO.java index 8328e09..109037a 100644 --- a/src/main/java/com/delfood/dto/OwnerDTO.java +++ b/src/main/java/com/delfood/dto/OwnerDTO.java @@ -1,14 +1,18 @@ package com.delfood.dto; import java.time.LocalDateTime; + import lombok.Getter; import lombok.NonNull; import lombok.Setter; import lombok.ToString; +import org.apache.ibatis.type.Alias; + @Getter @Setter @ToString +@Alias("Owner") public class OwnerDTO { public enum Status { DEFAULT, DELETED @@ -29,4 +33,4 @@ public enum Status { private Status status; -} +} \ No newline at end of file diff --git a/src/main/java/com/delfood/mapper/MemberMapper.java b/src/main/java/com/delfood/mapper/MemberMapper.java index c3e7524..95cfb7e 100644 --- a/src/main/java/com/delfood/mapper/MemberMapper.java +++ b/src/main/java/com/delfood/mapper/MemberMapper.java @@ -21,4 +21,5 @@ public interface MemberMapper { int updateMemberAddress(String id, String address, String addressDetail); int idCheck(String id); + } diff --git a/src/main/java/com/delfood/service/OwnerService.java b/src/main/java/com/delfood/service/OwnerService.java index 045f660..c404ef5 100644 --- a/src/main/java/com/delfood/service/OwnerService.java +++ b/src/main/java/com/delfood/service/OwnerService.java @@ -4,8 +4,11 @@ import com.delfood.mapper.DMLOperationError; import com.delfood.mapper.OwnerMapper; import com.delfood.utils.SHA256Util; +import javax.management.RuntimeErrorException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.interceptor.RollbackRuleAttribute; @Service @@ -45,6 +48,7 @@ public OwnerDTO ownerInfo(String id) { * * @return */ + @Transactional(rollbackFor = RuntimeException.class) public DMLOperationError updateOwnerMailAndTel(String id, String mail, String tel) { int result = ownerMapper.updateMailAndTel(id, mail, tel); if (result == 1) { @@ -52,7 +56,7 @@ public DMLOperationError updateOwnerMailAndTel(String id, String mail, String te } else if (result == 0) { return DMLOperationError.NONE_CHANGED; // 데이터가 변경되지 않음 } else { - return DMLOperationError.TOO_MANY_CHANGED; // 반복된 요청 + throw new RuntimeException("password update error : " + DMLOperationError.TOO_MANY_CHANGED); } } @@ -63,6 +67,7 @@ public DMLOperationError updateOwnerMailAndTel(String id, String mail, String te * @param password 변경할 비밀번호 * @return */ + @Transactional(rollbackFor = RuntimeException.class) // runtimeException이 발생하면 rollback을 수행한다. public DMLOperationError updateOwnerPassword(String id, String password) { String cryptoPassword = SHA256Util.encryptSHA256(password); int result = ownerMapper.updatePassword(id, cryptoPassword); @@ -71,7 +76,7 @@ public DMLOperationError updateOwnerPassword(String id, String password) { } else if (result == 0) { return DMLOperationError.NONE_CHANGED; } else { - return DMLOperationError.TOO_MANY_CHANGED; + throw new RuntimeException("password update error : " + DMLOperationError.TOO_MANY_CHANGED); } } diff --git a/src/main/resources/mybatis/mapper/owner.xml b/src/main/resources/mybatis/mapper/owner.xml index 03857f5..ff6389c 100644 --- a/src/main/resources/mybatis/mapper/owner.xml +++ b/src/main/resources/mybatis/mapper/owner.xml @@ -2,14 +2,14 @@ - SELECT id, password, name, mail, tel, created_at createdAt, updated_at updatedAt, status FROM OWNER WHERE id = #{id} AND password = #{password} - SELECT id, password, name, mail, tel, created_at createdAt, updated_at updatedAt, status FROM OWNER WHERE id = #{id} @@ -23,8 +23,13 @@ UPDATE OWNER - SET mail = #{mail}, + SET + + mail = #{mail}, + + tel = #{tel}, + updated_at = NOW() WHERE id = #{id} From 8b61187dbfac3b7e3e5611534a4d361904e23bd9 Mon Sep 17 00:00:00 2001 From: binaryyoung Date: Thu, 17 Oct 2019 18:16:15 +0900 Subject: [PATCH 3/3] =?UTF-8?q?-=20DB=20=EA=B4=80=EB=A0=A8=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - controller의 login 메서드가 에러를 throw하지 않도록 변경 - 사장 정보 수정 시 Patch 방식을 이용하도록 변경 - 메일, 전화번호 수정 메서드에 비밀번호가 맞아야 수정할 수 있도록 변경 - 비밀번호 수정 시 기존 비밀번호와 새 비밀번호 NPE 방지 조건 수정 - 사장 업데이트 Response 클래스 하나로 통합 - OwnerService 로그인 메서드 사장 정보 조회 메서드로 변경 --- .../delfood/controller/OwnerController.java | 152 ++++++++---------- .../com/delfood/service/OwnerService.java | 9 +- src/main/resources/application.properties | 13 +- 3 files changed, 77 insertions(+), 97 deletions(-) diff --git a/src/main/java/com/delfood/controller/OwnerController.java b/src/main/java/com/delfood/controller/OwnerController.java index d21c1b1..d7f7597 100644 --- a/src/main/java/com/delfood/controller/OwnerController.java +++ b/src/main/java/com/delfood/controller/OwnerController.java @@ -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; @@ -38,7 +39,7 @@ public class OwnerController { @PostMapping("login") public ResponseEntity 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 responseEntity; @@ -46,17 +47,17 @@ public ResponseEntity login(@RequestBody OwnerLoginRequest l ownerLoginResponse = OwnerLoginResponse.FAIL; responseEntity = new ResponseEntity(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, HttpStatus.OK); - } else if (Status.DELETED.equals(ownerInfo.getStatus())) { // 삭제된 계정일 때 - ownerLoginResponse = OwnerLoginResponse.DELETED; - responseEntity = new ResponseEntity(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, HttpStatus.OK); + } else { + ownerLoginResponse = OwnerLoginResponse.DELETED; + responseEntity = new ResponseEntity(ownerLoginResponse, + HttpStatus.UNAUTHORIZED); + } } return responseEntity; } @@ -95,7 +96,7 @@ public ResponseEntity ownerInfo(HttpSession session) { if (id == null) { responseEntity = new ResponseEntity(HttpStatus.UNAUTHORIZED); } else { - OwnerDTO ownerInfo = ownerService.ownerInfo(id); + OwnerDTO ownerInfo = ownerService.getOwner(id); responseEntity = new ResponseEntity(ownerInfo, HttpStatus.OK); } return responseEntity; @@ -108,36 +109,38 @@ public ResponseEntity ownerInfo(HttpSession session) { * @param session 현재 사용자 세션 * @return */ - @PutMapping - public ResponseEntity updateOwnerInfo( + @PatchMapping + public ResponseEntity 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 responseEntity; - - if (mail == null) { - responseEntity = new ResponseEntity( - UpdateOwnerMailAndTelResponse.EMPTY_MAIL, HttpStatus.BAD_REQUEST); - } else if (tel == null) { - responseEntity = new ResponseEntity( - UpdateOwnerMailAndTelResponse.EMPTY_TEL, HttpStatus.BAD_REQUEST); - } else if (id == null) { - responseEntity = new ResponseEntity( - UpdateOwnerMailAndTelResponse.NO_LOGIN, HttpStatus.UNAUTHORIZED); - } else { - DMLOperationError dmlOperationError = ownerService.updateOwnerMailAndTel(id, mail, tel); - if (dmlOperationError == DMLOperationError.SUCCESS) { - responseEntity = new ResponseEntity( - 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( + UpdateOwnerResponse.NO_LOGIN, HttpStatus.UNAUTHORIZED); + } + + if (ownerService.getOwner(id, password) == null) { + return new ResponseEntity( + UpdateOwnerResponse.PASSWORD_MISMATCH, HttpStatus.UNAUTHORIZED); + } + + if (mail == null && tel == null) { // 변경하려는 정보가 둘 다 null일 경우 + return new ResponseEntity( + UpdateOwnerResponse.EMPTY_CONTENT, HttpStatus.BAD_REQUEST); + } + + DMLOperationError dmlOperationError = ownerService.updateOwnerMailAndTel(id, mail, tel); + if (dmlOperationError == DMLOperationError.SUCCESS) { + return new ResponseEntity( + 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; } /** @@ -147,38 +150,39 @@ public ResponseEntity updateOwnerInfo( * @param session 현재 사용자의 세션 * @return */ - @PutMapping("password") - public ResponseEntity updatePassword( + @PatchMapping("password") + public ResponseEntity updatePassword( @RequestBody UpdateOwnerPasswordRequest passwordResquest, HttpSession session) { String id = (String) session.getAttribute("LOGIN_OWNER_ID"); String password = passwordResquest.getPassword(); String newPassword = passwordResquest.getNewPassword(); - ResponseEntity responseEntity; + ResponseEntity responseEntity; if (id == null) { // 비 로그인 상태 - responseEntity = new ResponseEntity( - UpdateOwnerPasswordResponse.NO_LOGIN, HttpStatus.UNAUTHORIZED); - } else if (ownerService.login(id, password) == null) { // 아이디와 비밀번호 불일치 - responseEntity = new ResponseEntity( - UpdateOwnerPasswordResponse.PASSWORD_MISMATCH, HttpStatus.BAD_REQUEST); - } else if (newPassword == null) { - responseEntity = new ResponseEntity( - UpdateOwnerPasswordResponse.EMPTY_PASSOWRD, HttpStatus.BAD_REQUEST); + responseEntity = new ResponseEntity( + UpdateOwnerResponse.NO_LOGIN, HttpStatus.UNAUTHORIZED); + } else if (password == null || newPassword == null) { // 비밀번호나 새 비밀번호를 입력하지 않은 경우 + responseEntity = new ResponseEntity( + UpdateOwnerResponse.EMPTY_PASSOWRD, HttpStatus.BAD_REQUEST); + } else if (ownerService.getOwner(id, password) == null) { // 아이디와 비밀번호 불일치 + responseEntity = new ResponseEntity( + UpdateOwnerResponse.PASSWORD_MISMATCH, HttpStatus.UNAUTHORIZED); } else if (password.equals(newPassword)) { // 이전 패스워드와 동일한 경우 - responseEntity = new ResponseEntity( - UpdateOwnerPasswordResponse.PASSWORD_DUPLICATED, HttpStatus.CONFLICT); + responseEntity = new ResponseEntity( + UpdateOwnerResponse.PASSWORD_DUPLICATED, HttpStatus.CONFLICT); } else { DMLOperationError dmlOperationError = ownerService.updateOwnerPassword(id, newPassword); if (DMLOperationError.SUCCESS.equals(dmlOperationError)) { - responseEntity = new ResponseEntity( - UpdateOwnerPasswordResponse.SUCCESS, HttpStatus.OK); + responseEntity = new ResponseEntity( + UpdateOwnerResponse.SUCCESS, HttpStatus.OK); } else { log.error("Password Update Error {}", passwordResquest); throw new RuntimeException("Password Update Error"); } + } return responseEntity; } @@ -199,6 +203,8 @@ private static class OwnerLoginRequest { @Setter @Getter private static class UpdateOwnerMailAndTelRequest { + @NonNull + private String password; @NonNull private String mail; @NonNull @@ -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); } diff --git a/src/main/java/com/delfood/service/OwnerService.java b/src/main/java/com/delfood/service/OwnerService.java index c404ef5..6af5da3 100644 --- a/src/main/java/com/delfood/service/OwnerService.java +++ b/src/main/java/com/delfood/service/OwnerService.java @@ -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; @@ -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); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 80f1f71..e9dd540 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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= \ No newline at end of file +spring.session.store-type=redis \ No newline at end of file