This commit is contained in:
Filip Izydorczyk 2020-09-30 19:15:32 +02:00
parent b503ebcbcc
commit fa74965504
8 changed files with 131 additions and 21 deletions

View File

@ -23,4 +23,15 @@ public class Assignment {
this.commision = commision; this.commision = commision;
this.group = group; this.group = group;
} }
public Assignment() {
}
public Long getId() {
return this.id;
}
public Groups getGroup() {
return this.group;
}
} }

View File

@ -1,11 +1,18 @@
package com.plannaplan.repositories; package com.plannaplan.repositories;
import java.util.List;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import org.springframework.data.jpa.repository.JpaRepository; 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; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface AssignmentRepository extends JpaRepository<Assignment, Long> { public interface AssignmentRepository extends JpaRepository<Assignment, Long> {
@Query("FROM Assignment WHERE commision_id = ?1")
List<Assignment> getByCommision(@Param("commision_id") Long id);
} }

View File

@ -14,4 +14,7 @@ public interface CommisionRepository extends JpaRepository<Commision, Long> {
@Query("FROM Commision WHERE owner_id = ?1") @Query("FROM Commision WHERE owner_id = ?1")
List<Commision> getUsers(@Param("owner_id") Long id); List<Commision> getUsers(@Param("owner_id") Long id);
@Query("FROM Commision WHERE owner_id = ?1 order by commisionDate desc")
List<Commision> getNewestCommision(@Param("owner_id") Long id);
} }

View File

@ -1,14 +1,16 @@
package com.plannaplan.services; package com.plannaplan.services;
import com.plannaplan.abstracts.EventWatcher; import java.util.List;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision;
import com.plannaplan.repositories.AssignmentRepository; import com.plannaplan.repositories.AssignmentRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public class AssignmentService extends EventWatcher { public class AssignmentService {
@Autowired @Autowired
private AssignmentRepository repo; private AssignmentRepository repo;
@ -19,4 +21,8 @@ public class AssignmentService extends EventWatcher {
public void save(Assignment assignment) { public void save(Assignment assignment) {
this.repo.save(assignment); this.repo.save(assignment);
} }
public List<Assignment> getCommisionAssignments(Commision com) {
return this.repo.getByCommision(com.getId());
}
} }

View File

@ -1,6 +1,7 @@
package com.plannaplan.services; package com.plannaplan.services;
import java.util.List; import java.util.List;
import java.util.Optional;
import com.plannaplan.entities.Commision; import com.plannaplan.entities.Commision;
import com.plannaplan.entities.User; import com.plannaplan.entities.User;
@ -27,4 +28,8 @@ public class CommisionService {
Long id = user.getId(); Long id = user.getId();
return this.repo.getUsers(id); return this.repo.getUsers(id);
} }
public Optional<Commision> getNewestCommision(User user) {
return Optional.of(this.repo.getNewestCommision(user.getId()).get(0));
}
} }

View File

@ -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<List<Object>> getCurrentAssignments() {
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
Optional<Commision> com = this.commisionService.getNewestCommision(user);
if (com.isPresent()) {
List<Assignment> respone = this.assignmentService.getCommisionAssignments(com.get());
List<Object> finalResponse = new ArrayList<>();
for (Assignment a : respone) {
Dictionary<String, Object> elem = new Hashtable<>();
Dictionary<String, Object> 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);
}
}

View File

@ -5,7 +5,6 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
import java.util.Optional;
import com.plannaplan.App; import com.plannaplan.App;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
@ -15,22 +14,18 @@ import com.plannaplan.entities.User;
import com.plannaplan.services.AssignmentService; import com.plannaplan.services.AssignmentService;
import com.plannaplan.services.CommisionService; import com.plannaplan.services.CommisionService;
import com.plannaplan.services.GroupService; import com.plannaplan.services.GroupService;
import com.plannaplan.services.UserService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; 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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
@RestController @RestController
@CrossOrigin @CrossOrigin
@RequestMapping("/api/" + App.API_VERSION + "/commisions") @RequestMapping("/api/" + App.API_VERSION + "/commisions")
public class CommisionController { public class CommisionController extends TokenBasedController {
@Autowired @Autowired
private CommisionService commisionService; private CommisionService commisionService;
@ -41,9 +36,6 @@ public class CommisionController {
@Autowired @Autowired
private AssignmentService assignmentService; private AssignmentService assignmentService;
@Autowired
private UserService userService;
public CommisionController() { public CommisionController() {
} }
@ -70,14 +62,4 @@ public class CommisionController {
return new ResponseEntity<>(result, HttpStatus.OK); return new ResponseEntity<>(result, HttpStatus.OK);
} }
private Optional<User> getCurrentUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof AnonymousAuthenticationToken)) {
return Optional.of(this.userService.getUserByEmail(authentication.getName()));
} else {
return Optional.empty();
}
}
} }

View File

@ -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<User> getCurrentUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof AnonymousAuthenticationToken)) {
return Optional.of(this.userService.getUserByEmail(authentication.getName()));
} else {
return Optional.empty();
}
}
}