Add validator and service to refactor.
This commit is contained in:
@@ -2,6 +2,7 @@ package fr.ippon.example.intellijfeatures.controller;
|
|||||||
|
|
||||||
import fr.ippon.example.intellijfeatures.model.User;
|
import fr.ippon.example.intellijfeatures.model.User;
|
||||||
import fr.ippon.example.intellijfeatures.services.mail.EmailService;
|
import fr.ippon.example.intellijfeatures.services.mail.EmailService;
|
||||||
|
import fr.ippon.example.intellijfeatures.services.validator.UserValidator;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import static org.springframework.http.HttpStatus.NO_CONTENT;
|
import static org.springframework.http.HttpStatus.NO_CONTENT;
|
||||||
@@ -10,14 +11,17 @@ import static org.springframework.http.HttpStatus.NO_CONTENT;
|
|||||||
@RequestMapping("/api/mails")
|
@RequestMapping("/api/mails")
|
||||||
public class EmailController {
|
public class EmailController {
|
||||||
private final EmailService emailService;
|
private final EmailService emailService;
|
||||||
|
private final UserValidator userValidator;
|
||||||
|
|
||||||
public EmailController(EmailService emailService) {
|
public EmailController(EmailService emailService, UserValidator userValidator) {
|
||||||
this.emailService = emailService;
|
this.emailService = emailService;
|
||||||
|
this.userValidator = userValidator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/send")
|
@PostMapping("/send")
|
||||||
@ResponseStatus(NO_CONTENT)
|
@ResponseStatus(NO_CONTENT)
|
||||||
public void sendMail(@RequestBody User user) {
|
public void sendMail(@RequestBody User user) {
|
||||||
|
userValidator.check(user);
|
||||||
emailService.sendMail(user);
|
emailService.sendMail(user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package fr.ippon.example.intellijfeatures.exception;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
|
||||||
|
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
|
||||||
|
public class BadRequestException extends BusinessException {
|
||||||
|
public BadRequestException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BadRequestException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package fr.ippon.example.intellijfeatures.exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Business exception.
|
||||||
|
*/
|
||||||
|
public class BusinessException extends RuntimeException {
|
||||||
|
|
||||||
|
public BusinessException() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an exception with a message.
|
||||||
|
* @param message The description of the error met.
|
||||||
|
*/
|
||||||
|
public BusinessException(final String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an exception with a message and a code.
|
||||||
|
* @param message The description of the error met.
|
||||||
|
* @param cause The cause of the exception.
|
||||||
|
*/
|
||||||
|
public BusinessException(final String message, final Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package fr.ippon.example.intellijfeatures.services.validator;
|
||||||
|
|
||||||
|
import fr.ippon.example.intellijfeatures.exception.BadRequestException;
|
||||||
|
import fr.ippon.example.intellijfeatures.model.User;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class UserValidator {
|
||||||
|
public void check(User user) {
|
||||||
|
if (user.getName() == null || ObjectUtils.isEmpty(user.getName())) {
|
||||||
|
throw new BadRequestException("User name is required.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user.getEmail() == null || !Pattern.matches("\\w+@\\w+\\.\\w+", user.getEmail())) {
|
||||||
|
throw new BadRequestException("User email is required and should be a valid email.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package fr.ippon.example.intellijfeatures.torefactor;
|
||||||
|
|
||||||
|
import com.mitchellbosecke.pebble.PebbleEngine;
|
||||||
|
import fr.ippon.example.intellijfeatures.exception.BadRequestException;
|
||||||
|
import fr.ippon.example.intellijfeatures.model.User;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.mail.javamail.JavaMailSender;
|
||||||
|
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.mail.MessagingException;
|
||||||
|
import javax.mail.internet.MimeMessage;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
import java.io.Writer;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/mail2")
|
||||||
|
public class MonolithController {
|
||||||
|
private final JavaMailSender javaMailSender;
|
||||||
|
|
||||||
|
public MonolithController(JavaMailSender javaMailSender) {
|
||||||
|
this.javaMailSender = javaMailSender;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/send")
|
||||||
|
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||||
|
public void sendMail(@RequestBody User user) throws IOException, MessagingException {
|
||||||
|
if (user.getName() == null || ObjectUtils.isEmpty(user.getName())) {
|
||||||
|
throw new BadRequestException("User name is required.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user.getEmail() == null || !Pattern.matches("\\w+@\\w+\\.\\w+", user.getEmail())) {
|
||||||
|
throw new BadRequestException("User email is required and should be a valid email.");
|
||||||
|
}
|
||||||
|
|
||||||
|
PebbleEngine pebbleEngine = new PebbleEngine.Builder().build();
|
||||||
|
|
||||||
|
Writer writer = new StringWriter();
|
||||||
|
pebbleEngine.getTemplate("templates/mail/user-mail.html")
|
||||||
|
.evaluate(writer, Map.of("user", user));
|
||||||
|
String htmlContent = writer.toString();
|
||||||
|
|
||||||
|
Writer writer2 = new StringWriter();
|
||||||
|
pebbleEngine.getTemplate("templates/mail/user-mail.txt")
|
||||||
|
.evaluate(writer2, Map.of("user", user));
|
||||||
|
String textContent = writer2.toString();
|
||||||
|
|
||||||
|
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
|
||||||
|
|
||||||
|
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
|
||||||
|
helper.setTo(user.getEmail());
|
||||||
|
helper.setSubject("Hello");
|
||||||
|
helper.setText(textContent, htmlContent);
|
||||||
|
javaMailSender.send(mimeMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,9 @@
|
|||||||
|
server:
|
||||||
|
error:
|
||||||
|
whitelabel:
|
||||||
|
enabled: false # Disable html error responses.
|
||||||
|
include-stacktrace: never
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
mail:
|
mail:
|
||||||
host: localhost
|
host: localhost
|
||||||
|
|||||||
Reference in New Issue
Block a user