Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mohamed Bengayed-Ala-Mtiri #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package de.tekup.studentsabsence.controllers;


import de.tekup.studentsabsence.entities.Absence;
import de.tekup.studentsabsence.entities.Group;
import de.tekup.studentsabsence.entities.Student;
Expand All @@ -9,7 +7,7 @@
import de.tekup.studentsabsence.holders.GroupSubjectHolder;
import de.tekup.studentsabsence.services.AbsenceService;
import de.tekup.studentsabsence.services.GroupService;
import de.tekup.studentsabsence.services.GroupSubjectService;
import de.tekup.studentsabsence.services.GroupSubService;
import de.tekup.studentsabsence.services.SubjectService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
Expand All @@ -26,9 +24,10 @@
public class GroupController {
private final GroupService groupService;
private final SubjectService subjectService;
private final GroupSubjectService groupSubjectService;
private final GroupSubService groupSubjectService;
private final AbsenceService absenceService;


@GetMapping({"", "/"})
public String index(Model model) {
List<Group> groups = groupService.getAllGroups();
Expand All @@ -45,7 +44,7 @@ public String addView(Model model) {
}

@PostMapping("/add")
public String add(@Valid Group group, BindingResult bindingResult, Model model) {
public String add(@Valid @ModelAttribute("group") Group group, BindingResult bindingResult, Model model) {
if(bindingResult.hasErrors()) {
model.addAttribute("levels", LevelEnum.values());
model.addAttribute("specialities", SpecialityEnum.values());
Expand Down Expand Up @@ -89,7 +88,6 @@ public String show(@PathVariable long id, Model model) {
model.addAttribute("groupSubjects",groupSubjectService.getSubjectsByGroupId(id));
model.addAttribute("students",group.getStudents());
model.addAttribute("absenceService", absenceService);

group.getStudents().forEach(student -> {

});
Expand Down Expand Up @@ -139,8 +137,22 @@ public String addAbsenceView(@PathVariable long id, Model model) {

@PostMapping("/{id}/add-absences")
public String addAbsence(@PathVariable long id, @Valid Absence absence, BindingResult bindingResult, @RequestParam(value = "students", required = false) List<Student> students, Model model) {
//TODO Complete the body of this method
//TODO method add-abscences (executable)

if(bindingResult.hasErrors())
{
return "groups/add-absences";
}
if(!students.isEmpty()){
for(int i=0; i<students.size(); i++){
Absence abs=new Absence();
abs.setHours(absence.getHours());
abs.setStudent(students.get(i));
abs.setSubject(absence.getSubject());
abs.setStartDate(absence.getStartDate());
absenceService.addAbsence(abs); }}
return "redirect:/groups/"+id+"/add-absences";
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import de.tekup.studentsabsence.entities.Image;
import de.tekup.studentsabsence.entities.Student;
import de.tekup.studentsabsence.services.GroupService;
import de.tekup.studentsabsence.services.ImageService;
import de.tekup.studentsabsence.services.ImService;
import de.tekup.studentsabsence.services.StudentService;
import lombok.AllArgsConstructor;
import org.apache.tomcat.util.http.fileupload.IOUtils;
Expand All @@ -19,14 +19,13 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Controller
@AllArgsConstructor
@RequestMapping("/students")
public class StudentController {
private final StudentService studentService;
private final GroupService groupService;
private final ImageService imageService;
private final ImService imageService;

@GetMapping({"", "/"})
public String index(Model model) {
Expand All @@ -43,7 +42,9 @@ public String addView(Model model) {
}

@PostMapping("/add")
public String add(@Valid Student student, BindingResult bindingResult, Model model) {
public String add(@Valid @ModelAttribute("student") Student student, BindingResult bindingResult, Model model) {


if(bindingResult.hasErrors()) {
model.addAttribute("groups", groupService.getAllGroups());
return "students/add";
Expand All @@ -53,6 +54,7 @@ public String add(@Valid Student student, BindingResult bindingResult, Model mod
return "redirect:/students";
}


@GetMapping("/{sid}/update")
public String updateView(@PathVariable Long sid, Model model) {
model.addAttribute("student", studentService.getStudentBySid(sid));
Expand All @@ -61,7 +63,7 @@ public String updateView(@PathVariable Long sid, Model model) {
}

@PostMapping("/{sid}/update")
public String update(@PathVariable Long sid, @Valid Student student, BindingResult bindingResult, Model model) {
public String update(@PathVariable Long sid, @Valid @ModelAttribute("student") Student student, BindingResult bindingResult, Model model) {
if(bindingResult.hasErrors()) {
model.addAttribute("groups", groupService.getAllGroups());
return "students/update";
Expand Down Expand Up @@ -90,9 +92,25 @@ public String addImageView(@PathVariable Long sid, Model model) {
}

@PostMapping("/{sid}/add-image")
//TODO complete the parameters of this method
public String addImage() {
//TODO complete the body of this method
//TODO complete the parameters of this method (c bon )
public String addImage(@PathVariable Long sid,@RequestParam("image") MultipartFile image) throws IOException {

Student s=studentService.getStudentBySid(sid);


if(!image.isEmpty()){

Image img= null;
try {
img = imageService.addImage(image);
} catch (IOException e) {
throw new RuntimeException(e);
}
s.setImage(img);
studentService.updateStudent(s);


}
return "redirect:/students";
}

Expand All @@ -108,4 +126,15 @@ public void getStudentPhoto(HttpServletResponse response, @PathVariable("sid") l
}
}

@GetMapping("/{sid}/{subid}/mail")

public String EliminationMail(@PathVariable Long sid,@PathVariable Long subid) {
try {


}catch (Exception e){
System.out.println("L email n a pas pu envoyer");
}
return "subjects/subjectsAbsence";
}
}
7 changes: 6 additions & 1 deletion src/main/java/de/tekup/studentsabsence/entities/Absence.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ public class Absence implements Serializable {
@NotNull(message = "Hours is required")
@Positive(message = "Should be positive")
private float hours;
//TODO Complete Relations with other entities
//TODO Relations with other entities (OK)
@ManyToOne
public Student student;
@OneToOne
public Subject subject;



}
10 changes: 9 additions & 1 deletion src/main/java/de/tekup/studentsabsence/entities/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@Entity
@Data
@ToString(exclude = "students")
Expand All @@ -29,8 +31,14 @@ public class Group {
@NotNull(message = "Speciality is required")
@Enumerated(EnumType.STRING)
private SpecialityEnum speciality;
//TODO Complete Relations with other entities

//TODO Complete Relations with other entities (OK)
@OneToMany(mappedBy = "group")
public List<Student> students;

@OneToMany
public List<GroupSubject> groupSubjects;


}

Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public class GroupSubjectKey implements Serializable {
private Long groupId ;
@Column(name = "subject_id")
private Long subjectId;

}
1 change: 1 addition & 0 deletions src/main/java/de/tekup/studentsabsence/entities/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ public class Image {
private String fileType;
@Lob
private byte[] data;

}
15 changes: 13 additions & 2 deletions src/main/java/de/tekup/studentsabsence/entities/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import javax.validation.constraints.*;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.List;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString(exclude = {"image","group","absences"})
public class Student implements Serializable {
//TODO Complete Validations of fields
//TODO Complete Validations of fields (OK)


@Id
Expand All @@ -28,7 +30,16 @@ public class Student implements Serializable {
@DateTimeFormat(pattern = "dd-MM-yyyy")
private LocalDate dob;

//TODO Complete Relations with other entities
//TODO Relations with other entities (OK)
@NotNull(message = "Ce champs est obligatoire")
@ManyToOne
public Group group;

@OneToMany
public List<Absence>absences;

@OneToOne
public Image image;



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.tekup.studentsabsence.repositories;

import de.tekup.studentsabsence.entities.Group;
import de.tekup.studentsabsence.entities.GroupSubject;
import de.tekup.studentsabsence.entities.GroupSubjectKey;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface GroupSubRepository extends CrudRepository<GroupSubject, GroupSubjectKey> {
List<GroupSubject> findAllByGroup(Group id);
///TODO create a methode to find a groupSubject by Group Id and Subject Id(OK)
GroupSubject findByGroupIdAndSubjectId(Long gid, Long sid);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.tekup.studentsabsence.repositories;

import de.tekup.studentsabsence.entities.Image;
import org.springframework.data.repository.CrudRepository;

public interface ImRepository extends CrudRepository<Image, String> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.tekup.studentsabsence.services;

import de.tekup.studentsabsence.entities.Group;
import de.tekup.studentsabsence.entities.GroupSubject;
import de.tekup.studentsabsence.entities.Subject;

import java.util.List;

public interface GroupSubService {
void addSubjectToGroup(Group group, Subject subject, float hours);
List<GroupSubject> getSubjectsByGroupId(Long id);
void deleteSubjectFromGroup(Long gid, Long sid);
}
12 changes: 12 additions & 0 deletions src/main/java/de/tekup/studentsabsence/services/ImService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.tekup.studentsabsence.services;

import de.tekup.studentsabsence.entities.Image;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

public interface ImService {
Image getImage(String id);

Image addImage(MultipartFile image) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package de.tekup.studentsabsence.services.impl;

import de.tekup.studentsabsence.entities.Group;
import de.tekup.studentsabsence.entities.GroupSubject;
import de.tekup.studentsabsence.entities.GroupSubjectKey;
import de.tekup.studentsabsence.entities.Subject;
import de.tekup.studentsabsence.repositories.GroupSubRepository;
import de.tekup.studentsabsence.services.GroupService;
import de.tekup.studentsabsence.services.GroupSubService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class GroupSubServiceImp implements GroupSubService {
private final GroupSubRepository groupSubjectRepository;
private final GroupService groupService;

@Override
public void addSubjectToGroup(Group group, Subject subject, float hours) {
groupSubjectRepository.save(new GroupSubject(
new GroupSubjectKey(group.getId(),subject.getId()),
group,
subject,
hours
));
}

@Override
public List<GroupSubject> getSubjectsByGroupId(Long id) {
Group group = groupService.getGroupById(id);
return new ArrayList<>(groupSubjectRepository.findAllByGroup(group));
}

@Override
public void deleteSubjectFromGroup(Long gid, Long sid) {
//TODO find a groupSubject by Group Id and Subject Id (OK)
GroupSubject groupSubject = groupSubjectRepository.findByGroupIdAndSubjectId(gid,sid);

groupSubjectRepository.delete(groupSubject);
}


}