add rest controller

This commit is contained in:
Anton Romanov 2024-10-22 17:39:38 +04:00
parent e8db1102e6
commit 265bf8b275
3 changed files with 51 additions and 0 deletions

View File

@ -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'
}

View File

@ -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;

View File

@ -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<Email> 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);
}
}