|
@@ -1,14 +1,21 @@
|
|
|
package es.uv.garcosda.controllers;
|
|
|
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
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.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.RestController;
|
|
|
|
|
|
+import es.uv.garcosda.domain.Mail;
|
|
|
import es.uv.garcosda.domain.User;
|
|
|
+import es.uv.garcosda.objects.MailDTO;
|
|
|
+import es.uv.garcosda.services.DispatcherService;
|
|
|
import es.uv.garcosda.services.UserService;
|
|
|
|
|
|
@RestController
|
|
@@ -16,7 +23,16 @@ import es.uv.garcosda.services.UserService;
|
|
|
public class MailerController {
|
|
|
|
|
|
@Autowired
|
|
|
- private UserService us;
|
|
|
+ UserService us;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ DispatcherService ds;
|
|
|
+
|
|
|
+ @GetMapping("users")
|
|
|
+ public ResponseEntity<?> getUsers() {
|
|
|
+ List<User> u = this.us.findAll();
|
|
|
+ return new ResponseEntity<List<User>>(u, HttpStatus.OK);
|
|
|
+ }
|
|
|
|
|
|
@GetMapping("users/{email}")
|
|
|
public ResponseEntity<?> getUser(@PathVariable("email") String email) {
|
|
@@ -25,7 +41,24 @@ public class MailerController {
|
|
|
return new ResponseEntity<String>("No user found with email "+email, HttpStatus.NOT_FOUND);
|
|
|
}
|
|
|
else {
|
|
|
- return new ResponseEntity<User>(u, HttpStatus.NOT_FOUND);
|
|
|
+ return new ResponseEntity<User>(u, HttpStatus.OK);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("send")
|
|
|
+ public ResponseEntity<?> sendMail(@RequestBody MailDTO mail){
|
|
|
+ User from = this.us.findByEmail(mail.getFrom());
|
|
|
+ User to = this.us.findByEmail(mail.getTo());
|
|
|
+ if(from.getEmail() == null) {
|
|
|
+ return new ResponseEntity<String>("No origin user found with email "+mail.getFrom(), HttpStatus.NOT_FOUND);
|
|
|
+ }
|
|
|
+ else if(to.getEmail() == null) {
|
|
|
+ return new ResponseEntity<String>("No destination user found with email "+mail.getTo(), HttpStatus.NOT_FOUND);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ Mail m = new Mail(from, to, mail.getSubject(), mail.getBody());
|
|
|
+ ds.sendMessage(m);
|
|
|
+ return new ResponseEntity<Mail>(m, HttpStatus.OK);
|
|
|
}
|
|
|
}
|
|
|
|