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

projet spring boot #16

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
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<description>StudentsAbsence</description>
<properties>
<java.version>11</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,48 @@ 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()) {
model.addAttribute("group",groupService.getGroupById(id));
model.addAttribute("subjects",groupService.getGroupById(id).getGroupSubjects());
model.addAttribute("students",groupService.getGroupById(id).getStudents());

return "groups/add-absences";
}
Absence absence1=new Absence();
absence1.setSubject(absence.getSubject());
absence1.setHours(absence.getHours());
absence1.setStartDate(absence.getStartDate());



for (Student student:students
) {
absence1.setStudent(student);

absenceService.addAbsence(absence1);


}














return "redirect:/groups/"+id+"/add-absences";
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
Expand Down Expand Up @@ -91,7 +92,19 @@ 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(@PathVariable Long sid,@RequestParam("image") MultipartFile file) throws IOException {


Student student=studentService.getStudentBySid(sid);
Image image=new Image();
image.setStudent(student);
image.setData(file.getBytes());
image.setFileName(StringUtils.cleanPath(file.getOriginalFilename()));
image.setFileType(file.getContentType());
student.setImage(image);
studentService.updateStudent(student);


//TODO complete the body of this method
return "redirect:/students";
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/de/tekup/studentsabsence/entities/Absence.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
Expand All @@ -12,10 +13,12 @@
import java.io.Serializable;
import java.time.LocalDateTime;


@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString(exclude = {"subject"})
public class Absence implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -29,5 +32,13 @@ public class Absence implements Serializable {
private float hours;
//TODO Complete Relations with other entities

// relation Many to one (Student ,absence )
@ManyToOne
private Student student;
// relation one to one (subject ,absence )
@OneToOne
private Subject subject;



}
11 changes: 11 additions & 0 deletions 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,7 +31,16 @@ public class Group {
@NotNull(message = "Speciality is required")
@Enumerated(EnumType.STRING)
private SpecialityEnum speciality;

//TODO Complete Relations with other entities
// relation one to many (Student ,group )
@OneToMany(mappedBy = "group")
private List<Student> students;

// relation one to many (Group ,GroupSubject )
@OneToMany(mappedBy = "group")
private List<GroupSubject> groupSubjects;




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ public class GroupSubject implements Serializable {
@EmbeddedId
private GroupSubjectKey id;



// relation many to one (GroupSubject ,group )
@ManyToOne
@MapsId("group_id")
@MapsId("groupId")
@JoinColumn(name = "group_id")
private Group group;

// relation many to one (GroupSubject ,Subject )
@ManyToOne
@MapsId("subject_id")
@MapsId("subjectId")
@JoinColumn(name = "subject_id")
private Subject subject;

private float hours;

}
13 changes: 9 additions & 4 deletions src/main/java/de/tekup/studentsabsence/entities/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.*;

@Entity
@Data
Expand All @@ -23,4 +20,12 @@ public class Image {
private String fileType;
@Lob
private byte[] data;
// relation one to one (image ,Absence )
@OneToOne(mappedBy = "image")
private Student student;
// relation one to one (subject ,absence )
@OneToOne(mappedBy = "subject")
private Absence Absence;
public Image(Object o, String fileName, String fileType, byte[] data) {
}
}
21 changes: 20 additions & 1 deletion src/main/java/de/tekup/studentsabsence/entities/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import javax.validation.constraints.*;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.List;

@Entity
@Data
@AllArgsConstructor
Expand All @@ -28,6 +29,24 @@ public class Student implements Serializable {
@DateTimeFormat(pattern = "dd-MM-yyyy")
private LocalDate dob;




//TODO Complete Relations with other entities
// relation one to one (image ,Absence )
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "image_id", referencedColumnName = "id")
Image image;

// relation many to one (Student ,group )
@ManyToOne
Group group;
// relation one to many (Student ,absence )
@OneToMany(mappedBy = "student",fetch = FetchType.LAZY)
private List<Absence> absences;



//TODO Complete Relations with other entities


Expand Down
15 changes: 11 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,22 +4,29 @@
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;
import java.util.List;


@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor


public class Subject implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "Name is required")
private String name;
// relation one to one (subject ,absence )
@OneToOne(mappedBy = "subject")
private Absence absence;
// relation one to many (Subject ,GroupSubject )
@OneToMany(mappedBy = "subject")
private List<GroupSubject> groupSubjects;

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import org.springframework.data.repository.CrudRepository;

import java.util.List;
import java.util.Optional;

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

import java.util.List;

public interface SubjectRepository extends CrudRepository<Subject, Long> {
@Override
List<Subject> findAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.io.IOException;

public interface ImageService {
Image getImage(String id);
Image getImage(String id) throws Exception;

Image addImage(MultipartFile image) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import de.tekup.studentsabsence.entities.Student;

import java.util.List;
import java.util.Optional;

public interface StudentService {
List<Student> getAllStudents();
Expand All @@ -13,5 +14,5 @@ public interface StudentService {

Student updateStudent(Student student);

Student deleteStudent(Long sid);
Optional<Student> deleteStudent(Long sid);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,25 @@ public List<Absence> getAllAbsencesByGroupId(Long id) {

@Override
public List<Absence> getAllAbsencesByStudentId(Long sid) {
List<Absence> absences = new ArrayList<>();
List<Absence> absences =absenceRepository.findAllByStudent_Sid(sid);
//TODO complete the missing instructions
return absences;
}

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

return absences;
}

@Override
public List<Absence> getAllAbsencesByGroupIdAndSubjectId(Long gid, Long id) {
List<Absence> absences = new ArrayList<>();
System.out.println(gid);
absenceRepository.findAllByStudent_Group_IdAndSubject_Id(gid, id).forEach(absences::add);
System.out.println("####################### abs#############################");
System.out.println(absences);
return absences;
}

Expand All @@ -57,6 +60,12 @@ public Absence getAbsenceById(Long id) {

@Override
public Absence addAbsence(Absence absence) {

System.out.println(absence);




return absenceRepository.save(absence);
}

Expand All @@ -82,12 +91,22 @@ public float hoursCountByGroupAndSubject(Long gid, Long id) {
@Override
public float hoursCountByStudentAndSubject(Long sid, Long id) {
List<Absence> absences = getAllAbsencesByStudentIdAndSubjectId(sid, id);

System.out.println(absences);
return countHours(absences);
}
//TODO Complete the countHours method
public float countHours(List<Absence> absences) {
float count=0 ;
for (Absence absence:absences
) {
count =count+absence.getHours();

}

System.out.println(count);

return 0;
return count;
}

}
Loading