Ds Spring
Ds Spring
java
package estl.gi.workshop;
import java.util.*;
import jakarta.persistence.*;
import lombok.*;
// Lombok annotations
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Student {
@Id
private String cne;
private String firstName;
private String secondName;
private String email;
private String login;
private String password;
@ManyToMany
@JoinTable(
name = "participation",
joinColumns = @JoinColumn(name = "student_cne"),
inverseJoinColumns = @JoinColumn(name = "workshop_code"),
uniqueConstraints = @UniqueConstraint(columnNames =
{"student_cne", "workshop_code"})
)
@ToString.Exclude // Avoid potential recursion issues
private List<Workshop> workshops = new ArrayList<>();
}
Workshop.java
package estl.gi.workshop;
import java.util.*;
import org.springframework.format.annotation.DateTimeFormat;
import jakarta.persistence.*;
import lombok.*;
// Lombok annotations
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Workshop {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long code;
private String name;
private String moderator;
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm")
private Date start;
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm")
private Date end;
@ManyToMany(mappedBy = "workshops")
@ToString.Exclude // Avoid recursive issues in toString
private List<Student> students = new ArrayList<>();
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface WorkshopRepository extends
JpaRepository<Workshop, Long> {
}
StudentRepository«java
package est.gi.hightecheventplatform.student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends JpaRepository<Student,
String> {
}
WorkshopController.java
package estl.gi.workshop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/workshops")
public class WorkshopController {
@Autowired
private WorkshopService workshopService;
@GetMapping
public String getWorkshops(Model model) {
model.addAttribute("workshops",
workshopService.getAllWorkshops());
return "all-workshops";
}
@GetMapping("/add")
public String addWorkshop() {
return "add-workshop";
}
@PostMapping("/create")
public String postWorkshop(Workshop workshop) {
workshopService.createWorkshop(workshop);
return "redirect:/workshops";
}
@GetMapping("/view")
public String getWorkshop(@RequestParam(name = "code") Long
code, Model model) {
model.addAttribute("workshop",
workshopService.getWorkshop(code));
return "update-workshop";
}
@PostMapping("/update")
public String putWorkshop(Workshop workshop) {
workshopService.updateWorkshop(workshop);
return "redirect:/workshops";
}
@GetMapping("/delete")
public String deleteWorkshop(@RequestParam(name = "code") Long
code) {
workshopService.removeWorkshop(code);
return "redirect:/workshops";
}
@GetMapping("/availables")
public String getAvailableWorkshops(@RequestParam(value = "cne")
String cne, Model
model) {
model.addAttribute("cne", cne);
model.addAttribute("fullname",
workshopService.getStudentFullNameByCne(cne));
model.addAttribute("workshops",
workshopService.getAvailableWorkshops(cne));
model.addAttribute("participations",
workshopService.getParticipations(cne));
return "available-workshops";
}
}
WorkshopService.java
package estl.gi.workshop;
import java.util.*;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class WorkshopService {
@Autowired
WorkshopRepository workshopRepository;
@Autowired
private StudentRepository studentRepository;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping
public String index() {
return "index";
}
@PostMapping("/login")
public String login(@ModelAttribute Student student, Model model)
{
Optional<Student> optionalStudent =
studentService.getStudentByLoginAndPassword(student.getLogin(),
student.getPassword());
if (optionalStudent.isPresent()) {
return "redirect:/workshops/availables?cne=" +
optionalStudent.get().getCne();}
else {
model.addAttribute("msg", "Échec de l'authentification. Veuillez
vérifier votre Login/Mot de passe.");
return "index";}
}
@GetMapping("/register")
public String registerStudent() {
return "registration";
}
@PostMapping("/create")
public String postStudent(@ModelAttribute Student student) {
studentService.createStudent(student);
return "redirect:/workshops/availables?cne=" +
student.getCne();
}
@PostMapping("/participate")
public String participate(@RequestParam("studentCNE") String
studentCNE,@RequestParam(value = "workshopCodes", required
= false) List<Long> workshopCodes) {
studentService.participateInWorkshops(studentCNE,
workshopCodes);
return "redirect:/workshops/availables?cne=" + studentCNE;
}
}
StudentService.java
package estl.gi.workshop;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
@Autowired
private WorkshopRepository workshopRepository;
@Transactional
public void participateInWorkshops(String studentCNE, List<Long>
workshopCodes) {
Student student =
studentRepository.findById(studentCNE).orElse(null);
if (student != null && workshopCodes != null) {
workshopCodes.retainAll(workshopRepository.getCodesOfAvailableW
orkshops(studentCNE));
List<Workshop> workshops =
workshopRepository.findAllById(workshopCodes);
student.setWorkshops(workshops);
studentRepository.save(student);
}
}
}
pom.xml
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>