Works
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
@ -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<User> getCurrentUser() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
|
||||
if (!(authentication instanceof AnonymousAuthenticationToken)) {
|
||||
return Optional.of(this.userService.getUserByEmail(authentication.getName()));
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user