Merge pull request #23 from HediMjid/hedii

on croise les doigts
This commit is contained in:
Hédi 2022-02-02 15:54:13 +01:00 committed by GitHub
commit bedffd0881
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 193 additions and 12 deletions

View File

@ -38,7 +38,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>

View File

@ -21,7 +21,7 @@ public class ContactController {
private ContactRepository contactRepo; private ContactRepository contactRepo;
@GetMapping(value = "/{id}") @GetMapping(value = "/{id}")
@PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')") //@PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')")
public ResponseEntity<?> findById(@PathVariable int id){ public ResponseEntity<?> findById(@PathVariable int id){
Optional<Contact> contact = null; Optional<Contact> contact = null;
try try
@ -35,7 +35,7 @@ public class ContactController {
} }
@GetMapping(value = "team/{team_id}") @GetMapping(value = "team/{team_id}")
@PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')") //@PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')")
public ResponseEntity<?> findByTeamId(@PathVariable int team_id){ public ResponseEntity<?> findByTeamId(@PathVariable int team_id){
List<Contact> contacts = null; List<Contact> contacts = null;
try try
@ -49,7 +49,7 @@ public class ContactController {
} }
@PostMapping(value="/add") @PostMapping(value="/add")
@PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')") //@PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')")
public ResponseEntity<?> addContact(@RequestBody Contact contact){ public ResponseEntity<?> addContact(@RequestBody Contact contact){
Contact resultContact = null; Contact resultContact = null;
try { try {
@ -62,7 +62,7 @@ public class ContactController {
} }
@PutMapping("/update/{id}") @PutMapping("/update/{id}")
@PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')") //@PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')")
public ResponseEntity<?> updateContact(@RequestBody Contact contact, @PathVariable Integer id) throws Exception { public ResponseEntity<?> updateContact(@RequestBody Contact contact, @PathVariable Integer id) throws Exception {
Contact resultContact = null; Contact resultContact = null;
try { try {
@ -76,7 +76,7 @@ public class ContactController {
} }
@DeleteMapping(value = "/delete/{id}") @DeleteMapping(value = "/delete/{id}")
@PreAuthorize("hasRole('ROLE_PARENT')") //@PreAuthorize("hasRole('ROLE_PARENT')")
public ResponseEntity<?> deleteContact(@PathVariable int id){ public ResponseEntity<?> deleteContact(@PathVariable int id){
try { try {
contactRepo.delete(contactRepo.getById(id)); contactRepo.delete(contactRepo.getById(id));

View File

@ -0,0 +1,43 @@
package fr.organizee.controller;
import fr.organizee.model.Mail;
import fr.organizee.service.SendMailService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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 javax.mail.MessagingException;
@RestController
@RequestMapping("/sendmail")
public class MailController {
SendMailService service;
public MailController(SendMailService service) {
this.service = service;
}
// Envoi de mail en text brut
@PostMapping("/text")
public ResponseEntity<String> sendMail(@RequestBody Mail mail) {
service.sendMail(mail);
return new ResponseEntity<>("Email Sent successfully", HttpStatus.OK);
}
// Envoi de mail au format HTML
@PostMapping("/html")
public ResponseEntity<String> sendMailHTML(@RequestBody Mail mail) throws MessagingException {
service.sendMailHTML(mail);
return new ResponseEntity<>("HTML mail sent successfully", HttpStatus.OK);
}
// Envoi du mail avec une piece jointe
@PostMapping("/attachment")
public ResponseEntity<String> sendAttachmentEmail(@RequestBody Mail mail) throws MessagingException {
service.sendMailWithAttachments(mail);
return new ResponseEntity<>("Attachment mail sent successfully", HttpStatus.OK);
}
}

View File

@ -1,5 +1,6 @@
package fr.organizee.model; package fr.organizee.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*; import javax.persistence.*;
@ -18,7 +19,7 @@ public class Contact {
private LocalDate dateNaissance; private LocalDate dateNaissance;
@ManyToOne @ManyToOne
@JoinColumn(name="TEAM_ID") @JoinColumn(name="TEAM_ID")
@JsonIgnoreProperties("contact") @JsonIgnoreProperties({"contact","membre"})
private Team team; private Team team;
public Contact() { public Contact() {

View File

@ -1,5 +1,6 @@
package fr.organizee.model; package fr.organizee.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*; import javax.persistence.*;
@ -14,13 +15,13 @@ public class Evenement {
private LocalDateTime eventFin; private LocalDateTime eventFin;
private int allDay; private int allDay;
private String libelle; private String libelle;
@ManyToOne(cascade = CascadeType.MERGE) @ManyToOne
@JoinColumn(name="MEMBRE_ID") @JoinColumn(name="MEMBRE_ID")
@JsonIgnoreProperties("evenement") @JsonIgnoreProperties("evenement")
private Membre membre; private Membre membre;
@ManyToOne(cascade = CascadeType.MERGE) @ManyToOne
@JoinColumn(name="TEAM_ID") @JoinColumn(name="TEAM_ID")
@JsonIgnoreProperties("evenement") @JsonIgnoreProperties({"evenement", "membre"})
private Team team; private Team team;
public Evenement() { public Evenement() {

View File

@ -0,0 +1,41 @@
package fr.organizee.model;
public class Mail {
private String recipient;
private String subject;
private String message;
public Mail() {
}
public Mail(String recipient, String subject, String message) {
this.recipient = recipient;
this.subject = subject;
this.message = message;
}
public String getRecipient() {
return recipient;
}
public void setRecipient(String recipient) {
this.recipient = recipient;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@ -15,7 +15,7 @@ public class Menu {
private int validationProposition; private int validationProposition;
@ManyToOne @ManyToOne
@JoinColumn(name="TEAM_ID") @JoinColumn(name="TEAM_ID")
@JsonIgnoreProperties("menu") @JsonIgnoreProperties({"menu","membre"})
private Team team; private Team team;
public Menu() { public Menu() {

View File

@ -1,5 +1,6 @@
package fr.organizee.model; package fr.organizee.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*; import javax.persistence.*;
@ -12,17 +13,24 @@ public class Team {
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private int id; private int id;
private String nom; private String nom;
@OneToMany(mappedBy = "team", fetch=FetchType.LAZY, cascade = CascadeType.ALL) @OneToMany(mappedBy = "team", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
@JsonIgnoreProperties("team") @JsonIgnoreProperties("team")
private List<Membre> membres = new ArrayList<>(); private List<Membre> membres = new ArrayList<>();
@OneToMany(mappedBy = "team", fetch=FetchType.LAZY, cascade = CascadeType.ALL) @OneToMany(mappedBy = "team", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
@JsonIgnoreProperties("team") @JsonIgnoreProperties("team")
@JsonIgnore
private List<Contact> contacts = new ArrayList<>(); private List<Contact> contacts = new ArrayList<>();
@OneToMany(mappedBy = "team", fetch=FetchType.LAZY, cascade = CascadeType.ALL) @OneToMany(mappedBy = "team", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
@JsonIgnoreProperties("team") @JsonIgnoreProperties("team")
@JsonIgnore
private List<TodoList> todolists = new ArrayList<>(); private List<TodoList> todolists = new ArrayList<>();
@OneToMany(mappedBy = "team", fetch=FetchType.LAZY, cascade = CascadeType.ALL) @OneToMany(mappedBy = "team", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
@JsonIgnoreProperties("team") @JsonIgnoreProperties("team")
@JsonIgnore
private List<Menu> menus = new ArrayList<>(); private List<Menu> menus = new ArrayList<>();
public Team() { public Team() {

View File

@ -1,5 +1,6 @@
package fr.organizee.model; package fr.organizee.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*; import javax.persistence.*;
@ -14,7 +15,7 @@ public class TodoList {
private String nom; private String nom;
@ManyToOne @ManyToOne
@JoinColumn(name="TEAM_ID") @JoinColumn(name="TEAM_ID")
@JsonIgnoreProperties("todolist") @JsonIgnoreProperties({"todolist","membre"})
private Team team; private Team team;
@OneToMany(mappedBy = "todolist", fetch=FetchType.LAZY, cascade = CascadeType.ALL) @OneToMany(mappedBy = "todolist", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
@JsonIgnoreProperties("todolist") @JsonIgnoreProperties("todolist")

View File

@ -0,0 +1,15 @@
package fr.organizee.service;
import fr.organizee.model.Mail;
import javax.mail.MessagingException;
public interface SendMailService {
void sendMail(Mail mail);
void sendMailHTML(Mail mail) throws MessagingException;
void sendMailWithAttachments(Mail mail) throws MessagingException;
}

View File

@ -0,0 +1,68 @@
package fr.organizee.service;
import fr.organizee.model.Mail;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@Service
public class SendMailServiceImpl implements SendMailService {
private final JavaMailSender javaMailSender;
public SendMailServiceImpl(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
@Override
public void sendMail(Mail mail) {
SimpleMailMessage msg = new SimpleMailMessage();
msg.setTo(mail.getRecipient(), mail.getRecipient());
msg.setSubject(mail.getSubject());
msg.setText(mail.getMessage());
javaMailSender.send(msg);
}
@Override
public void sendMailHTML(Mail mail) throws MessagingException {
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");
helper.setTo(mail.getRecipient());
helper.setSubject(mail.getSubject());
String htmlMsg = "<h3>Test d'envoi de mail au format HTML</h3>"
+"<img src='http://www.apache.org/images/asf_logo_wide.gif'>";
msg.setContent(htmlMsg, "text/html");
javaMailSender.send(msg);
}
@Override
public void sendMailWithAttachments(Mail mail) throws MessagingException {
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, true);
helper.setTo("destinataire@email");
helper.setSubject("Testing from Spring Boot");
helper.setText("Find the attached image", true);
helper.addAttachment("hero.jpg", new ClassPathResource("hero.jpg"));
javaMailSender.send(msg);
}
}