Merge origin/deanery-plan-changing to master: With fixed restservice/.../App.java
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
@ -2,9 +2,11 @@ package com.plannaplan.controllers;
|
||||
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.plannaplan.App;
|
||||
import com.plannaplan.entities.Assignment;
|
||||
@ -17,10 +19,12 @@ import com.plannaplan.responses.models.CommisionResponse;
|
||||
import com.plannaplan.services.AssignmentService;
|
||||
import com.plannaplan.services.CommisionService;
|
||||
import com.plannaplan.services.GroupService;
|
||||
import com.plannaplan.types.UserRoles;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@ -30,40 +34,64 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@RequestMapping("/api/" + App.API_VERSION + "/commisions")
|
||||
public class CommisionController extends TokenBasedController {
|
||||
|
||||
@Autowired
|
||||
private CommisionService commisionService;
|
||||
@Autowired
|
||||
private CommisionService commisionService;
|
||||
|
||||
@Autowired
|
||||
private GroupService groupServcicxe;
|
||||
@Autowired
|
||||
private GroupService groupServcicxe;
|
||||
|
||||
@Autowired
|
||||
private AssignmentService assignmentService;
|
||||
@Autowired
|
||||
private AssignmentService assignmentService;
|
||||
|
||||
public CommisionController() {
|
||||
}
|
||||
public CommisionController() {
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public ResponseEntity<String> addCommision(@RequestBody List<Long> groups) throws UserNotFoundException {
|
||||
@PostMapping(value = { "/add", "/add/{id}" })
|
||||
public ResponseEntity<String> addCommision(@RequestBody List<Long> groups,
|
||||
@PathVariable(name = "id", required = false) Long userId) {
|
||||
|
||||
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
|
||||
Commision com = new Commision(user);
|
||||
this.commisionService.save(com);
|
||||
try {
|
||||
|
||||
groups.stream().forEach((groupId) -> {
|
||||
Groups group = this.groupServcicxe.getGroupById(groupId).orElseThrow(() -> new NullPointerException());
|
||||
Assignment a = new Assignment(group, com);
|
||||
this.assignmentService.save(a);
|
||||
});
|
||||
final User asker = this.getCurrentUser()
|
||||
.orElseThrow(() -> new UserNotFoundException("Invalid token"));
|
||||
|
||||
return new ResponseEntity<>("Succes", HttpStatus.OK);
|
||||
}
|
||||
final User user = userId != null
|
||||
? userService.getById(userId).orElseThrow(
|
||||
() -> new UserNotFoundException("Given user id not exist"))
|
||||
: asker;
|
||||
|
||||
@GetMapping("/getAllCommisions")
|
||||
public ResponseEntity<List<CommisionResponse>> getAlCommisions() throws UserNotFoundException {
|
||||
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
|
||||
List<CommisionResponse> result = CommisionResponseMappers
|
||||
.mapToResponse(this.commisionService.getUsersCommisions(user));
|
||||
return new ResponseEntity<>(result, HttpStatus.OK);
|
||||
}
|
||||
Assert.isTrue((asker.getRole() == UserRoles.DEANERY && user.getRole() == UserRoles.STUDENT
|
||||
|| (asker.getId() == user.getId() && user.getRole() == UserRoles.STUDENT)),
|
||||
"Incorrect attempt to change plan");
|
||||
|
||||
Optional<Long> notExistingGroup = this.groupServcicxe.findNotExistingGroup(groups);
|
||||
Assert.isTrue(!notExistingGroup.isPresent(), "Group "
|
||||
+ notExistingGroup.orElse(Long.MIN_VALUE).toString() + "doesn't exist");
|
||||
|
||||
final Commision com = new Commision(user);
|
||||
this.commisionService.save(com);
|
||||
|
||||
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);
|
||||
} catch (UserNotFoundException exception) {
|
||||
return new ResponseEntity<>(exception.getMessage(), HttpStatus.NOT_FOUND);
|
||||
} catch (IllegalArgumentException exception) {
|
||||
return new ResponseEntity<>(exception.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/getAllCommisions")
|
||||
public ResponseEntity<List<CommisionResponse>> getAlCommisions() throws UserNotFoundException {
|
||||
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
|
||||
List<CommisionResponse> result = CommisionResponseMappers
|
||||
.mapToResponse(this.commisionService.getUsersCommisions(user));
|
||||
return new ResponseEntity<>(result, HttpStatus.OK);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user