From 265bf8b275eb34d122c63a343208d9416bcb4504 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 22 Oct 2024 17:39:38 +0400 Subject: [PATCH] add rest controller --- build.gradle | 1 + src/main/java/email/AjaxController.java | 2 + src/main/java/email/EmailRestController.java | 48 ++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 src/main/java/email/EmailRestController.java diff --git a/build.gradle b/build.gradle index b1f06d5..64e5ab1 100644 --- a/build.gradle +++ b/build.gradle @@ -31,5 +31,6 @@ dependencies { implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final' implementation group: 'javassist', name: 'javassist', version: '3.12.1.GA' implementation group: 'junit', name: 'junit' + implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5' } diff --git a/src/main/java/email/AjaxController.java b/src/main/java/email/AjaxController.java index 6158c51..a34369c 100644 --- a/src/main/java/email/AjaxController.java +++ b/src/main/java/email/AjaxController.java @@ -2,6 +2,7 @@ package email; import email.model.Email; import email.service.EmailService; +import io.swagger.v3.oas.annotations.Hidden; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -13,6 +14,7 @@ import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller +@Hidden @RequestMapping("/ajax") public class AjaxController { private final EmailService emailService; diff --git a/src/main/java/email/EmailRestController.java b/src/main/java/email/EmailRestController.java new file mode 100644 index 0000000..69c9258 --- /dev/null +++ b/src/main/java/email/EmailRestController.java @@ -0,0 +1,48 @@ +package email; + +import email.model.Email; +import email.service.EmailService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/rest") +public class EmailRestController { + private final EmailService emailService; + + public EmailRestController(EmailService emailService) { + this.emailService = emailService; + } + + @ResponseBody + @GetMapping("/getEmail/{id}") + public Email getEmail(@PathVariable("id") Integer id) { + return emailService.getEmailById(id); + } + + @ResponseBody + @GetMapping("/list") + public List getList() { + return emailService.getAllEmails(); + } + + @ResponseBody + @PostMapping("/saveEmail") + public Email saveEmail(@RequestBody Email email) { + if (email.getTo().isEmpty()) { + throw new RuntimeException("Поле 'Кому' не должно быть пустым"); + } + Email previousEmail = emailService.getEmailById(email.getId()); + previousEmail.setMessage(email.getMessage()); + previousEmail.setSubject(email.getSubject()); + previousEmail.setTo(email.getTo()); + return emailService.save(previousEmail); + } +}