From fa749655040725629ae14a65eb9ea66ef0077991 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Wed, 30 Sep 2020 19:15:32 +0200 Subject: [PATCH] Works --- .../com/plannaplan/entities/Assignment.java | 11 +++ .../repositories/AssignmentRepository.java | 7 ++ .../repositories/CommisionRepository.java | 3 + .../services/AssignmentService.java | 10 ++- .../plannaplan/services/CommisionService.java | 5 ++ .../controllers/AssignmentsController.java | 67 +++++++++++++++++++ .../controllers/CommisionController.java | 20 +----- .../controllers/TokenBasedController.java | 29 ++++++++ 8 files changed, 131 insertions(+), 21 deletions(-) create mode 100755 restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java create mode 100755 restservice/src/main/java/com/plannaplan/controllers/TokenBasedController.java diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java index 8214dbd..cb0f8ff 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java @@ -23,4 +23,15 @@ public class Assignment { this.commision = commision; this.group = group; } + + public Assignment() { + } + + public Long getId() { + return this.id; + } + + public Groups getGroup() { + return this.group; + } } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/AssignmentRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/AssignmentRepository.java index 19af52d..929525b 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/AssignmentRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/AssignmentRepository.java @@ -1,11 +1,18 @@ package com.plannaplan.repositories; +import java.util.List; + import com.plannaplan.entities.Assignment; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @Repository public interface AssignmentRepository extends JpaRepository { + @Query("FROM Assignment WHERE commision_id = ?1") + List getByCommision(@Param("commision_id") Long id); + } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/CommisionRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/CommisionRepository.java index 650569f..4f86813 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/CommisionRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/CommisionRepository.java @@ -14,4 +14,7 @@ public interface CommisionRepository extends JpaRepository { @Query("FROM Commision WHERE owner_id = ?1") List getUsers(@Param("owner_id") Long id); + @Query("FROM Commision WHERE owner_id = ?1 order by commisionDate desc") + List getNewestCommision(@Param("owner_id") Long id); + } diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/AssignmentService.java b/buisnesslogic/src/main/java/com/plannaplan/services/AssignmentService.java index b34633a..734d554 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/AssignmentService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/AssignmentService.java @@ -1,14 +1,16 @@ package com.plannaplan.services; -import com.plannaplan.abstracts.EventWatcher; +import java.util.List; + import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Commision; import com.plannaplan.repositories.AssignmentRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service -public class AssignmentService extends EventWatcher { +public class AssignmentService { @Autowired private AssignmentRepository repo; @@ -19,4 +21,8 @@ public class AssignmentService extends EventWatcher { public void save(Assignment assignment) { this.repo.save(assignment); } + + public List getCommisionAssignments(Commision com) { + return this.repo.getByCommision(com.getId()); + } } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java b/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java index 8bef5bc..77700be 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java @@ -1,6 +1,7 @@ package com.plannaplan.services; import java.util.List; +import java.util.Optional; import com.plannaplan.entities.Commision; import com.plannaplan.entities.User; @@ -27,4 +28,8 @@ public class CommisionService { Long id = user.getId(); return this.repo.getUsers(id); } + + public Optional getNewestCommision(User user) { + return Optional.of(this.repo.getNewestCommision(user.getId()).get(0)); + } } diff --git a/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java b/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java new file mode 100755 index 0000000..5265287 --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java @@ -0,0 +1,67 @@ +package com.plannaplan.controllers; + +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.ArrayList; +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.List; +import java.util.Optional; + +import com.plannaplan.App; +import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Commision; +import com.plannaplan.entities.Groups; +import com.plannaplan.entities.User; +import com.plannaplan.services.AssignmentService; +import com.plannaplan.services.CommisionService; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; + +@RestController +@CrossOrigin +@RequestMapping("/api/" + App.API_VERSION + "/assignments") +public class AssignmentsController extends TokenBasedController { + + @Autowired + private CommisionService commisionService; + + @Autowired + private AssignmentService assignmentService; + + @GetMapping("/getCurrentAssignments") + public ResponseEntity> getCurrentAssignments() { + User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException()); + Optional com = this.commisionService.getNewestCommision(user); + if (com.isPresent()) { + List respone = this.assignmentService.getCommisionAssignments(com.get()); + + List finalResponse = new ArrayList<>(); + for (Assignment a : respone) { + Dictionary elem = new Hashtable<>(); + Dictionary groupInfo = new Hashtable<>(); + elem.put("id", a.getId()); + Groups g = a.getGroup(); + groupInfo.put("id", g.getId()); + groupInfo.put("id", g.getId()); + groupInfo.put("day", g.getDay().label); + groupInfo.put("time", g.getTimeString()); + groupInfo.put("lecturer", g.getLecturer().toString()); + groupInfo.put("room", g.getRoom()); + groupInfo.put("capacity", g.getCapacity()); + groupInfo.put("type", g.getType()); + elem.put("group", groupInfo); + finalResponse.add(elem); + } + + return new ResponseEntity<>(finalResponse, HttpStatus.OK); + } + + return new ResponseEntity<>(null, HttpStatus.OK); + } +} diff --git a/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java index f288585..9172efb 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java @@ -5,7 +5,6 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; -import java.util.Optional; import com.plannaplan.App; import com.plannaplan.entities.Assignment; @@ -15,22 +14,18 @@ import com.plannaplan.entities.User; import com.plannaplan.services.AssignmentService; import com.plannaplan.services.CommisionService; import com.plannaplan.services.GroupService; -import com.plannaplan.services.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.security.authentication.AnonymousAuthenticationToken; @RestController @CrossOrigin @RequestMapping("/api/" + App.API_VERSION + "/commisions") -public class CommisionController { +public class CommisionController extends TokenBasedController { @Autowired private CommisionService commisionService; @@ -41,9 +36,6 @@ public class CommisionController { @Autowired private AssignmentService assignmentService; - @Autowired - private UserService userService; - public CommisionController() { } @@ -70,14 +62,4 @@ public class CommisionController { return new ResponseEntity<>(result, HttpStatus.OK); } - private Optional getCurrentUser() { - Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - - if (!(authentication instanceof AnonymousAuthenticationToken)) { - return Optional.of(this.userService.getUserByEmail(authentication.getName())); - } else { - return Optional.empty(); - } - } - } diff --git a/restservice/src/main/java/com/plannaplan/controllers/TokenBasedController.java b/restservice/src/main/java/com/plannaplan/controllers/TokenBasedController.java new file mode 100755 index 0000000..7541c2b --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/controllers/TokenBasedController.java @@ -0,0 +1,29 @@ +package com.plannaplan.controllers; + +import java.util.Optional; + +import com.plannaplan.entities.User; +import com.plannaplan.services.UserService; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.authentication.AnonymousAuthenticationToken; + +public abstract class TokenBasedController { + @Autowired + protected UserService userService; + + public TokenBasedController() { + } + + protected Optional getCurrentUser() { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + + if (!(authentication instanceof AnonymousAuthenticationToken)) { + return Optional.of(this.userService.getUserByEmail(authentication.getName())); + } else { + return Optional.empty(); + } + } +} \ No newline at end of file