0% found this document useful (0 votes)
6 views13 pages

Ds Spring

The document contains Java code for a workshop management system using Spring Boot, JPA, and Lombok. It includes entity classes for Student and Workshop, repositories for data access, controllers for handling web requests, and services for business logic. Additionally, it provides configuration details for dependencies and application properties for database connectivity.

Uploaded by

lahcen.zaghmane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Ds Spring

The document contains Java code for a workshop management system using Spring Boot, JPA, and Lombok. It includes entity classes for Student and Workshop, repositories for data access, controllers for handling web requests, and services for business logic. Additionally, it provides configuration details for dependencies and application properties for database connectivity.

Uploaded by

lahcen.zaghmane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

student.

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;

@Column(length = 10000, columnDefinition = "TEXT")


private String description;

private int seats;

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

public void removeParticipations() {


for (Student student : students) {
student.getWorkshops().remove(this);
}
students.clear();
}
}
WorkshopRepository«java
package est.gi.hightecheventplatform.workshop;

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;

public List<Workshop> getAllWorkshops() {


return workshopRepository.findAll();
}
public Workshop getWorkshop(Long code) {
return workshopRepository.findById(code).get();
}
public String createWorkshop(Workshop workshop) {
workshopRepository.save(workshop);
return "Workshop added successfully";
}
public void removeWorkshop(Long workshopCode) {
Workshop workshop = workshopRepository.findById(workshopCode).get();
workshop.removeParticipations();
workshopRepository.save(workshop);
workshopRepository.deleteById(workshopCode);
}
public String updateWorkshop(Workshop workshop) {
Optional<Workshop> WorkshopOptional =
workshopRepository.findById(workshop.getCode());
if (WorkshopOptional.isPresent()) {
workshopRepository.save(workshop);
return "Workshop updated successfully";
}
return "Workshop not found";
}
public List<Object[]> getAvailableWorkshops(String cne) {
List<Object[]> availableWorkshops =
workshopRepository.findAvailableWorkshops(cne);
return availableWorkshops;
}
public List<Long> getParticipations(String cne) {
Student student = studentRepository.findById(cne).orElse(null);
if (student != null) {
List<Workshop> workshops = student.getWorkshops();
return
workshops.stream().map(Workshop::getCode).collect(Collectors.toList());
}
return Collections.emptyList();
}
public String getStudentFullNameByCne(String cne) {
return studentRepository.findFullNameByCne(cne);
}
}
StudentController.java
package estl.gi.workshop;

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;

public List<Student> getAllStudents() {


return studentRepository.findAll();
}

public Optional<Student> getStudent(String cne) {


return studentRepository.findById(cne);
}

public String createStudent(Student student) {


studentRepository.save(student);
return "Student added successfully";
}

public void removeStudent(String id) {


studentRepository.deleteById(id);
}

public String updateStudent(Student student) {


Optional<Student> studentOptional =
studentRepository.findById(student.getCne());
if (studentOptional.isPresent()) {
studentRepository.save(student);
return "Student updated successfully";
}
return "Student not found";
}
public Optional<Student> getStudentByLoginAndPassword(String
login, String password) {
return studentRepository.findByLoginAndPassword(login, password);
}

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

<!-- Spring Boot Starter Data JPA -->


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- Spring Boot Starter Thymeleaf -->


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<!-- MySQL Connector -->


<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>

<!-- Lombok -->


<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<!-- Spring Boot Starter Test -->


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.propriets
server.port=8080
spring.datasource.url=jdbc:mysql://152.68.14.73:3376/hightecheventdb
spring.datasource.username=root
[email protected]

You might also like