2020-09-29 17:21:34 +02:00
|
|
|
package com.plannaplan.controllers;
|
|
|
|
|
|
|
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
2020-09-30 17:46:04 +02:00
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
2020-10-29 16:25:55 +01:00
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
2020-09-29 17:21:34 +02:00
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
2020-09-30 17:46:04 +02:00
|
|
|
import java.util.List;
|
2020-09-29 17:21:34 +02:00
|
|
|
|
|
|
|
import com.plannaplan.App;
|
|
|
|
import com.plannaplan.entities.Assignment;
|
|
|
|
import com.plannaplan.entities.Commision;
|
|
|
|
import com.plannaplan.entities.Groups;
|
2020-09-29 19:06:29 +02:00
|
|
|
import com.plannaplan.entities.User;
|
2020-10-01 16:46:45 +02:00
|
|
|
import com.plannaplan.exceptions.UserNotFoundException;
|
2020-10-15 17:29:40 +02:00
|
|
|
import com.plannaplan.responses.mappers.CommisionResponseMappers;
|
|
|
|
import com.plannaplan.responses.models.CommisionResponse;
|
2020-09-29 17:21:34 +02:00
|
|
|
import com.plannaplan.services.AssignmentService;
|
|
|
|
import com.plannaplan.services.CommisionService;
|
|
|
|
import com.plannaplan.services.GroupService;
|
2020-10-29 16:25:55 +01:00
|
|
|
import com.plannaplan.types.UserRoles;
|
2020-09-29 17:21:34 +02:00
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
2020-09-30 17:46:04 +02:00
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
2020-09-29 17:21:34 +02:00
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
@CrossOrigin
|
|
|
|
@RequestMapping("/api/" + App.API_VERSION + "/commisions")
|
2020-09-30 19:15:32 +02:00
|
|
|
public class CommisionController extends TokenBasedController {
|
2020-09-29 17:21:34 +02:00
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private CommisionService commisionService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private GroupService groupServcicxe;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private AssignmentService assignmentService;
|
|
|
|
|
|
|
|
public CommisionController() {
|
|
|
|
}
|
|
|
|
|
2020-10-29 16:25:55 +01:00
|
|
|
@PostMapping(value = { "/add", "/add/{id}" })
|
|
|
|
public ResponseEntity<String> addCommision(@RequestBody List<Long> groups,
|
|
|
|
@PathVariable(name = "id", required = false) Long userId) throws UserNotFoundException {
|
2020-09-29 19:06:29 +02:00
|
|
|
|
2020-09-30 17:46:04 +02:00
|
|
|
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
|
2020-10-29 16:25:55 +01:00
|
|
|
|
|
|
|
if (userId != null && !((user.getId() == userId) || (user.getRole() == UserRoles.DEANERY))) {
|
|
|
|
return new ResponseEntity<>("Forbidden access atempt", HttpStatus.FORBIDDEN);
|
|
|
|
} else if (userId != null) {
|
|
|
|
user = userService.getById(userId).orElseThrow(() -> new UserNotFoundException("Given user id not exist"));
|
|
|
|
}
|
|
|
|
|
|
|
|
final Long notExistingGroup = this.groupServcicxe.findNotExistingGroup(groups);
|
|
|
|
if (notExistingGroup != Long.MIN_VALUE) {
|
|
|
|
return new ResponseEntity<>("Group " + notExistingGroup.toString() + " doesn't exist",
|
|
|
|
HttpStatus.NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
final Commision com = new Commision(user);
|
2020-09-29 17:21:34 +02:00
|
|
|
this.commisionService.save(com);
|
2020-09-30 17:46:04 +02:00
|
|
|
|
|
|
|
groups.stream().forEach((groupId) -> {
|
|
|
|
Groups group = this.groupServcicxe.getGroupById(groupId).orElseThrow(() -> new NullPointerException());
|
|
|
|
Assignment a = new Assignment(group, com);
|
|
|
|
this.assignmentService.save(a);
|
|
|
|
});
|
2020-09-29 17:21:34 +02:00
|
|
|
|
2020-09-29 19:06:29 +02:00
|
|
|
return new ResponseEntity<>("Succes", HttpStatus.OK);
|
2020-09-29 17:21:34 +02:00
|
|
|
}
|
|
|
|
|
2020-09-30 17:46:04 +02:00
|
|
|
@GetMapping("/getAllCommisions")
|
2020-10-15 17:29:40 +02:00
|
|
|
public ResponseEntity<List<CommisionResponse>> getAlCommisions() throws UserNotFoundException {
|
2020-09-30 17:46:04 +02:00
|
|
|
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
|
2020-10-15 17:29:40 +02:00
|
|
|
List<CommisionResponse> result = CommisionResponseMappers
|
|
|
|
.mapToResponse(this.commisionService.getUsersCommisions(user));
|
2020-09-30 17:46:04 +02:00
|
|
|
return new ResponseEntity<>(result, HttpStatus.OK);
|
|
|
|
}
|
|
|
|
|
2020-09-29 17:21:34 +02:00
|
|
|
}
|