From 186cccdd95b711f5c06525823f2979927e0aab7f Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Mon, 24 Aug 2020 12:02:44 +0200 Subject: [PATCH] Added enpoint /getCoursesWithGroups --- .../java/com/plannaplan/entities/Course.java | 7 ++++- .../controllers/CoursesController.java | 28 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Course.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Course.java index 1987d16..be2767e 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Course.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Course.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @@ -16,7 +17,7 @@ public class Course { private Long id; private String name; private String symbol; - @OneToMany(mappedBy = "id") + @OneToMany(mappedBy = "courseId", fetch = FetchType.EAGER) private List groups = new ArrayList<>(); public Course() { @@ -42,4 +43,8 @@ public class Course { this.name = name; } + public List getGroups(){ + return this.groups; + } + } \ No newline at end of file diff --git a/restservice/src/main/java/com/plannaplan/controllers/CoursesController.java b/restservice/src/main/java/com/plannaplan/controllers/CoursesController.java index bc01b99..f6e2d38 100644 --- a/restservice/src/main/java/com/plannaplan/controllers/CoursesController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/CoursesController.java @@ -6,6 +6,7 @@ import java.util.Hashtable; import java.util.List; import com.plannaplan.entities.Course; +import com.plannaplan.entities.Groups; import com.plannaplan.services.CourseService; import org.springframework.beans.factory.annotation.Autowired; @@ -35,4 +36,31 @@ public class CoursesController { return new ResponseEntity<>(response, HttpStatus.OK); } + @GetMapping("/getCoursesWithGroups") + public ResponseEntity>> getCoursesWithGroups() { + List courses = this.courseService.getAllCourses(); + List> response = new ArrayList<>(); + for(Course c : courses){ + Dictionary element = new Hashtable<>(); + element.put("id", c.getId()); + element.put("name",c.getName()); + List> groups = new ArrayList<>(); + for(Groups g : c.getGroups()){ + Dictionary group = new Hashtable<>(); + group.put("id", g.getId()); + group.put("day", g.getDay().label); + group.put("time", g.getTimeString()); + group.put("lecturer", g.getLecturer().toString()); + group.put("room", g.getRoom()); + group.put("type", g.getType()); + groups.add(group); + } + + element.put("groups", groups); + response.add(element); + } + + return new ResponseEntity<>(response, HttpStatus.OK); + } + } \ No newline at end of file