Merged with master

This commit is contained in:
Filip Izydorczyk
2020-12-12 15:38:47 +01:00
10 changed files with 278 additions and 16 deletions

View File

@ -29,7 +29,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
@CrossOrigin
@RequestMapping("/api/" + App.API_VERSION + "/assignments")
@Api(tags = {
"Assignments" }, value = "Assignments", description = "Assignment is representation of student willing to join given group (lecture or calss)")
"Assignments" }, value = "Assignments", description = "Assignment is representation of student willing to join given group (lecture or calss). This comtroller is depreaceted. Use commission instead")
@Deprecated
public class AssignmentsController extends TokenBasedController {
@Autowired
@ -39,7 +40,8 @@ public class AssignmentsController extends TokenBasedController {
private AssignmentService assignmentService;
@GetMapping("/user")
@ApiOperation(value = "Return user current assignemts (from newest commision). STUDENT Token needs to be provided.")
@ApiOperation(value = "Return user current assignemts (from newest commision). STUDENT Token needs to be provided. This method is depreaceted. Use `/api/v1/commisions/user/schedule` instead.")
@Deprecated
public ResponseEntity<List<AssignmentResponse>> getCurrentAssignments() throws Exception {
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException("User not found"));
Optional<Commision> com = this.commisionService.getNewestCommision(user);

View File

@ -33,6 +33,12 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.ArrayList;
import com.plannaplan.responses.mappers.AssignmentResponseMappers;
import com.plannaplan.responses.models.AssignmentResponse;
@RestController
@CrossOrigin
@ -96,22 +102,75 @@ public class CommisionController extends TokenBasedController {
@GetMapping("/user")
@ApiOperation("Return list of user all commisions (history of schedules)")
public ResponseEntity<List<CommisionResponse>> getAlCommisions() throws UserNotFoundException {
public ResponseEntity<List<? extends CommisionResponse>> getAlCommisions(
@RequestParam(name = "groups", defaultValue = "false") @ApiParam(value = "Boolean if we want to display wiht commision's group ids") Boolean groups)
throws UserNotFoundException {
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
List<CommisionResponse> result = CommisionResponseMappers
.mapToResponse(this.commisionService.getUsersCommisions(user));
List<? extends CommisionResponse> result;
if (!groups) {
result = CommisionResponseMappers.mapToResponse(this.commisionService.getUsersCommisions(user));
} else {
result = CommisionResponseMappers
.mapToResponseWithGroups(this.commisionService.getUsersCommisions(user));
}
return new ResponseEntity<>(result, HttpStatus.OK);
}
@GetMapping("/user/schedule")
@ApiOperation(value = "Return user current assignemts (from newest commision). STUDENT Token needs to be provided.")
public ResponseEntity<List<AssignmentResponse>> getCurrentAssignments() throws Exception {
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException("User not found"));
Optional<Commision> com = this.commisionService.getNewestCommision(user);
if (com.isPresent()) {
List<Assignment> respone = this.assignmentService.getCommisionAssignments(com.get());
return new ResponseEntity<>(AssignmentResponseMappers.mapToResponse(respone), HttpStatus.OK);
}
return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK);
}
@PreAuthorize("hasRole('ROLE_DEANERY')")
@GetMapping("/user/{id}")
@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)
public ResponseEntity<List<? extends CommisionResponse>> getCommision(@PathVariable(name = "id") Long userId,
@RequestParam(name = "groups", defaultValue = "false") @ApiParam(value = "Boolean if we want to display wiht commision's group ids") Boolean groups)
throws UserNotFoundException {
User user = this.userService.getById(userId).orElseThrow(() -> new NullPointerException());
List<CommisionResponse> result = CommisionResponseMappers
.mapToResponse(this.commisionService.getUsersCommisions(user));
List<? extends CommisionResponse> result;
if (!groups) {
result = CommisionResponseMappers.mapToResponse(this.commisionService.getUsersCommisions(user));
} else {
result = CommisionResponseMappers
.mapToResponseWithGroups(this.commisionService.getUsersCommisions(user));
}
return new ResponseEntity<>(result, HttpStatus.OK);
}
@PreAuthorize("hasRole('ROLE_DEANERY')")
@GetMapping("/user/{id}/schedule")
@ApiOperation(value = "Return given user current assignemts (from newest commision). DEANERY Token needs to be provided.")
public ResponseEntity<List<AssignmentResponse>> getCurrentAssignmentsDeanery(
@PathVariable(name = "id") Long userId) throws Exception {
User user = this.userService.getById(userId).orElseThrow(() -> new NullPointerException());
if (user.getRole() == UserRoles.DEANERY) {
return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
}
Optional<Commision> com = this.commisionService.getNewestCommision(user);
if (com.isPresent()) {
List<Assignment> respone = this.assignmentService.getCommisionAssignments(com.get());
return new ResponseEntity<>(AssignmentResponseMappers.mapToResponse(respone), HttpStatus.OK);
}
return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK);
}
}

View File

@ -6,9 +6,15 @@ import java.util.stream.Collectors;
import com.plannaplan.entities.Commision;
import com.plannaplan.responses.models.CommisionResponse;
import com.plannaplan.responses.models.CommisionWithGroupsResponse;
public class CommisionResponseMappers {
public static final List<CommisionResponse> mapToResponse(List<Commision> commisions) {
return commisions.stream().filter(Objects::nonNull).map(CommisionResponse::new).collect(Collectors.toList());
}
public static final List<CommisionWithGroupsResponse> mapToResponseWithGroups(List<Commision> commisions) {
return commisions.stream().filter(Objects::nonNull).map(CommisionWithGroupsResponse::new)
.collect(Collectors.toList());
}
}

View File

@ -1,7 +1,5 @@
package com.plannaplan.responses.models;
import java.sql.Timestamp;
import com.plannaplan.entities.Commision;
import io.swagger.annotations.ApiModel;
@ -13,14 +11,14 @@ public class CommisionResponse {
private Long id;
@ApiModelProperty(value = "Timestamp where the user commit the commision")
private Timestamp commisionDate;
private String commisionDate;
public CommisionResponse(Commision commision) {
this.id = commision.getId();
this.commisionDate = commision.getCommisionDate();
this.commisionDate = commision.getCommisionDate().toString();
}
public Timestamp getCommisionDate() {
public String getCommisionDate() {
return commisionDate;
}

View File

@ -0,0 +1,36 @@
package com.plannaplan.responses.models;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(description = "Response shows information about commision and its groups.", value = "CommisionWithGroupsResponse")
public class CommisionWithGroupsResponse extends CommisionResponse {
@ApiModelProperty(value = "List of groups ids in databse that belongs to commision")
private List<Long> groups;
public CommisionWithGroupsResponse(Commision commision) {
super(commision);
this.groups = commision.getAssignments().stream().filter(Objects::nonNull)
.map(new Function<Assignment, Long>() {
@Override
public Long apply(Assignment arg0) {
return arg0.getGroup().getId();
}
}).collect(Collectors.toList());
}
public List<Long> getGroups() {
return groups;
}
}