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

93 lines
4.2 KiB
Java
Raw Normal View History

2020-08-08 14:14:42 +02:00
package com.plannaplan.controllers;
import java.util.HashMap;
2020-08-08 14:14:42 +02:00
import java.util.List;
2020-12-18 17:23:41 +01:00
import java.util.Optional;
2020-08-08 14:14:42 +02:00
2020-09-15 11:31:30 +02:00
import com.plannaplan.App;
2020-08-08 14:14:42 +02:00
import com.plannaplan.entities.Groups;
import com.plannaplan.responses.mappers.GroupsMappers;
import com.plannaplan.responses.models.GroupDefaultResponse;
import com.plannaplan.responses.models.CourseWithGroupsResponse;
2020-08-08 14:14:42 +02:00
import com.plannaplan.services.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
2020-12-18 15:49:46 +01:00
import org.springframework.security.access.prepost.PreAuthorize;
2020-08-08 14:14:42 +02:00
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
2020-12-18 15:49:46 +01:00
import org.springframework.web.bind.annotation.PutMapping;
2020-09-15 11:31:30 +02:00
import org.springframework.web.bind.annotation.RequestMapping;
2020-08-08 14:14:42 +02:00
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
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
2021-01-16 13:57:58 +01:00
/**
* Rest controller to Groups related endpoints. More detailed api docs is
* available via swagger
*/
2020-08-08 14:14:42 +02:00
@RestController
@CrossOrigin
2020-09-15 11:31:30 +02:00
@RequestMapping("/api/" + App.API_VERSION + "/groups")
2020-11-04 16:40:02 +01:00
@Api(tags = {
"Group" }, value = "Group", description = "Enpoints to deal with gorups. Group is related directly to course and can be either class and lecture")
2020-08-08 14:14:42 +02:00
public class GroupController {
@Autowired
private GroupService groupService;
2021-01-16 13:57:58 +01:00
/**
* @param id course to display with groups
* @param capacity should include capaticty in response
* @param takenPlaces should include takenPlaces in response
* @return CourseWithGroupsResponse
*/
@GetMapping("/course/{id}")
2020-11-04 16:58:26 +01:00
@ApiOperation(value = "Return list of lectures and classes (if present) given course")
public ResponseEntity<CourseWithGroupsResponse<? extends GroupDefaultResponse>> getCourses(
2020-11-05 14:32:42 +01:00
@PathVariable(name = "id") Long id,
@RequestParam(name = "capacity", defaultValue = "true") @ApiParam(value = "Boolean if we want to have capacity field in response") Boolean capacity,
@RequestParam(name = "takenPlaces", defaultValue = "false") @ApiParam(value = "Boolean if we want to have respoonse with information about taken places by other students") Boolean takenPlaces) {
2020-08-11 17:36:58 +02:00
List<Groups> groups = this.groupService.getGroupsByCourse(id);
if (takenPlaces) {
HashMap<Long, Integer> ammounts = this.groupService.getTakenPlaces(groups);
if (capacity) {
return new ResponseEntity<>(GroupsMappers.mapToGetCourseGroupsWithCapacityResponse(groups, ammounts),
HttpStatus.OK);
}
return new ResponseEntity<>(GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups, ammounts),
HttpStatus.OK);
}
if (capacity) {
return new ResponseEntity<>(GroupsMappers.mapToGetCourseGroupsWithCapacityResponse(groups), HttpStatus.OK);
2020-08-11 17:36:58 +02:00
}
return new ResponseEntity<>(GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups), HttpStatus.OK);
2020-08-08 14:14:42 +02:00
}
2020-12-18 15:49:46 +01:00
2021-01-16 13:57:58 +01:00
/**
* @param id group id to change capacity
* @param newcapacity new capacity to insert
* @return ResponseEntity was action success
*/
2020-12-18 15:49:46 +01:00
@PutMapping("/{id}/capacity")
2020-12-18 17:23:41 +01:00
@PreAuthorize("hasRole('ROLE_DEANERY')")
2020-12-18 15:52:05 +01:00
@ApiOperation(value = "Change capacity of given group. You need to provide DEANERY token to be ale to change capacity")
public ResponseEntity<String> updateCapacity(
2020-12-18 16:33:58 +01:00
@PathVariable(name = "id", required = true) @ApiParam(value = "id of group to change capacity") Long id,
@RequestParam(name = "newcapacity", required = true) Integer newcapacity) {
2020-12-18 17:23:41 +01:00
final Optional<Groups> group = this.groupService.getGroupById(id);
if (group.isEmpty()) {
2020-12-18 15:49:46 +01:00
return new ResponseEntity<>("Given group doens't exist", HttpStatus.NOT_FOUND);
}
2020-12-18 17:23:41 +01:00
final Groups gr = group.get();
gr.setCapacity(newcapacity);
this.groupService.save(gr);
2020-12-18 15:49:46 +01:00
return new ResponseEntity<>("Success", HttpStatus.OK);
}
2020-08-08 14:14:42 +02:00
}