backend/restservice/src/main/java/com/plannaplan/controllers/CommisionController.java

118 lines
5.9 KiB
Java
Raw Normal View History

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;
import org.springframework.web.bind.annotation.RestController;
2020-11-05 15:27:51 +01:00
import org.springframework.web.bind.annotation.PathVariable;
2020-11-04 16:40:02 +01:00
import io.swagger.annotations.Api;
2020-11-04 16:58:26 +01:00
import io.swagger.annotations.ApiOperation;
2020-11-04 17:23:29 +01:00
import io.swagger.annotations.ApiParam;
2020-11-04 16:40:02 +01:00
2020-09-30 17:46:04 +02:00
import java.util.List;
2020-10-29 17:38:05 +01:00
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;
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;
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;
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-11-05 15:27:51 +01:00
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
@CrossOrigin
@RequestMapping("/api/" + App.API_VERSION + "/commisions")
2020-11-05 15:27:51 +01:00
@Api(tags = { "Commisions" }, value = "Commisions", description = "Commision is representation of student selected assignments at time. All assignments are attached to some commision so we can see current assignments and also browse histroy of changes for given user")
2020-09-30 19:15:32 +02:00
public class CommisionController extends TokenBasedController {
@Autowired
private CommisionService commisionService;
@Autowired
private GroupService groupServcicxe;
@Autowired
private AssignmentService assignmentService;
public CommisionController() {
}
2020-11-05 15:27:51 +01:00
@PostMapping(value = { "/user", "/user/{id}" })
@ApiOperation(value = "Create commision with assignents to given groups. If group doesn't exist error will be thrown")
public ResponseEntity<String> addCommision(
@RequestBody @ApiParam(value = "List of groups ids user want to assign to. If group doesnt exisit error will be thrown") List<Long> groups,
2020-10-30 14:27:13 +01:00
@PathVariable(name = "id", required = false) Long userId) {
2020-10-30 14:27:13 +01:00
try {
2020-09-30 17:46:04 +02:00
2020-10-30 14:27:13 +01:00
final User asker = this.getCurrentUser()
.orElseThrow(() -> new UserNotFoundException("Invalid token"));
2020-10-30 14:27:13 +01:00
final User user = userId != null
? userService.getById(userId).orElseThrow(
() -> new UserNotFoundException("Given user id not exist"))
: asker;
Assert.isTrue((asker.getRole() == UserRoles.DEANERY && user.getRole() == UserRoles.STUDENT
2020-11-05 15:27:51 +01:00
|| (asker.getId().equals(user.getId()) && user.getRole() == UserRoles.STUDENT)),
2020-10-30 14:27:13 +01:00
"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);
}
}
2020-11-05 15:27:51 +01:00
@GetMapping("/user")
@ApiOperation("Return list of user all commisions (history of schedules)")
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);
}
@PreAuthorize("hasRole('ROLE_DEANERY')")
@GetMapping("/user/{id}")
2020-11-05 15:27:51 +01:00
@ApiOperation("Return list of commisions for given user. To be able to access this data u need to provide DEANERY token")
public ResponseEntity<List<CommisionResponse>> getCommision(@PathVariable(name = "id") Long userId)
throws UserNotFoundException {
User user = this.userService.getById(userId).orElseThrow(() -> new NullPointerException());
List<CommisionResponse> result = CommisionResponseMappers
.mapToResponse(this.commisionService.getUsersCommisions(user));
return new ResponseEntity<>(result, HttpStatus.OK);
}
2020-09-30 17:46:04 +02:00
}