From 4efedf7f35c48797c1e6b633ae69bb52b49795ac Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Mon, 28 Sep 2020 16:07:10 +0200 Subject: [PATCH 1/6] concept for assigmnents --- .../com/plannaplan/entities/Assignment.java | 4 +-- .../com/plannaplan/entities/Commision.java | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100755 buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java index 72e52f8..99dd236 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java @@ -16,8 +16,8 @@ public class Assignment { @JoinColumn(name = "group_id") private Groups group; @ManyToOne - @JoinColumn(name = "user_id") - private User user; + @JoinColumn(name = "commision_id") + private Commision commision; public Assignment() { } diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java new file mode 100755 index 0000000..9f93ccf --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java @@ -0,0 +1,33 @@ +package com.plannaplan.entities; + +import java.sql.Timestamp; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; + +@Entity +public class Commision { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + @ManyToOne + @JoinColumn(name = "owner_id") + private User commisionOwner; + private Timestamp commisionDate; + + public Commision() { + } + + public Timestamp getCommisionDate() { + return commisionDate; + } + + public User getCommisionOwner() { + return commisionOwner; + } + +} From 3ad4d3a84bc7926048bdc692dbd19fa27309fdf0 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Tue, 29 Sep 2020 17:21:34 +0200 Subject: [PATCH 2/6] Checkpoint kinda workls. Ill take a nap and go bacjk to work --- .../com/plannaplan/entities/Assignment.java | 4 +- .../com/plannaplan/entities/Commision.java | 17 +++++- .../repositories/CommisionRepository.java | 11 ++++ .../services/AssignmentService.java | 5 ++ .../plannaplan/services/CommisionService.java | 22 +++++++ .../com/plannaplan/services/GroupService.java | 11 +++- .../controllers/CommisionController.java | 58 +++++++++++++++++++ 7 files changed, 121 insertions(+), 7 deletions(-) create mode 100755 buisnesslogic/src/main/java/com/plannaplan/repositories/CommisionRepository.java create mode 100755 buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java create mode 100755 restservice/src/main/java/com/plannaplan/controllers/CommisionController.java diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java index 99dd236..8214dbd 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java @@ -19,6 +19,8 @@ public class Assignment { @JoinColumn(name = "commision_id") private Commision commision; - public Assignment() { + public Assignment(Groups group, Commision commision) { + this.commision = commision; + this.group = group; } } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java index 9f93ccf..950798b 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java @@ -1,25 +1,36 @@ package com.plannaplan.entities; import java.sql.Timestamp; +import java.util.List; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; +import javax.persistence.OneToOne; @Entity public class Commision { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; - @ManyToOne + @OneToOne @JoinColumn(name = "owner_id") private User commisionOwner; private Timestamp commisionDate; - public Commision() { + @OneToMany(mappedBy = "commision") + private List assignments; + + public Commision(User user) { + this.commisionDate = new Timestamp(System.currentTimeMillis()); + this.commisionOwner = user; + } + + public Long getId() { + return this.id; } public Timestamp getCommisionDate() { diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/CommisionRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/CommisionRepository.java new file mode 100755 index 0000000..f5e7908 --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/CommisionRepository.java @@ -0,0 +1,11 @@ +package com.plannaplan.repositories; + +import com.plannaplan.entities.Commision; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CommisionRepository extends JpaRepository { + +} diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/AssignmentService.java b/buisnesslogic/src/main/java/com/plannaplan/services/AssignmentService.java index 53e74cf..b34633a 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/AssignmentService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/AssignmentService.java @@ -1,6 +1,7 @@ package com.plannaplan.services; import com.plannaplan.abstracts.EventWatcher; +import com.plannaplan.entities.Assignment; import com.plannaplan.repositories.AssignmentRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -14,4 +15,8 @@ public class AssignmentService extends EventWatcher { public AssignmentService() { super(); } + + public void save(Assignment assignment) { + this.repo.save(assignment); + } } \ 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 new file mode 100755 index 0000000..0d1909b --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java @@ -0,0 +1,22 @@ +package com.plannaplan.services; + +import com.plannaplan.entities.Commision; +import com.plannaplan.repositories.CommisionRepository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class CommisionService { + @Autowired + private CommisionRepository repo; + + public CommisionService() { + } + + public Commision save(Commision commision) { + this.repo.save(commision); + return commision; + + } +} diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java b/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java index 3142411..7361b13 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java @@ -1,6 +1,7 @@ package com.plannaplan.services; import java.util.List; +import java.util.Optional; import com.plannaplan.entities.Groups; import com.plannaplan.repositories.GroupRepository; @@ -28,11 +29,15 @@ public class GroupService { this.repo.save(group); } - public void delete(Groups groups){ + public void delete(Groups groups) { this.repo.delete(groups); } - public int getGroupsAmmount(){ - return (int)this.repo.count(); + public int getGroupsAmmount() { + return (int) this.repo.count(); + } + + public Optional getGroupById(Long id) { + return this.repo.findById(id); } } \ No newline at end of file diff --git a/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java new file mode 100755 index 0000000..344da41 --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java @@ -0,0 +1,58 @@ +package com.plannaplan.controllers; + +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.RestController; + +import java.io.IOException; + +import com.plannaplan.App; +import com.plannaplan.Controller; +import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Commision; +import com.plannaplan.entities.Groups; +import com.plannaplan.models.ConfigData; +import com.plannaplan.services.AssignmentService; +import com.plannaplan.services.CommisionService; +import com.plannaplan.services.GroupService; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; + +@RestController +@CrossOrigin +@RequestMapping("/api/" + App.API_VERSION + "/commisions") +public class CommisionController { + + @Autowired + private CommisionService commisionService; + + @Autowired + private GroupService groupServcicxe; + + @Autowired + private AssignmentService assignmentService; + + public CommisionController() { + } + + @PostMapping("/add") + public String addCommision(@RequestParam("id") Long id) { + Groups group = this.groupServcicxe.getGroupById(id).orElseThrow(() -> new NullPointerException()); + Commision com = new Commision(null); + // Object principal = + // SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + + Assignment a = new Assignment(group, com); + + this.commisionService.save(com); + this.assignmentService.save(a); + + return "Success"; + } + +} From b4d1c874614fcc21bc52ba1285c635376d599c0e Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Tue, 29 Sep 2020 19:06:29 +0200 Subject: [PATCH 3/6] Checkpoint getting authority works + added new user --- .../src/main/java/com/plannaplan/App.java | 26 +++++++++++++---- .../controllers/CommisionController.java | 28 +++++++++++++++---- .../security/AuthenticationProvider.java | 2 +- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/restservice/src/main/java/com/plannaplan/App.java b/restservice/src/main/java/com/plannaplan/App.java index 7f8c652..17f8659 100755 --- a/restservice/src/main/java/com/plannaplan/App.java +++ b/restservice/src/main/java/com/plannaplan/App.java @@ -28,11 +28,25 @@ public class App { @EventListener(ApplicationReadyEvent.class) public void importData() { - User testUser = new User(); - testUser.setEmail("filizy@st.amu.edu.pl"); - testUser.setName("Filip"); - testUser.setSurname("Izydorczyk"); - testUser.setRole(UserRoles.STUDENT); - this.userService.save(testUser); + User filip = new User(); + filip.setEmail("filizy@st.amu.edu.pl"); + filip.setName("Filip"); + filip.setSurname("Izydorczyk"); + filip.setRole(UserRoles.STUDENT); + this.userService.save(filip); + + User hub = new User(); + hub.setEmail("hubwrz1@st.amu.edu.pl"); + hub.setName("Hubert"); + hub.setSurname("Wrzesiński"); + hub.setRole(UserRoles.STUDENT); + this.userService.save(hub); + + User mac = new User(); + mac.setEmail("macglo2@st.amu.edu.pl"); + mac.setName("Maciej"); + mac.setSurname("Głowacki"); + mac.setRole(UserRoles.STUDENT); + this.userService.save(mac); } } diff --git a/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java index 344da41..58f9693 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java @@ -10,18 +10,26 @@ import com.plannaplan.Controller; import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Commision; import com.plannaplan.entities.Groups; +import com.plannaplan.entities.User; import com.plannaplan.models.ConfigData; 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.security.core.userdetails.UserDetails; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; +import org.springframework.security.authentication.AnonymousAuthenticationToken; +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @RestController @CrossOrigin @@ -37,22 +45,32 @@ public class CommisionController { @Autowired private AssignmentService assignmentService; + @Autowired + private UserService userService; + public CommisionController() { } @PostMapping("/add") - public String addCommision(@RequestParam("id") Long id) { + public ResponseEntity addCommision(@RequestParam("id") Long id) { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + User user; + + if (!(authentication instanceof AnonymousAuthenticationToken)) { + user = this.userService.getUserByEmail(authentication.getName()); + } else { + return new ResponseEntity<>("Succes", HttpStatus.UNAUTHORIZED); + } + Groups group = this.groupServcicxe.getGroupById(id).orElseThrow(() -> new NullPointerException()); - Commision com = new Commision(null); - // Object principal = - // SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + Commision com = new Commision(user); Assignment a = new Assignment(group, com); this.commisionService.save(com); this.assignmentService.save(a); - return "Success"; + return new ResponseEntity<>("Succes", HttpStatus.OK); } } diff --git a/restservice/src/main/java/com/plannaplan/security/AuthenticationProvider.java b/restservice/src/main/java/com/plannaplan/security/AuthenticationProvider.java index aa9ff3d..3836732 100755 --- a/restservice/src/main/java/com/plannaplan/security/AuthenticationProvider.java +++ b/restservice/src/main/java/com/plannaplan/security/AuthenticationProvider.java @@ -59,7 +59,7 @@ public class AuthenticationProvider extends AbstractUserDetailsAuthenticationPro @Override public String getUsername() { - return user.getName() + " " + user.getSurname(); + return user.getEmail(); } @Override From b503ebcbcc4b3a65881366f976162fc2234e753d Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Wed, 30 Sep 2020 17:46:04 +0200 Subject: [PATCH 4/6] Added commisions endponts --- .../com/plannaplan/entities/Commision.java | 3 ++ .../java/com/plannaplan/entities/User.java | 4 ++ .../repositories/CommisionRepository.java | 6 +++ .../plannaplan/services/CommisionService.java | 8 +++ .../src/main/java/com/plannaplan/App.java | 8 +++ .../controllers/CommisionController.java | 51 +++++++++++-------- 6 files changed, 58 insertions(+), 22 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java index 950798b..dd4b698 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java @@ -29,6 +29,9 @@ public class Commision { this.commisionOwner = user; } + public Commision() { + } + public Long getId() { return this.id; } diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/User.java b/buisnesslogic/src/main/java/com/plannaplan/entities/User.java index e675415..2b8da7f 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/User.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/User.java @@ -68,4 +68,8 @@ public class User { public void setName(String name) { this.name = name; } + + public Long getId() { + return this.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 f5e7908..650569f 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/CommisionRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/CommisionRepository.java @@ -1,11 +1,17 @@ package com.plannaplan.repositories; +import java.util.List; + import com.plannaplan.entities.Commision; 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 CommisionRepository extends JpaRepository { + @Query("FROM Commision WHERE owner_id = ?1") + List getUsers(@Param("owner_id") Long id); } diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java b/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java index 0d1909b..8bef5bc 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java @@ -1,6 +1,9 @@ package com.plannaplan.services; +import java.util.List; + import com.plannaplan.entities.Commision; +import com.plannaplan.entities.User; import com.plannaplan.repositories.CommisionRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -19,4 +22,9 @@ public class CommisionService { return commision; } + + public List getUsersCommisions(User user) { + Long id = user.getId(); + return this.repo.getUsers(id); + } } diff --git a/restservice/src/main/java/com/plannaplan/App.java b/restservice/src/main/java/com/plannaplan/App.java index 17f8659..ac38c67 100755 --- a/restservice/src/main/java/com/plannaplan/App.java +++ b/restservice/src/main/java/com/plannaplan/App.java @@ -48,5 +48,13 @@ public class App { mac.setSurname("Głowacki"); mac.setRole(UserRoles.STUDENT); this.userService.save(mac); + + User mar = new User(); + mar.setEmail("marwoz16@st.amu.edu.pl"); + mar.setName("Marcin"); + mar.setSurname("Woźniak"); + mar.setRole(UserRoles.STUDENT); + this.userService.save(mar); + } } diff --git a/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java index 58f9693..f288585 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java @@ -1,17 +1,17 @@ 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.io.IOException; +import java.util.List; +import java.util.Optional; import com.plannaplan.App; -import com.plannaplan.Controller; import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Commision; import com.plannaplan.entities.Groups; import com.plannaplan.entities.User; -import com.plannaplan.models.ConfigData; import com.plannaplan.services.AssignmentService; import com.plannaplan.services.CommisionService; import com.plannaplan.services.GroupService; @@ -22,14 +22,10 @@ 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.security.core.userdetails.UserDetails; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.multipart.MultipartFile; import org.springframework.security.authentication.AnonymousAuthenticationToken; -import org.springframework.security.authentication.AuthenticationProvider; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @RestController @CrossOrigin @@ -52,25 +48,36 @@ public class CommisionController { } @PostMapping("/add") - public ResponseEntity addCommision(@RequestParam("id") Long id) { - Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - User user; + public ResponseEntity addCommision(@RequestBody List groups) { - if (!(authentication instanceof AnonymousAuthenticationToken)) { - user = this.userService.getUserByEmail(authentication.getName()); - } else { - return new ResponseEntity<>("Succes", HttpStatus.UNAUTHORIZED); - } - - Groups group = this.groupServcicxe.getGroupById(id).orElseThrow(() -> new NullPointerException()); + User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException()); Commision com = new Commision(user); - - Assignment a = new Assignment(group, com); - this.commisionService.save(com); - this.assignmentService.save(a); + + groups.stream().forEach((groupId) -> { + Groups group = this.groupServcicxe.getGroupById(groupId).orElseThrow(() -> new NullPointerException()); + Assignment a = new Assignment(group, com); + this.assignmentService.save(a); + }); return new ResponseEntity<>("Succes", HttpStatus.OK); } + @GetMapping("/getAllCommisions") + public ResponseEntity> getAlCommisions() { + User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException()); + List result = this.commisionService.getUsersCommisions(user); + 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(); + } + } + } From fa749655040725629ae14a65eb9ea66ef0077991 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Wed, 30 Sep 2020 19:15:32 +0200 Subject: [PATCH 5/6] 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 From 51ea557a066c68aad3d8238157ec857abbbe5191 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Wed, 30 Sep 2020 19:16:15 +0200 Subject: [PATCH 6/6] Small fix --- .../java/com/plannaplan/controllers/AssignmentsController.java | 1 - 1 file changed, 1 deletion(-) diff --git a/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java b/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java index 5265287..63646bf 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/AssignmentsController.java @@ -48,7 +48,6 @@ public class AssignmentsController extends TokenBasedController { 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());