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

Jassem-Mouadh #7

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
Expand Up @@ -4,13 +4,12 @@
import de.tekup.studentsabsence.entities.Absence;
import de.tekup.studentsabsence.entities.Group;
import de.tekup.studentsabsence.entities.Student;
import de.tekup.studentsabsence.entities.Subject;
import de.tekup.studentsabsence.enums.LevelEnum;
import de.tekup.studentsabsence.enums.SpecialityEnum;
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.SubjectService;
import de.tekup.studentsabsence.repositories.StudentRepository;
import de.tekup.studentsabsence.services.*;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
Expand All @@ -28,6 +27,8 @@ public class GroupController {
private final SubjectService subjectService;
private final GroupSubjectService groupSubjectService;
private final AbsenceService absenceService;
private final StudentService studentService;
private final StudentRepository studentRepository;

@GetMapping({"", "/"})
public String index(Model model) {
Expand Down Expand Up @@ -140,6 +141,19 @@ 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
if (bindingResult.hasErrors()|| students == null){
Group group = groupService.getGroupById(id);
model.addAttribute("group",group);
model.addAttribute("absence",absence);
model.addAttribute("groupSubjects",groupSubjectService.getSubjectsByGroupId(id));
model.addAttribute("students", group.getStudents());
return "group/add-absences";
}
for(Student student1 : students){
absence.setId(null);
absence.setStudent(student1);
absenceService.addAbsence(absence);
}
return "redirect:/groups/"+id+"/add-absences";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package de.tekup.studentsabsence.controllers;

import de.tekup.studentsabsence.entities.Group;
import de.tekup.studentsabsence.entities.Image;
import de.tekup.studentsabsence.entities.Student;
import de.tekup.studentsabsence.holders.GroupSubjectHolder;
import de.tekup.studentsabsence.services.GroupService;
import de.tekup.studentsabsence.services.ImageService;
import de.tekup.studentsabsence.services.StudentService;
Expand Down Expand Up @@ -48,15 +50,18 @@ public String add(@Valid Student student, BindingResult bindingResult, Model mod
model.addAttribute("groups", groupService.getAllGroups());
return "students/add";
}

studentService.addStudent(student);
return "redirect:/students";
}

@GetMapping("/{sid}/update")
public String updateView(@PathVariable Long sid, Model model) {
model.addAttribute("student", studentService.getStudentBySid(sid));
model.addAttribute("groups", groupService.getAllGroups());
List<Group> groups = groupService.getAllGroups();
model.addAttribute("groups", groups);
System.out.println(

);
return "students/update";
}

Expand Down Expand Up @@ -91,8 +96,14 @@ public String addImageView(@PathVariable Long sid, Model model) {

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

//TODO complete the body of this method
Image image = imageService.addImage(multipartFile);
Student student1 = studentService.getStudentBySid(sid);
student1.setImage(image);
studentService.updateStudent(student1);

return "redirect:/students";
}

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/de/tekup/studentsabsence/entities/Absence.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.tekup.studentsabsence.entities;

import com.fasterxml.jackson.annotation.JsonBackReference;
import de.tekup.studentsabsence.services.StudentService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -28,6 +30,10 @@ public class Absence implements Serializable {
@Positive(message = "Should be positive")
private float hours;
//TODO Complete Relations with other entities

@JsonBackReference
@ManyToOne
private Student student;
@ManyToOne
private Subject subject;

}
7 changes: 7 additions & 0 deletions src/main/java/de/tekup/studentsabsence/entities/Group.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.tekup.studentsabsence.entities;

import com.fasterxml.jackson.annotation.JsonBackReference;
import de.tekup.studentsabsence.enums.LevelEnum;
import de.tekup.studentsabsence.enums.SpecialityEnum;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -30,6 +31,12 @@ public class Group {
@Enumerated(EnumType.STRING)
private SpecialityEnum speciality;
//TODO Complete Relations with other entities
@JsonBackReference
@OneToMany(mappedBy = "group")
private List<Student> student;

@ManyToMany
private List<Subject> subjects;



Expand Down
19 changes: 19 additions & 0 deletions src/main/java/de/tekup/studentsabsence/entities/Student.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.tekup.studentsabsence.entities;

import com.fasterxml.jackson.annotation.JsonBackReference;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -21,14 +22,32 @@ public class Student implements Serializable {

@Id
private Long sid;
@NotBlank(message = "firstName is required")
private String firstName;
@NotBlank(message = "lastName is required")
private String lastName;
@Email
@NotBlank(message = "Email is required")
private String email;
@NotBlank(message = "Phone is required")
private String phone;
@DateTimeFormat(pattern = "dd-MM-yyyy")
@Past(message = "Should be a date in the past")
@NotNull(message = "Date of birth is required")
private LocalDate dob;

//TODO Complete Relations with other entities
@JsonBackReference
@ManyToOne
private Group group;

@JsonBackReference
@OneToMany(mappedBy = "student")
private List<Absence> absences;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "image_id", referencedColumnName = "id")
private Image image ;



Expand Down
11 changes: 7 additions & 4 deletions src/main/java/de/tekup/studentsabsence/entities/Subject.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;

Expand All @@ -22,4 +19,10 @@ public class Subject implements Serializable {
@NotBlank(message = "Name is required")
private String name;

@OneToOne
private Absence absence;

@ManyToMany
private List<Group> groups;

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public interface AbsenceRepository extends CrudRepository<Absence, Long> {
List<Absence> findAllByStudent_Sid(Long sid);
List<Absence> findAllByStudent_SidAndSubject_Id(Long sid, Long id);
List<Absence> findAllByStudent_Group_IdAndSubject_Id(Long gid, Long id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
public interface GroupSubjectRepository extends CrudRepository<GroupSubject, GroupSubjectKey> {
List<GroupSubject> findAllByGroup(Group id);
///TODO create a methode to find a groupSubject by Group Id and Subject Id
GroupSubject findAllByGroupId_AndAndSubjectId(Long gid, Long sid);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
import org.springframework.data.repository.CrudRepository;

public interface ImageRepository extends CrudRepository<Image, String> {
Image findImageById(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ public List<Absence> getAllAbsencesByGroupId(Long id) {
public List<Absence> getAllAbsencesByStudentId(Long sid) {
List<Absence> absences = new ArrayList<>();
//TODO complete the missing instructions
absenceRepository.findAllByStudent_Sid(sid).forEach(absences ::add);
return absences;
}

@Override
public List<Absence> getAllAbsencesByStudentIdAndSubjectId(Long sid, Long id) {
List<Absence> absences = new ArrayList<>();
//TODO complete the missing instructions
absenceRepository.findAllByStudent_SidAndSubject_Id(sid , id).forEach(absences::add);
return absences;
}

Expand Down Expand Up @@ -86,6 +88,9 @@ public float hoursCountByStudentAndSubject(Long sid, Long id) {
}
//TODO Complete the countHours method
public float countHours(List<Absence> absences) {
for (Absence absence : absences) {
hours=hours+absence.getHours();
}

return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public List<GroupSubject> getSubjectsByGroupId(Long id) {
public void deleteSubjectFromGroup(Long gid, Long sid) {
//TODO find a groupSubject by Group Id and Subject Id
GroupSubject groupSubject = null;
GroupSubject groupSubject = groupSubjectRepository.findAllByGroupId_AndAndSubjectId(gid,sid);

groupSubjectRepository.delete(groupSubject);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class ImageServiceImp implements ImageService {
//TODO Complete this method
@Override
public Image getImage(String id) {
return null;
return null ;
return imageRepository.findImageById(id);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@AllArgsConstructor
public class StudentServiceImp implements StudentService {
private final StudentRepository studentRepository;
private final StudentRepository studentRepository;

@Override
public List<Student> getAllStudents() {
Expand All @@ -38,11 +39,15 @@ public Student addStudent(Student student) {
@Override
public Student updateStudent(Student student) {
return null;
return studentRepository.save(student);
}

//TODO Complete this method
@Override
public Student deleteStudent(Long sid) {
return null;
Student student = getStudentBySid(sid);
studentRepository.delete(student);
return (student);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class SubjectServiceImp implements SubjectService {
@Override
public List<Subject> getAllSubjects() {
return null;
return (List<Subject>) subjectRepository.findAll();
}

@Override
Expand Down