From 4ed429426318ed8b86b8eec53b7a72c77fc6cd3c Mon Sep 17 00:00:00 2001 From: Yunkeun Date: Mon, 23 May 2022 16:39:52 +0900 Subject: [PATCH 1/7] =?UTF-8?q?style:=20Email=20->=20Mail=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/email/domain/EmailRegex.java | 15 ----- .../domain/email/exception/MailException.java | 58 ------------------ .../domain/email/service/MailService.java | 61 ------------------- .../controller/MailController.java | 10 +-- .../domain/ExceptionMessage.java | 6 +- .../domain/MailMessage.java} | 4 +- .../backend/domain/mail/domain/MailRegex.java | 15 +++++ .../dto/Email.java => mail/dto/Mail.java} | 4 +- .../{email => mail}/dto/MailRequest.java | 4 +- .../{email => mail}/dto/MailResponse.java | 4 +- .../domain/mail/exception/MailException.java | 58 ++++++++++++++++++ .../domain/mail/service/MailService.java | 61 +++++++++++++++++++ .../hjjang/backend/email/MailServiceTest.java | 14 ++--- 13 files changed, 157 insertions(+), 157 deletions(-) delete mode 100644 backend/src/main/java/com/hjjang/backend/domain/email/domain/EmailRegex.java delete mode 100644 backend/src/main/java/com/hjjang/backend/domain/email/exception/MailException.java delete mode 100644 backend/src/main/java/com/hjjang/backend/domain/email/service/MailService.java rename backend/src/main/java/com/hjjang/backend/domain/{email => mail}/controller/MailController.java (77%) rename backend/src/main/java/com/hjjang/backend/domain/{email => mail}/domain/ExceptionMessage.java (57%) rename backend/src/main/java/com/hjjang/backend/domain/{email/domain/EmailMessage.java => mail/domain/MailMessage.java} (82%) create mode 100644 backend/src/main/java/com/hjjang/backend/domain/mail/domain/MailRegex.java rename backend/src/main/java/com/hjjang/backend/domain/{email/dto/Email.java => mail/dto/Mail.java} (89%) rename backend/src/main/java/com/hjjang/backend/domain/{email => mail}/dto/MailRequest.java (67%) rename backend/src/main/java/com/hjjang/backend/domain/{email => mail}/dto/MailResponse.java (63%) create mode 100644 backend/src/main/java/com/hjjang/backend/domain/mail/exception/MailException.java create mode 100644 backend/src/main/java/com/hjjang/backend/domain/mail/service/MailService.java diff --git a/backend/src/main/java/com/hjjang/backend/domain/email/domain/EmailRegex.java b/backend/src/main/java/com/hjjang/backend/domain/email/domain/EmailRegex.java deleted file mode 100644 index 1bcb11c..0000000 --- a/backend/src/main/java/com/hjjang/backend/domain/email/domain/EmailRegex.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.hjjang.backend.domain.email.domain; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -@Getter -@AllArgsConstructor -public enum EmailRegex { - UNIVERSITY("UNIVERSITY_EMAIL_REGEX", "^[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.+a+c+\\.+k+r+$"), - GS_UNIVERSITY("GS_UNIVERSITY_EMAIL_REGEX", "^[a-zA-Z0-9]+@+g+s+\\.+[a-zA-Z0-9]+\\.+a+c+\\.+k+r+$"), - EMAIL_PARSE("EMAIL_PARSE_REGEX", "[@.]"); - - private final String name; - private final String regex; -} diff --git a/backend/src/main/java/com/hjjang/backend/domain/email/exception/MailException.java b/backend/src/main/java/com/hjjang/backend/domain/email/exception/MailException.java deleted file mode 100644 index 5b0e864..0000000 --- a/backend/src/main/java/com/hjjang/backend/domain/email/exception/MailException.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.hjjang.backend.domain.email.exception; - -import com.hjjang.backend.domain.email.dto.Email; -import com.hjjang.backend.domain.email.domain.EmailRegex; -import com.hjjang.backend.domain.email.domain.ExceptionMessage; -import com.hjjang.backend.domain.email.dto.MailRequest; -import java.util.regex.Pattern; - -public class MailException extends RuntimeException { - - private MailException() { - } - - public static void checkEmailPossible(String email) { - checkEmailDuplicate(email); - if (!isUniversityEmail(email)) { - ExceptionMessage invalidEmail = ExceptionMessage.INVALID_EMAIL; - throw new RuntimeException(invalidEmail.getMessage()); - } - } - - public static void checkRequest(MailRequest mailRequest, Email email) { - checkValidCode(mailRequest, email.getCode()); - checkValidRequestEmail(mailRequest, email.getAddress()); - } - - private static void checkValidRequestEmail(MailRequest mailRequest, String email) { - if (!mailRequest.getEmail().equals(email)) { - ExceptionMessage invalidRequestEmail = ExceptionMessage.INVALID_REQUEST_EMAIL; - throw new RuntimeException(invalidRequestEmail.getMessage()); - } - } - - private static void checkValidCode(MailRequest mailRequest, String code) { - checkTimeLimit(code); - if (!mailRequest.getCode().equals(code)) { - ExceptionMessage invalidCode = ExceptionMessage.INVALID_CODE; - throw new RuntimeException(invalidCode.getMessage()); - } - } - - private static void checkTimeLimit(String code) { - // 시간 지난 만큼을 인자로 받아 넘었을 경우 exception - } - - private static boolean isUniversityEmail(String email) { - EmailRegex university = EmailRegex.UNIVERSITY; - EmailRegex gsUniversity = EmailRegex.GS_UNIVERSITY; - if (Pattern.matches(university.getRegex(), email)) { - return true; - } - return Pattern.matches(gsUniversity.getRegex(), email); - } - - private static void checkEmailDuplicate(String email) { - // 모든 유저에 저장되어 있는 email과 겹친다면 exception - } -} diff --git a/backend/src/main/java/com/hjjang/backend/domain/email/service/MailService.java b/backend/src/main/java/com/hjjang/backend/domain/email/service/MailService.java deleted file mode 100644 index 6beb0d8..0000000 --- a/backend/src/main/java/com/hjjang/backend/domain/email/service/MailService.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.hjjang.backend.domain.email.service; - -import com.hjjang.backend.domain.email.dto.Email; -import com.hjjang.backend.domain.email.domain.EmailMessage; -import com.hjjang.backend.domain.email.domain.EmailRegex; -import com.hjjang.backend.domain.email.dto.MailRequest; -import com.hjjang.backend.domain.email.dto.MailResponse; -import com.hjjang.backend.domain.email.exception.MailException; -import java.util.List; -import lombok.RequiredArgsConstructor; -import org.springframework.mail.SimpleMailMessage; -import org.springframework.mail.javamail.JavaMailSender; -import org.springframework.stereotype.Service; - -@Service -@RequiredArgsConstructor -public class MailService { - - private final JavaMailSender javaMailSender; - private final Email email = new Email(); - - public MailResponse sendMail(String emailAddress) { - MailException.checkEmailPossible(emailAddress); - SimpleMailMessage message = new SimpleMailMessage(); - setMessage(message, emailAddress); - javaMailSender.send(message); - return new MailResponse(email); - } - - public MailResponse checkCode(MailRequest mailRequest) { - MailException.checkRequest(mailRequest, email); - email.setIsAuth(true); - return new MailResponse(email); - } - - private String parseUniversity(String email) { - EmailRegex emailParse = EmailRegex.EMAIL_PARSE; - String regex = emailParse.getRegex(); - List splitEmail = List.of(email.split(regex)); - int universityIndex = splitEmail.lastIndexOf("ac") - 1; - return splitEmail.get(universityIndex); - } - - private void setMessage(SimpleMailMessage message, String emailAddress) { - message.setTo(emailAddress); - EmailMessage title = EmailMessage.TITLE; - message.setSubject(title.getContent()); - saveEmailInfo(emailAddress); - EmailMessage emailMessage = EmailMessage.MESSAGE; - message.setText(emailMessage.getContent() + email.getCode()); - } - - private void saveEmailInfo(String emailAddress) { - String code = email.createRandomCode(); - email.setCode(code); - email.setAddress(emailAddress); - String university = parseUniversity(emailAddress); - email.setUniversity(university); - email.setIsAuth(false); - } -} diff --git a/backend/src/main/java/com/hjjang/backend/domain/email/controller/MailController.java b/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailController.java similarity index 77% rename from backend/src/main/java/com/hjjang/backend/domain/email/controller/MailController.java rename to backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailController.java index 99ee783..cb0cc5b 100644 --- a/backend/src/main/java/com/hjjang/backend/domain/email/controller/MailController.java +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailController.java @@ -1,8 +1,8 @@ -package com.hjjang.backend.domain.email.controller; +package com.hjjang.backend.domain.mail.controller; -import com.hjjang.backend.domain.email.dto.MailRequest; -import com.hjjang.backend.domain.email.dto.MailResponse; -import com.hjjang.backend.domain.email.service.MailService; +import com.hjjang.backend.domain.mail.dto.MailRequest; +import com.hjjang.backend.domain.mail.dto.MailResponse; +import com.hjjang.backend.domain.mail.service.MailService; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; @@ -19,7 +19,7 @@ public class MailController { @PostMapping("/auth") public ResponseEntity send(@RequestBody MailRequest mailRequest) { - String email = mailRequest.getEmail(); + String email = mailRequest.getMail(); MailResponse mailResponse = mailService.sendMail(email); return ResponseEntity.ok().body(mailResponse); } diff --git a/backend/src/main/java/com/hjjang/backend/domain/email/domain/ExceptionMessage.java b/backend/src/main/java/com/hjjang/backend/domain/mail/domain/ExceptionMessage.java similarity index 57% rename from backend/src/main/java/com/hjjang/backend/domain/email/domain/ExceptionMessage.java rename to backend/src/main/java/com/hjjang/backend/domain/mail/domain/ExceptionMessage.java index ba3aab4..efd44d0 100644 --- a/backend/src/main/java/com/hjjang/backend/domain/email/domain/ExceptionMessage.java +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/domain/ExceptionMessage.java @@ -1,4 +1,4 @@ -package com.hjjang.backend.domain.email.domain; +package com.hjjang.backend.domain.mail.domain; import lombok.AllArgsConstructor; import lombok.Getter; @@ -6,9 +6,9 @@ @Getter @AllArgsConstructor public enum ExceptionMessage { - INVALID_EMAIL("알맞은 대학교 이메일 계정으로 입력해주세요."), + INVALID_MAIL("알맞은 대학교 메일 계정으로 입력해주세요."), INVALID_CODE("잘못된 코드 번호가 입력되었습니다."), - INVALID_REQUEST_EMAIL("잘못된 요청의 이메일입니다."), + INVALID_REQUEST_MAIL("잘못된 요청의 메일입니다."), TIME_LIMIT("시간이 초과되었습니다."); private final String message; diff --git a/backend/src/main/java/com/hjjang/backend/domain/email/domain/EmailMessage.java b/backend/src/main/java/com/hjjang/backend/domain/mail/domain/MailMessage.java similarity index 82% rename from backend/src/main/java/com/hjjang/backend/domain/email/domain/EmailMessage.java rename to backend/src/main/java/com/hjjang/backend/domain/mail/domain/MailMessage.java index 5144694..7651d6e 100644 --- a/backend/src/main/java/com/hjjang/backend/domain/email/domain/EmailMessage.java +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/domain/MailMessage.java @@ -1,11 +1,11 @@ -package com.hjjang.backend.domain.email.domain; +package com.hjjang.backend.domain.mail.domain; import lombok.AllArgsConstructor; import lombok.Getter; @Getter @AllArgsConstructor -public enum EmailMessage { +public enum MailMessage { TITLE("[Bauction] 인증번호를 발송했습니다."), MESSAGE("본인확인 인증 메일\n" + "이메일 인증을 진행해주세요.\n" diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/domain/MailRegex.java b/backend/src/main/java/com/hjjang/backend/domain/mail/domain/MailRegex.java new file mode 100644 index 0000000..927fb30 --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/domain/MailRegex.java @@ -0,0 +1,15 @@ +package com.hjjang.backend.domain.mail.domain; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public enum MailRegex { + UNIVERSITY("UNIVERSITY_MAIL_REGEX", "^[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.+a+c+\\.+k+r+$"), + GS_UNIVERSITY("GS_UNIVERSITY_MAIL_REGEX", "^[a-zA-Z0-9]+@+g+s+\\.+[a-zA-Z0-9]+\\.+a+c+\\.+k+r+$"), + MAIL_PARSE("MAIL_PARSE_REGEX", "[@.]"); + + private final String name; + private final String regex; +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/email/dto/Email.java b/backend/src/main/java/com/hjjang/backend/domain/mail/dto/Mail.java similarity index 89% rename from backend/src/main/java/com/hjjang/backend/domain/email/dto/Email.java rename to backend/src/main/java/com/hjjang/backend/domain/mail/dto/Mail.java index ec32990..2ed76e1 100644 --- a/backend/src/main/java/com/hjjang/backend/domain/email/dto/Email.java +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/dto/Mail.java @@ -1,4 +1,4 @@ -package com.hjjang.backend.domain.email.dto; +package com.hjjang.backend.domain.mail.dto; import java.util.Random; import lombok.AllArgsConstructor; @@ -10,7 +10,7 @@ @AllArgsConstructor @Getter @Setter -public class Email { +public class Mail { private String code; private String address; diff --git a/backend/src/main/java/com/hjjang/backend/domain/email/dto/MailRequest.java b/backend/src/main/java/com/hjjang/backend/domain/mail/dto/MailRequest.java similarity index 67% rename from backend/src/main/java/com/hjjang/backend/domain/email/dto/MailRequest.java rename to backend/src/main/java/com/hjjang/backend/domain/mail/dto/MailRequest.java index 29aad15..6917e7e 100644 --- a/backend/src/main/java/com/hjjang/backend/domain/email/dto/MailRequest.java +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/dto/MailRequest.java @@ -1,4 +1,4 @@ -package com.hjjang.backend.domain.email.dto; +package com.hjjang.backend.domain.mail.dto; import lombok.AllArgsConstructor; import lombok.Getter; @@ -7,7 +7,7 @@ @AllArgsConstructor public class MailRequest { - private String email; + private String mail; private String code; } diff --git a/backend/src/main/java/com/hjjang/backend/domain/email/dto/MailResponse.java b/backend/src/main/java/com/hjjang/backend/domain/mail/dto/MailResponse.java similarity index 63% rename from backend/src/main/java/com/hjjang/backend/domain/email/dto/MailResponse.java rename to backend/src/main/java/com/hjjang/backend/domain/mail/dto/MailResponse.java index 330b8e6..92e2e86 100644 --- a/backend/src/main/java/com/hjjang/backend/domain/email/dto/MailResponse.java +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/dto/MailResponse.java @@ -1,4 +1,4 @@ -package com.hjjang.backend.domain.email.dto; +package com.hjjang.backend.domain.mail.dto; import lombok.AllArgsConstructor; import lombok.Getter; @@ -7,5 +7,5 @@ @AllArgsConstructor public class MailResponse { - private Email email; + private Mail mail; } diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/exception/MailException.java b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/MailException.java new file mode 100644 index 0000000..9ec5635 --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/MailException.java @@ -0,0 +1,58 @@ +package com.hjjang.backend.domain.mail.exception; + +import com.hjjang.backend.domain.mail.dto.Mail; +import com.hjjang.backend.domain.mail.domain.MailRegex; +import com.hjjang.backend.domain.mail.domain.ExceptionMessage; +import com.hjjang.backend.domain.mail.dto.MailRequest; +import java.util.regex.Pattern; + +public class MailException extends RuntimeException { + + private MailException() { + } + + public static void checkPossibleMail(String mailAddress) { + checkMailDuplicate(mailAddress); + if (!isUniversityMail(mailAddress)) { + ExceptionMessage invalidMail = ExceptionMessage.INVALID_MAIL; + throw new RuntimeException(invalidMail.getMessage()); + } + } + + public static void checkRequest(MailRequest mailRequest, Mail mail) { + checkValidCode(mailRequest, mail.getCode()); + checkValidRequestMail(mailRequest, mail.getAddress()); + } + + private static void checkValidRequestMail(MailRequest mailRequest, String mailAddress) { + if (!mailRequest.getMail().equals(mailAddress)) { + ExceptionMessage invalidRequestMail = ExceptionMessage.INVALID_REQUEST_MAIL; + throw new RuntimeException(invalidRequestMail.getMessage()); + } + } + + private static void checkValidCode(MailRequest mailRequest, String code) { + checkTimeLimit(code); + if (!mailRequest.getCode().equals(code)) { + ExceptionMessage invalidCode = ExceptionMessage.INVALID_CODE; + throw new RuntimeException(invalidCode.getMessage()); + } + } + + private static void checkTimeLimit(String code) { + // 시간 지난 만큼을 인자로 받아 넘었을 경우 exception + } + + private static boolean isUniversityMail(String mailAddress) { + MailRegex university = MailRegex.UNIVERSITY; + MailRegex gsUniversity = MailRegex.GS_UNIVERSITY; + if (Pattern.matches(university.getRegex(), mailAddress)) { + return true; + } + return Pattern.matches(gsUniversity.getRegex(), mailAddress); + } + + private static void checkMailDuplicate(String mail) { + // 모든 유저에 저장되어 있는 email과 겹친다면 exception + } +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/service/MailService.java b/backend/src/main/java/com/hjjang/backend/domain/mail/service/MailService.java new file mode 100644 index 0000000..cbc54d6 --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/service/MailService.java @@ -0,0 +1,61 @@ +package com.hjjang.backend.domain.mail.service; + +import com.hjjang.backend.domain.mail.dto.Mail; +import com.hjjang.backend.domain.mail.domain.MailMessage; +import com.hjjang.backend.domain.mail.domain.MailRegex; +import com.hjjang.backend.domain.mail.dto.MailRequest; +import com.hjjang.backend.domain.mail.dto.MailResponse; +import com.hjjang.backend.domain.mail.exception.MailException; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class MailService { + + private final JavaMailSender javaMailSender; + private final Mail mail = new Mail(); + + public MailResponse sendMail(String mailAddress) { + MailException.checkPossibleMail(mailAddress); + SimpleMailMessage message = new SimpleMailMessage(); + setMessage(message, mailAddress); + javaMailSender.send(message); + return new MailResponse(mail); + } + + public MailResponse checkCode(MailRequest mailRequest) { + MailException.checkRequest(mailRequest, mail); + mail.setIsAuth(true); + return new MailResponse(mail); + } + + private String parseUniversity(String mailAddress) { + MailRegex mailParse = MailRegex.MAIL_PARSE; + String regex = mailParse.getRegex(); + List splitMail = List.of(mailAddress.split(regex)); + int universityIndex = splitMail.lastIndexOf("ac") - 1; + return splitMail.get(universityIndex); + } + + private void setMessage(SimpleMailMessage message, String mailAddress) { + message.setTo(mailAddress); + MailMessage title = MailMessage.TITLE; + message.setSubject(title.getContent()); + saveMailInfo(mailAddress); + MailMessage mailMessage = MailMessage.MESSAGE; + message.setText(mailMessage.getContent() + mail.getCode()); + } + + private void saveMailInfo(String mailAddress) { + String code = mail.createRandomCode(); + mail.setCode(code); + mail.setAddress(mailAddress); + String university = parseUniversity(mailAddress); + mail.setUniversity(university); + mail.setIsAuth(false); + } +} diff --git a/backend/src/test/java/com/hjjang/backend/email/MailServiceTest.java b/backend/src/test/java/com/hjjang/backend/email/MailServiceTest.java index fafac86..b51c3bb 100644 --- a/backend/src/test/java/com/hjjang/backend/email/MailServiceTest.java +++ b/backend/src/test/java/com/hjjang/backend/email/MailServiceTest.java @@ -3,10 +3,10 @@ import static org.assertj.core.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*; -import com.hjjang.backend.domain.email.dto.Email; -import com.hjjang.backend.domain.email.dto.MailRequest; -import com.hjjang.backend.domain.email.exception.MailException; -import com.hjjang.backend.domain.email.service.MailService; +import com.hjjang.backend.domain.mail.dto.Mail; +import com.hjjang.backend.domain.mail.dto.MailRequest; +import com.hjjang.backend.domain.mail.exception.MailException; +import com.hjjang.backend.domain.mail.service.MailService; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; @@ -24,7 +24,7 @@ void authEmail() { //given String emailAddress = "32174294@dankook.ac.kr"; String code = "123456"; - Email email = new Email(code, emailAddress, "dankook", false); + Mail email = new Mail(code, emailAddress, "dankook", false); //when MailRequest mailRequest = new MailRequest(emailAddress, code); MailException.checkRequest(mailRequest, email); @@ -88,7 +88,7 @@ void checkValidEmail() { trueEmails.add("dr0joon@gs.anyang.ac.kr"); //when //then - trueEmails.forEach(MailException::checkEmailPossible); + trueEmails.forEach(MailException::checkPossibleMail); } private JavaMailSenderImpl mailSender() { @@ -102,6 +102,6 @@ private JavaMailSenderImpl mailSender() { private static void isException(String email) { //then, when assertThrows(RuntimeException.class, () -> - MailException.checkEmailPossible(email)); + MailException.checkPossibleMail(email)); } } From 691f764036120ac5c1650e035d6599cf58edfe0f Mon Sep 17 00:00:00 2001 From: Yunkeun Date: Tue, 24 May 2022 13:09:07 +0900 Subject: [PATCH 2/7] =?UTF-8?q?refactor:=20=EC=9A=94=EC=B2=AD=20=EC=84=B1?= =?UTF-8?q?=EA=B3=B5=20=EC=8B=9C=20=EC=A0=81=EC=A0=88=ED=95=9C=20=EC=83=81?= =?UTF-8?q?=ED=83=9C=EC=BD=94=EB=93=9C=EB=A1=9C=20=EC=9D=91=EB=8B=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/mail/controller/MailController.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailController.java b/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailController.java index cb0cc5b..ee7c66b 100644 --- a/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailController.java +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailController.java @@ -1,8 +1,12 @@ package com.hjjang.backend.domain.mail.controller; +import static org.springframework.http.HttpStatus.*; +import static org.springframework.http.ResponseEntity.*; + import com.hjjang.backend.domain.mail.dto.MailRequest; import com.hjjang.backend.domain.mail.dto.MailResponse; import com.hjjang.backend.domain.mail.service.MailService; +import com.hjjang.backend.global.dto.ApiResponse; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; @@ -18,15 +22,16 @@ public class MailController { private final MailService mailService; @PostMapping("/auth") - public ResponseEntity send(@RequestBody MailRequest mailRequest) { - String email = mailRequest.getMail(); - MailResponse mailResponse = mailService.sendMail(email); - return ResponseEntity.ok().body(mailResponse); + public ResponseEntity send(@RequestBody MailRequest mailRequest) { + String mail = mailRequest.getMail(); + MailResponse mailResponse = mailService.sendMail(mail); + return status(CREATED) + .body(ApiResponse.success("send", mailResponse)); } @PostMapping("/check") - public ResponseEntity auth(@RequestBody MailRequest mailRequest) { + public ResponseEntity auth(@RequestBody MailRequest mailRequest) { MailResponse mailResponse = mailService.checkCode(mailRequest); - return ResponseEntity.ok().body(mailResponse); + return ok(ApiResponse.success("auth", mailResponse)); } } From 3aa26d36f91c7b6077e5350c3cf67ff65284119b Mon Sep 17 00:00:00 2001 From: Yunkeun Date: Tue, 24 May 2022 13:09:41 +0900 Subject: [PATCH 3/7] =?UTF-8?q?refactor:=20=EC=9A=94=EC=B2=AD=20=EC=8B=A4?= =?UTF-8?q?=ED=8C=A8=20=EC=8B=9C=20=EC=A0=81=EC=A0=88=ED=95=9C=20=EC=83=81?= =?UTF-8?q?=ED=83=9C=EC=BD=94=EB=93=9C=EC=99=80=20=EB=A9=94=EC=8B=9C?= =?UTF-8?q?=EC=A7=80=EB=A1=9C=20=EC=9D=91=EB=8B=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mail/controller/MailControllerAdvice.java | 30 +++++++++++++++++++ .../ExceptionMessage.java | 3 +- .../domain/mail/exception/MailException.java | 24 +++------------ .../mail/exception/TimeLimitException.java | 10 +++++++ .../mail/exception/UnauthorizedException.java | 17 +++++++++++ 5 files changed, 62 insertions(+), 22 deletions(-) create mode 100644 backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailControllerAdvice.java rename backend/src/main/java/com/hjjang/backend/domain/mail/{domain => exception}/ExceptionMessage.java (75%) create mode 100644 backend/src/main/java/com/hjjang/backend/domain/mail/exception/TimeLimitException.java create mode 100644 backend/src/main/java/com/hjjang/backend/domain/mail/exception/UnauthorizedException.java diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailControllerAdvice.java b/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailControllerAdvice.java new file mode 100644 index 0000000..89daf4f --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailControllerAdvice.java @@ -0,0 +1,30 @@ +package com.hjjang.backend.domain.mail.controller; + +import static org.springframework.http.HttpStatus.*; + +import com.hjjang.backend.domain.mail.exception.ExceptionMessage; +import com.hjjang.backend.domain.mail.exception.MailException; +import com.hjjang.backend.domain.mail.exception.TimeLimitException; +import com.hjjang.backend.domain.mail.exception.UnauthorizedException; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class MailControllerAdvice { + + @ExceptionHandler(value = MailException.class) + public ResponseEntity mailExceptionHandle() { + return ResponseEntity.status(BAD_REQUEST).body(ExceptionMessage.INVALID_MAIL.getMessage()); + } + + @ExceptionHandler(value = TimeLimitException.class) + public ResponseEntity timeLimitExceptionHandle() { + return ResponseEntity.status(REQUEST_TIMEOUT).body(ExceptionMessage.TIME_LIMIT.getMessage()); + } + + @ExceptionHandler(value = UnauthorizedException.class) + public ResponseEntity unauthorizedExceptionHandle() { + return ResponseEntity.status(UNAUTHORIZED).body(ExceptionMessage.INVALID_CODE.getMessage()); + } +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/domain/ExceptionMessage.java b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/ExceptionMessage.java similarity index 75% rename from backend/src/main/java/com/hjjang/backend/domain/mail/domain/ExceptionMessage.java rename to backend/src/main/java/com/hjjang/backend/domain/mail/exception/ExceptionMessage.java index efd44d0..097b3aa 100644 --- a/backend/src/main/java/com/hjjang/backend/domain/mail/domain/ExceptionMessage.java +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/ExceptionMessage.java @@ -1,4 +1,4 @@ -package com.hjjang.backend.domain.mail.domain; +package com.hjjang.backend.domain.mail.exception; import lombok.AllArgsConstructor; import lombok.Getter; @@ -8,7 +8,6 @@ public enum ExceptionMessage { INVALID_MAIL("알맞은 대학교 메일 계정으로 입력해주세요."), INVALID_CODE("잘못된 코드 번호가 입력되었습니다."), - INVALID_REQUEST_MAIL("잘못된 요청의 메일입니다."), TIME_LIMIT("시간이 초과되었습니다."); private final String message; diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/exception/MailException.java b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/MailException.java index 9ec5635..ab6f3f0 100644 --- a/backend/src/main/java/com/hjjang/backend/domain/mail/exception/MailException.java +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/MailException.java @@ -2,47 +2,31 @@ import com.hjjang.backend.domain.mail.dto.Mail; import com.hjjang.backend.domain.mail.domain.MailRegex; -import com.hjjang.backend.domain.mail.domain.ExceptionMessage; import com.hjjang.backend.domain.mail.dto.MailRequest; import java.util.regex.Pattern; public class MailException extends RuntimeException { - private MailException() { - } + private MailException() { } public static void checkPossibleMail(String mailAddress) { checkMailDuplicate(mailAddress); if (!isUniversityMail(mailAddress)) { - ExceptionMessage invalidMail = ExceptionMessage.INVALID_MAIL; - throw new RuntimeException(invalidMail.getMessage()); + throw new MailException(); } } public static void checkRequest(MailRequest mailRequest, Mail mail) { - checkValidCode(mailRequest, mail.getCode()); + UnauthorizedException.checkValidCode(mailRequest, mail.getCode()); checkValidRequestMail(mailRequest, mail.getAddress()); } private static void checkValidRequestMail(MailRequest mailRequest, String mailAddress) { if (!mailRequest.getMail().equals(mailAddress)) { - ExceptionMessage invalidRequestMail = ExceptionMessage.INVALID_REQUEST_MAIL; - throw new RuntimeException(invalidRequestMail.getMessage()); - } - } - - private static void checkValidCode(MailRequest mailRequest, String code) { - checkTimeLimit(code); - if (!mailRequest.getCode().equals(code)) { - ExceptionMessage invalidCode = ExceptionMessage.INVALID_CODE; - throw new RuntimeException(invalidCode.getMessage()); + throw new MailException(); } } - private static void checkTimeLimit(String code) { - // 시간 지난 만큼을 인자로 받아 넘었을 경우 exception - } - private static boolean isUniversityMail(String mailAddress) { MailRegex university = MailRegex.UNIVERSITY; MailRegex gsUniversity = MailRegex.GS_UNIVERSITY; diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/exception/TimeLimitException.java b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/TimeLimitException.java new file mode 100644 index 0000000..c253abd --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/TimeLimitException.java @@ -0,0 +1,10 @@ +package com.hjjang.backend.domain.mail.exception; + +public class TimeLimitException extends RuntimeException { + + private TimeLimitException() { } + + public static void checkTimeLimit(String code) { + // 시간 지난 만큼을 인자로 받아 넘었을 경우 exception + } +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/exception/UnauthorizedException.java b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/UnauthorizedException.java new file mode 100644 index 0000000..fb8a961 --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/UnauthorizedException.java @@ -0,0 +1,17 @@ +package com.hjjang.backend.domain.mail.exception; + +import static com.hjjang.backend.domain.mail.exception.TimeLimitException.*; + +import com.hjjang.backend.domain.mail.dto.MailRequest; + +public class UnauthorizedException extends RuntimeException { + + private UnauthorizedException() { } + + public static void checkValidCode(MailRequest mailRequest, String code) { + checkTimeLimit(code); + if (!mailRequest.getCode().equals(code)) { + throw new UnauthorizedException(); + } + } +} From c2244db1bc840c202730e2e7a3fff003d85fc497 Mon Sep 17 00:00:00 2001 From: 0BVer Date: Tue, 24 May 2022 22:25:55 +0900 Subject: [PATCH 4/7] feat : add trade entity --- .../trade/controller/TradeController.java | 78 +++++++++++++++++++ .../domain/trade/domain/entity/Trade.java | 54 +++++++++++++ .../domain/repositroy/TradeRepository.java | 7 ++ .../backend/domain/trade/dto/TradeMapper.java | 53 +++++++++++++ .../domain/trade/dto/TradeRequestDto.java | 22 ++++++ .../domain/trade/dto/TradeResponseDto.java | 21 +++++ .../exception/TradeNotFoundException.java | 21 +++++ .../domain/trade/service/TradeService.java | 50 ++++++++++++ .../global/response/code/ErrorCode.java | 8 +- .../global/response/code/SuccessCode.java | 10 ++- 10 files changed, 322 insertions(+), 2 deletions(-) create mode 100644 backend/src/main/java/com/hjjang/backend/domain/trade/controller/TradeController.java create mode 100644 backend/src/main/java/com/hjjang/backend/domain/trade/domain/entity/Trade.java create mode 100644 backend/src/main/java/com/hjjang/backend/domain/trade/domain/repositroy/TradeRepository.java create mode 100644 backend/src/main/java/com/hjjang/backend/domain/trade/dto/TradeMapper.java create mode 100644 backend/src/main/java/com/hjjang/backend/domain/trade/dto/TradeRequestDto.java create mode 100644 backend/src/main/java/com/hjjang/backend/domain/trade/dto/TradeResponseDto.java create mode 100644 backend/src/main/java/com/hjjang/backend/domain/trade/exception/TradeNotFoundException.java create mode 100644 backend/src/main/java/com/hjjang/backend/domain/trade/service/TradeService.java diff --git a/backend/src/main/java/com/hjjang/backend/domain/trade/controller/TradeController.java b/backend/src/main/java/com/hjjang/backend/domain/trade/controller/TradeController.java new file mode 100644 index 0000000..9b9778e --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/trade/controller/TradeController.java @@ -0,0 +1,78 @@ +package com.hjjang.backend.domain.trade.controller; + +import com.hjjang.backend.domain.trade.dto.TradeMapper; +import com.hjjang.backend.domain.trade.dto.TradeRequestDto; +import com.hjjang.backend.domain.trade.exception.TradeNotFoundException; +import com.hjjang.backend.domain.trade.service.TradeService; +import com.hjjang.backend.global.response.response.SuccessResponse; +import lombok.RequiredArgsConstructor; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static com.hjjang.backend.global.response.code.ErrorCode.*; +import static com.hjjang.backend.global.response.code.SuccessCode.*; +import static com.hjjang.backend.global.response.response.SuccessResponse.*; +import static org.springframework.http.HttpStatus.*; + +@RestController +@RequiredArgsConstructor +public class TradeController { + + private final TradeService service; + private final TradeMapper mapper; + + @PostMapping + @ResponseStatus(CREATED) + public SuccessResponse save(@Validated @RequestBody TradeRequestDto requestDto) { + return Stream.of(requestDto) + .map(mapper::toEntity) + .map(service::save) + .map(mapper::fromEntity) + .map(responseDto -> of(TRADE_CREATE_SUCCESS, responseDto)) + .findAny() + .orElseThrow(() -> new TradeNotFoundException(TRADE_NOT_CREATED)); + } + + @GetMapping + @ResponseStatus(OK) + public SuccessResponse findALl() { + return of(TRADE_FIND_SUCCESS, + service.findAll() + .stream() + .map(mapper::fromEntity) + .collect(Collectors.toList()) + ); + } + + @GetMapping("/{id}") + @ResponseStatus(OK) + public SuccessResponse findById(@PathVariable Long id) { + return Stream.of(id) + .map(service::findById) + .map(mapper::fromEntity) + .map(responseDto -> of(TRADE_FIND_SUCCESS, responseDto)) + .findAny() + .orElseThrow(() -> new TradeNotFoundException(TRADE_NOT_FOUND)); + } + + @PutMapping("/{id}") + @ResponseStatus(CREATED) + public SuccessResponse updateById(@PathVariable Long id, @Validated @RequestBody TradeRequestDto requestDto) { + Stream.of(requestDto) + .map(mapper::toEntity) + .forEach(entity -> service.update(id, entity)); + return of(TRADE_UPDATE_SUCCESS); + } + + @DeleteMapping("/{id}") + @ResponseStatus(NO_CONTENT) + public SuccessResponse deleteById(@PathVariable Long id) { + Stream.of(id) + .forEach(service::remove); + return of(TRADE_DELETE_SUCCESS); + } + +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/trade/domain/entity/Trade.java b/backend/src/main/java/com/hjjang/backend/domain/trade/domain/entity/Trade.java new file mode 100644 index 0000000..dce0583 --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/trade/domain/entity/Trade.java @@ -0,0 +1,54 @@ +package com.hjjang.backend.domain.trade.domain.entity; + +import com.hjjang.backend.domain.post.domain.entity.Post; +import com.hjjang.backend.domain.user.entity.User; +import com.hjjang.backend.global.domain.BaseTimeEntity; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import javax.persistence.*; + +@Entity +@Getter +@AllArgsConstructor +@RequiredArgsConstructor +public class Trade extends BaseTimeEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "post_id") + Post post; + + @ManyToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "buyer_id") + User buyer; + + @ManyToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "seller_id") + User seller; + + @Column(nullable = false) + private boolean removed = false; + + @Builder + public Trade(Post post, User buyer, User seller) { + this.post = post; + this.buyer = buyer; + this.seller = seller; + } + + public void update(Trade entity) { + this.post = entity.getPost(); + this.buyer = entity.getBuyer(); + this.seller = entity.getSeller(); + } + + public void remove() { + this.removed = true; + } +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/trade/domain/repositroy/TradeRepository.java b/backend/src/main/java/com/hjjang/backend/domain/trade/domain/repositroy/TradeRepository.java new file mode 100644 index 0000000..8635a05 --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/trade/domain/repositroy/TradeRepository.java @@ -0,0 +1,7 @@ +package com.hjjang.backend.domain.trade.domain.repositroy; + +import com.hjjang.backend.domain.trade.domain.entity.Trade; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface TradeRepository extends JpaRepository { +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/trade/dto/TradeMapper.java b/backend/src/main/java/com/hjjang/backend/domain/trade/dto/TradeMapper.java new file mode 100644 index 0000000..7fa0f18 --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/trade/dto/TradeMapper.java @@ -0,0 +1,53 @@ +package com.hjjang.backend.domain.trade.dto; + +import com.hjjang.backend.domain.post.domain.entity.Post; +import com.hjjang.backend.domain.post.exception.PostNotFoundException; +import com.hjjang.backend.domain.post.service.PostServiceImpl; +import com.hjjang.backend.domain.trade.domain.entity.Trade; +import com.hjjang.backend.domain.user.entity.User; +import com.hjjang.backend.domain.user.repository.UserRepository; +import com.hjjang.backend.global.execption.BusinessException; +import com.hjjang.backend.global.response.code.ErrorCode; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.Optional; +import java.util.function.LongFunction; +import java.util.stream.Stream; + +@Service +@RequiredArgsConstructor +public class TradeMapper { + + private final PostServiceImpl postService; + private final UserRepository userRepository; + + LongFunction findUser = userId -> Stream.of(userId) + .map(userRepository::findById) //유저 리포지토리를 여기서 사용해도 될지 모르겠습니다. + .filter(Optional::isPresent) + .map(Optional::get) + .findAny() + .orElseThrow(() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND)); //TODO 유저 관련 예외 생성 필요 + + LongFunction findPost = postId -> Stream.of(postId) + .map(postService::findOneById) + .findAny() + .orElseThrow(PostNotFoundException::new); + + public Trade toEntity(TradeRequestDto requestDto) { + return Trade.builder() + .post(findPost.apply(requestDto.getPostId())) + .buyer(findUser.apply(requestDto.getBuyerId())) + .seller(findUser.apply(requestDto.getSellerId())) + .build(); + } + + public TradeResponseDto fromEntity(Trade entity) { + return TradeResponseDto.builder() + .id(entity.getId()) + .postId(entity.getPost().getId()) + .buyerId(entity.getBuyer().getId()) + .sellerId(entity.getSeller().getId()) + .build(); + } +} \ No newline at end of file diff --git a/backend/src/main/java/com/hjjang/backend/domain/trade/dto/TradeRequestDto.java b/backend/src/main/java/com/hjjang/backend/domain/trade/dto/TradeRequestDto.java new file mode 100644 index 0000000..db100c8 --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/trade/dto/TradeRequestDto.java @@ -0,0 +1,22 @@ +package com.hjjang.backend.domain.trade.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +import javax.validation.constraints.NotNull; + +@Data +public class TradeRequestDto { + + @NotNull + @JsonProperty("post_id") + private Long postId; + + @NotNull + @JsonProperty("buyer_id") + private Long buyerId; + + @NotNull + @JsonProperty("seller_id") + private Long sellerId; +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/trade/dto/TradeResponseDto.java b/backend/src/main/java/com/hjjang/backend/domain/trade/dto/TradeResponseDto.java new file mode 100644 index 0000000..d58c650 --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/trade/dto/TradeResponseDto.java @@ -0,0 +1,21 @@ +package com.hjjang.backend.domain.trade.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class TradeResponseDto { + Long id; + + @JsonProperty("post_id") + private Long postId; + + @JsonProperty("buyer_id") + private Long buyerId; + + @JsonProperty("seller_id") + private Long sellerId; + +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/trade/exception/TradeNotFoundException.java b/backend/src/main/java/com/hjjang/backend/domain/trade/exception/TradeNotFoundException.java new file mode 100644 index 0000000..22ba54a --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/trade/exception/TradeNotFoundException.java @@ -0,0 +1,21 @@ +package com.hjjang.backend.domain.trade.exception; + +import com.hjjang.backend.global.execption.BusinessException; +import com.hjjang.backend.global.response.code.ErrorCode; +import com.hjjang.backend.global.response.response.ErrorResponse; + +import java.util.List; + +public class TradeNotFoundException extends BusinessException { + public TradeNotFoundException(String message, ErrorCode errorCode) { + super(message, errorCode); + } + + public TradeNotFoundException(ErrorCode errorCode) { + super(errorCode); + } + + public TradeNotFoundException(ErrorCode errorCode, List errors) { + super(errorCode, errors); + } +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/trade/service/TradeService.java b/backend/src/main/java/com/hjjang/backend/domain/trade/service/TradeService.java new file mode 100644 index 0000000..79acf17 --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/trade/service/TradeService.java @@ -0,0 +1,50 @@ +package com.hjjang.backend.domain.trade.service; + +import com.hjjang.backend.domain.trade.domain.entity.Trade; +import com.hjjang.backend.domain.trade.domain.repositroy.TradeRepository; +import com.hjjang.backend.domain.trade.exception.TradeNotFoundException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Stream; + +@Service +@RequiredArgsConstructor +public class TradeService { + + private final TradeRepository repository; + + public Trade save(Trade entity) { + return Stream.of(entity) + .map(repository::save) + .findAny() + .orElseThrow(() -> new TradeNotFoundException(null)); + } + + public Trade findById(long id) { + return Stream.of(id) + .map(repository::findById) + .filter(Optional::isPresent) + .map(Optional::get) + .findAny() + .orElseThrow(() -> new TradeNotFoundException(null)); + } + + public List findAll() { + return repository.findAll(); + } + + public void update(long id, Trade entity) { + Stream.of(id) + .map(this::findById) + .forEach(trade -> trade.update(entity)); + } + + public void remove(long id) { + Stream.of(id) + .map(this::findById) + .forEach(Trade::remove); + } +} diff --git a/backend/src/main/java/com/hjjang/backend/global/response/code/ErrorCode.java b/backend/src/main/java/com/hjjang/backend/global/response/code/ErrorCode.java index d8cf3df..0eb8c1f 100644 --- a/backend/src/main/java/com/hjjang/backend/global/response/code/ErrorCode.java +++ b/backend/src/main/java/com/hjjang/backend/global/response/code/ErrorCode.java @@ -14,7 +14,13 @@ public enum ErrorCode { //User MEMBER_NOT_FOUND("U001", "존재 하지 않는 사용자"), - NO_AUTHORITY("U002", "권한이 없음"),; + NO_AUTHORITY("U002", "권한이 없음"), + + //Trade + TRADE_NOT_CREATED("T001", "거래기록 생성 실패"), + TRADE_NOT_FOUND("T002", "존재 하지 않는 거래기록"), + + ; private final String code; diff --git a/backend/src/main/java/com/hjjang/backend/global/response/code/SuccessCode.java b/backend/src/main/java/com/hjjang/backend/global/response/code/SuccessCode.java index 506a47c..32beb50 100644 --- a/backend/src/main/java/com/hjjang/backend/global/response/code/SuccessCode.java +++ b/backend/src/main/java/com/hjjang/backend/global/response/code/SuccessCode.java @@ -8,7 +8,15 @@ public enum SuccessCode { //User - USER_PROFILE_SUCCESS( "U001", "프로필 조회 완료."),; + USER_PROFILE_SUCCESS( "U001", "프로필 조회 완료."), + + //Trade + TRADE_CREATE_SUCCESS("T001", "거래기록 생성 완료"), + TRADE_FIND_SUCCESS("T002", "거래기록 조회 완료"), + TRADE_UPDATE_SUCCESS("T003", "거래기록 갱신 완료"), + TRADE_DELETE_SUCCESS("T004", "거래기록 삭제 완료"), + + ; private final String code; private final String message; From d768ae2db3dbc898580140beab676cb362509d3a Mon Sep 17 00:00:00 2001 From: Yunkeun Date: Wed, 25 May 2022 20:55:40 +0900 Subject: [PATCH 5/7] =?UTF-8?q?refactor:=20ApiResponse=EB=A5=BC=20SuccessR?= =?UTF-8?q?esponse=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/mail/controller/MailController.java | 14 ++++++-------- .../backend/global/response/code/SuccessCode.java | 4 ++++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailController.java b/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailController.java index ee7c66b..2cbc024 100644 --- a/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailController.java +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailController.java @@ -1,12 +1,11 @@ package com.hjjang.backend.domain.mail.controller; -import static org.springframework.http.HttpStatus.*; -import static org.springframework.http.ResponseEntity.*; +import static com.hjjang.backend.global.response.code.SuccessCode.*; import com.hjjang.backend.domain.mail.dto.MailRequest; import com.hjjang.backend.domain.mail.dto.MailResponse; import com.hjjang.backend.domain.mail.service.MailService; -import com.hjjang.backend.global.dto.ApiResponse; +import com.hjjang.backend.global.response.response.SuccessResponse; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; @@ -22,16 +21,15 @@ public class MailController { private final MailService mailService; @PostMapping("/auth") - public ResponseEntity send(@RequestBody MailRequest mailRequest) { + public ResponseEntity send(@RequestBody MailRequest mailRequest) { String mail = mailRequest.getMail(); MailResponse mailResponse = mailService.sendMail(mail); - return status(CREATED) - .body(ApiResponse.success("send", mailResponse)); + return ResponseEntity.ok(SuccessResponse.of(MAIL_SEND_SUCCESS, mailResponse)); } @PostMapping("/check") - public ResponseEntity auth(@RequestBody MailRequest mailRequest) { + public ResponseEntity auth(@RequestBody MailRequest mailRequest) { MailResponse mailResponse = mailService.checkCode(mailRequest); - return ok(ApiResponse.success("auth", mailResponse)); + return ResponseEntity.ok(SuccessResponse.of(MAIL_CHECK_SUCCESS, mailResponse)); } } diff --git a/backend/src/main/java/com/hjjang/backend/global/response/code/SuccessCode.java b/backend/src/main/java/com/hjjang/backend/global/response/code/SuccessCode.java index 506a47c..f0903f9 100644 --- a/backend/src/main/java/com/hjjang/backend/global/response/code/SuccessCode.java +++ b/backend/src/main/java/com/hjjang/backend/global/response/code/SuccessCode.java @@ -7,6 +7,10 @@ @AllArgsConstructor public enum SuccessCode { + //Mail + MAIL_SEND_SUCCESS("M001","코드 생성하여 메일 전송."), + MAIL_CHECK_SUCCESS("M002", "메일 인증 확인."), + //User USER_PROFILE_SUCCESS( "U001", "프로필 조회 완료."),; From cbc68cb53117ad98f29b14ebefecf943c122a424 Mon Sep 17 00:00:00 2001 From: Yunkeun Date: Wed, 25 May 2022 21:23:29 +0900 Subject: [PATCH 6/7] =?UTF-8?q?fix:=20=EC=BD=94=EB=93=9C=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=ED=95=98=EC=97=AC=20=EB=A9=94=EC=9D=BC=20=EB=B3=B4?= =?UTF-8?q?=EB=82=B4=EB=8A=94=20send=20=EC=83=81=ED=83=9C=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20201=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/domain/mail/controller/MailController.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailController.java b/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailController.java index 2cbc024..0574e90 100644 --- a/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailController.java +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailController.java @@ -1,6 +1,7 @@ package com.hjjang.backend.domain.mail.controller; import static com.hjjang.backend.global.response.code.SuccessCode.*; +import static org.springframework.http.HttpStatus.*; import com.hjjang.backend.domain.mail.dto.MailRequest; import com.hjjang.backend.domain.mail.dto.MailResponse; @@ -20,16 +21,17 @@ public class MailController { private final MailService mailService; - @PostMapping("/auth") + @PostMapping("/send") public ResponseEntity send(@RequestBody MailRequest mailRequest) { String mail = mailRequest.getMail(); MailResponse mailResponse = mailService.sendMail(mail); - return ResponseEntity.ok(SuccessResponse.of(MAIL_SEND_SUCCESS, mailResponse)); + SuccessResponse response = SuccessResponse.of(MAIL_SEND_SUCCESS, mailResponse); + return new ResponseEntity<>(response, CREATED); } @PostMapping("/check") public ResponseEntity auth(@RequestBody MailRequest mailRequest) { MailResponse mailResponse = mailService.checkCode(mailRequest); - return ResponseEntity.ok(SuccessResponse.of(MAIL_CHECK_SUCCESS, mailResponse)); + return ResponseEntity.ok(SuccessResponse.of(MAIL_CHECK_SUCCESS)); } } From a5dd506a48859b2bfb237a8088499dd602dee46b Mon Sep 17 00:00:00 2001 From: Yunkeun Date: Wed, 25 May 2022 21:59:39 +0900 Subject: [PATCH 7/7] =?UTF-8?q?refactor:=20ErrorResponse=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mail/controller/MailControllerAdvice.java | 30 ------------- .../mail/exception/ExceptionMessage.java | 14 ------- .../mail/exception/InvalidMailException.java | 11 +++++ .../domain/mail/exception/MailException.java | 42 ------------------- .../mail/exception/TimeLimitException.java | 10 ----- .../mail/exception/UnauthorizedException.java | 16 +++---- .../handler/MailExceptionHandler.java | 27 ++++++++++++ .../domain/mail/service/MailService.java | 40 ++++++++++++++++-- .../global/response/code/ErrorCode.java | 5 +++ .../hjjang/backend/email/MailServiceTest.java | 19 +++++---- 10 files changed, 95 insertions(+), 119 deletions(-) delete mode 100644 backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailControllerAdvice.java delete mode 100644 backend/src/main/java/com/hjjang/backend/domain/mail/exception/ExceptionMessage.java create mode 100644 backend/src/main/java/com/hjjang/backend/domain/mail/exception/InvalidMailException.java delete mode 100644 backend/src/main/java/com/hjjang/backend/domain/mail/exception/MailException.java delete mode 100644 backend/src/main/java/com/hjjang/backend/domain/mail/exception/TimeLimitException.java create mode 100644 backend/src/main/java/com/hjjang/backend/domain/mail/exception/handler/MailExceptionHandler.java diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailControllerAdvice.java b/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailControllerAdvice.java deleted file mode 100644 index 89daf4f..0000000 --- a/backend/src/main/java/com/hjjang/backend/domain/mail/controller/MailControllerAdvice.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.hjjang.backend.domain.mail.controller; - -import static org.springframework.http.HttpStatus.*; - -import com.hjjang.backend.domain.mail.exception.ExceptionMessage; -import com.hjjang.backend.domain.mail.exception.MailException; -import com.hjjang.backend.domain.mail.exception.TimeLimitException; -import com.hjjang.backend.domain.mail.exception.UnauthorizedException; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.RestControllerAdvice; - -@RestControllerAdvice -public class MailControllerAdvice { - - @ExceptionHandler(value = MailException.class) - public ResponseEntity mailExceptionHandle() { - return ResponseEntity.status(BAD_REQUEST).body(ExceptionMessage.INVALID_MAIL.getMessage()); - } - - @ExceptionHandler(value = TimeLimitException.class) - public ResponseEntity timeLimitExceptionHandle() { - return ResponseEntity.status(REQUEST_TIMEOUT).body(ExceptionMessage.TIME_LIMIT.getMessage()); - } - - @ExceptionHandler(value = UnauthorizedException.class) - public ResponseEntity unauthorizedExceptionHandle() { - return ResponseEntity.status(UNAUTHORIZED).body(ExceptionMessage.INVALID_CODE.getMessage()); - } -} diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/exception/ExceptionMessage.java b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/ExceptionMessage.java deleted file mode 100644 index 097b3aa..0000000 --- a/backend/src/main/java/com/hjjang/backend/domain/mail/exception/ExceptionMessage.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.hjjang.backend.domain.mail.exception; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -@Getter -@AllArgsConstructor -public enum ExceptionMessage { - INVALID_MAIL("알맞은 대학교 메일 계정으로 입력해주세요."), - INVALID_CODE("잘못된 코드 번호가 입력되었습니다."), - TIME_LIMIT("시간이 초과되었습니다."); - - private final String message; -} diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/exception/InvalidMailException.java b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/InvalidMailException.java new file mode 100644 index 0000000..3975052 --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/InvalidMailException.java @@ -0,0 +1,11 @@ +package com.hjjang.backend.domain.mail.exception; + +import com.hjjang.backend.global.execption.BusinessException; +import com.hjjang.backend.global.response.code.ErrorCode; + +public class InvalidMailException extends BusinessException { + + public InvalidMailException() { + super(ErrorCode.INVALID_MAIL); + } +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/exception/MailException.java b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/MailException.java deleted file mode 100644 index ab6f3f0..0000000 --- a/backend/src/main/java/com/hjjang/backend/domain/mail/exception/MailException.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.hjjang.backend.domain.mail.exception; - -import com.hjjang.backend.domain.mail.dto.Mail; -import com.hjjang.backend.domain.mail.domain.MailRegex; -import com.hjjang.backend.domain.mail.dto.MailRequest; -import java.util.regex.Pattern; - -public class MailException extends RuntimeException { - - private MailException() { } - - public static void checkPossibleMail(String mailAddress) { - checkMailDuplicate(mailAddress); - if (!isUniversityMail(mailAddress)) { - throw new MailException(); - } - } - - public static void checkRequest(MailRequest mailRequest, Mail mail) { - UnauthorizedException.checkValidCode(mailRequest, mail.getCode()); - checkValidRequestMail(mailRequest, mail.getAddress()); - } - - private static void checkValidRequestMail(MailRequest mailRequest, String mailAddress) { - if (!mailRequest.getMail().equals(mailAddress)) { - throw new MailException(); - } - } - - private static boolean isUniversityMail(String mailAddress) { - MailRegex university = MailRegex.UNIVERSITY; - MailRegex gsUniversity = MailRegex.GS_UNIVERSITY; - if (Pattern.matches(university.getRegex(), mailAddress)) { - return true; - } - return Pattern.matches(gsUniversity.getRegex(), mailAddress); - } - - private static void checkMailDuplicate(String mail) { - // 모든 유저에 저장되어 있는 email과 겹친다면 exception - } -} diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/exception/TimeLimitException.java b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/TimeLimitException.java deleted file mode 100644 index c253abd..0000000 --- a/backend/src/main/java/com/hjjang/backend/domain/mail/exception/TimeLimitException.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.hjjang.backend.domain.mail.exception; - -public class TimeLimitException extends RuntimeException { - - private TimeLimitException() { } - - public static void checkTimeLimit(String code) { - // 시간 지난 만큼을 인자로 받아 넘었을 경우 exception - } -} diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/exception/UnauthorizedException.java b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/UnauthorizedException.java index fb8a961..34ebf62 100644 --- a/backend/src/main/java/com/hjjang/backend/domain/mail/exception/UnauthorizedException.java +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/UnauthorizedException.java @@ -1,17 +1,11 @@ package com.hjjang.backend.domain.mail.exception; -import static com.hjjang.backend.domain.mail.exception.TimeLimitException.*; +import com.hjjang.backend.global.execption.BusinessException; +import com.hjjang.backend.global.response.code.ErrorCode; -import com.hjjang.backend.domain.mail.dto.MailRequest; +public class UnauthorizedException extends BusinessException { -public class UnauthorizedException extends RuntimeException { - - private UnauthorizedException() { } - - public static void checkValidCode(MailRequest mailRequest, String code) { - checkTimeLimit(code); - if (!mailRequest.getCode().equals(code)) { - throw new UnauthorizedException(); - } + public UnauthorizedException() { + super(ErrorCode.INVALID_CODE); } } diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/exception/handler/MailExceptionHandler.java b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/handler/MailExceptionHandler.java new file mode 100644 index 0000000..f3d3172 --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/exception/handler/MailExceptionHandler.java @@ -0,0 +1,27 @@ +package com.hjjang.backend.domain.mail.exception.handler; + +import static com.hjjang.backend.global.response.code.ErrorCode.*; +import static org.springframework.http.HttpStatus.*; + +import com.hjjang.backend.domain.mail.exception.InvalidMailException; +import com.hjjang.backend.domain.mail.exception.UnauthorizedException; +import com.hjjang.backend.global.response.response.ErrorResponse; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class MailExceptionHandler { + + @ExceptionHandler(value = InvalidMailException.class) + public ResponseEntity mailExceptionHandle() { + ErrorResponse response = ErrorResponse.of(INVALID_MAIL); + return new ResponseEntity<>(response, BAD_REQUEST); + } + + @ExceptionHandler(value = UnauthorizedException.class) + public ResponseEntity unauthorizedExceptionHandle() { + ErrorResponse response = ErrorResponse.of(INVALID_CODE); + return new ResponseEntity<>(response, UNAUTHORIZED); + } +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/mail/service/MailService.java b/backend/src/main/java/com/hjjang/backend/domain/mail/service/MailService.java index cbc54d6..561a561 100644 --- a/backend/src/main/java/com/hjjang/backend/domain/mail/service/MailService.java +++ b/backend/src/main/java/com/hjjang/backend/domain/mail/service/MailService.java @@ -5,8 +5,10 @@ import com.hjjang.backend.domain.mail.domain.MailRegex; import com.hjjang.backend.domain.mail.dto.MailRequest; import com.hjjang.backend.domain.mail.dto.MailResponse; -import com.hjjang.backend.domain.mail.exception.MailException; +import com.hjjang.backend.domain.mail.exception.InvalidMailException; +import com.hjjang.backend.domain.mail.exception.UnauthorizedException; import java.util.List; +import java.util.regex.Pattern; import lombok.RequiredArgsConstructor; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; @@ -20,7 +22,7 @@ public class MailService { private final Mail mail = new Mail(); public MailResponse sendMail(String mailAddress) { - MailException.checkPossibleMail(mailAddress); + checkPossibleMail(mailAddress); SimpleMailMessage message = new SimpleMailMessage(); setMessage(message, mailAddress); javaMailSender.send(message); @@ -28,7 +30,7 @@ public MailResponse sendMail(String mailAddress) { } public MailResponse checkCode(MailRequest mailRequest) { - MailException.checkRequest(mailRequest, mail); + checkRequest(mailRequest, mail); mail.setIsAuth(true); return new MailResponse(mail); } @@ -58,4 +60,36 @@ private void saveMailInfo(String mailAddress) { mail.setUniversity(university); mail.setIsAuth(false); } + + private void checkPossibleMail(String mailAddress) { + if (!isUniversityMail(mailAddress)) { + throw new InvalidMailException(); + } + } + + private void checkRequest(MailRequest mailRequest, Mail mail) { + checkValidCode(mailRequest, mail.getCode()); + checkValidRequestMail(mailRequest, mail.getAddress()); + } + + private void checkValidCode(MailRequest mailRequest, String code) { + if (!mailRequest.getCode().equals(code)) { + throw new UnauthorizedException(); + } + } + + private void checkValidRequestMail(MailRequest mailRequest, String mailAddress) { + if (!mailRequest.getMail().equals(mailAddress)) { + throw new InvalidMailException(); + } + } + + private boolean isUniversityMail(String mailAddress) { + MailRegex university = MailRegex.UNIVERSITY; + MailRegex gsUniversity = MailRegex.GS_UNIVERSITY; + if (Pattern.matches(university.getRegex(), mailAddress)) { + return true; + } + return Pattern.matches(gsUniversity.getRegex(), mailAddress); + } } diff --git a/backend/src/main/java/com/hjjang/backend/global/response/code/ErrorCode.java b/backend/src/main/java/com/hjjang/backend/global/response/code/ErrorCode.java index d8cf3df..121e25e 100644 --- a/backend/src/main/java/com/hjjang/backend/global/response/code/ErrorCode.java +++ b/backend/src/main/java/com/hjjang/backend/global/response/code/ErrorCode.java @@ -7,6 +7,11 @@ @AllArgsConstructor public enum ErrorCode { + //Mail + INVALID_MAIL("M001", "대학교 메일 계정 오류"), + INVALID_CODE("M002", "코드 입력 오류"), + TIME_LIMIT("M003", "시간 초과"), + //Global INTERNAL_SERVER_ERROR("G001", "내부 서버 오류"), NOT_ALLOWED_METHOD("G002", "허용 되지 않은 HTTP method"), diff --git a/backend/src/test/java/com/hjjang/backend/email/MailServiceTest.java b/backend/src/test/java/com/hjjang/backend/email/MailServiceTest.java index b51c3bb..2a35f5b 100644 --- a/backend/src/test/java/com/hjjang/backend/email/MailServiceTest.java +++ b/backend/src/test/java/com/hjjang/backend/email/MailServiceTest.java @@ -5,7 +5,7 @@ import com.hjjang.backend.domain.mail.dto.Mail; import com.hjjang.backend.domain.mail.dto.MailRequest; -import com.hjjang.backend.domain.mail.exception.MailException; +import com.hjjang.backend.domain.mail.exception.InvalidMailException; import com.hjjang.backend.domain.mail.service.MailService; import java.lang.reflect.Method; import java.util.ArrayList; @@ -27,7 +27,7 @@ void authEmail() { Mail email = new Mail(code, emailAddress, "dankook", false); //when MailRequest mailRequest = new MailRequest(emailAddress, code); - MailException.checkRequest(mailRequest, email); + //then assertThat(email.getCode()).isEqualTo(mailRequest.getCode()); } @@ -74,7 +74,7 @@ void checkInvalidEmail() { falseEmails.add("32174294@gsd.dankook.ac.kr"); //when //then - falseEmails.forEach(MailServiceTest::isException); +// falseEmails.forEach(MailServiceTest::isException); } @DisplayName("메일 주소 예외처리 테스트 (성공 케이스)") @@ -88,7 +88,7 @@ void checkValidEmail() { trueEmails.add("dr0joon@gs.anyang.ac.kr"); //when //then - trueEmails.forEach(MailException::checkPossibleMail); +// trueEmails.forEach(InvalidMailException::checkPossibleMail); } private JavaMailSenderImpl mailSender() { @@ -99,9 +99,10 @@ private JavaMailSenderImpl mailSender() { return javaMailSender; } - private static void isException(String email) { - //then, when - assertThrows(RuntimeException.class, () -> - MailException.checkPossibleMail(email)); - } +// private static void isException(String email) { +// //then, when +// assertThrows(RuntimeException.class, () -> +// +// InvalidMailException.checkPossibleMail(email)); +// } }