new endpoint

This commit is contained in:
Filip Izydorczyk 2021-01-17 20:36:46 +01:00
parent c4ec88e691
commit 11ec43911e
3 changed files with 108 additions and 0 deletions

View File

@ -42,6 +42,7 @@ import java.util.HashMap;
import java.util.Iterator;
import com.plannaplan.responses.mappers.AssignmentResponseMappers;
import com.plannaplan.responses.models.AssignmentDetailedResponse;
import com.plannaplan.responses.models.AssignmentResponse;
/**
@ -161,6 +162,28 @@ public class CommisionController extends TokenBasedController {
}
return new ResponseEntity<>(result, HttpStatus.OK);
}
/**
* @return list of user latests assignmets
* @throws UserNotFoundException if user was not found bny token
*/
@GetMapping("/user/assignments")
@ApiOperation("Return list of latest user commision assignments. User is recognized via token")
public ResponseEntity<List<AssignmentDetailedResponse>> getAllAssignmets() throws UserNotFoundException {
final User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
final Optional<Commision> latestCommision = this.commisionService.getNewestCommision(user);
if (latestCommision.isEmpty()) {
return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK);
}
final List<AssignmentDetailedResponse> response = AssignmentResponseMappers
.mapAssignmetnToDetialedResponse(latestCommision.get().getAssignments());
return new ResponseEntity<>(response, HttpStatus.OK);
}
/**

View File

@ -4,10 +4,13 @@ import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Course;
import com.plannaplan.entities.Groups;
import com.plannaplan.responses.models.AssignmentDetailedResponse;
import com.plannaplan.responses.models.AssignmentResponse;
import com.plannaplan.types.GroupType;
@ -69,4 +72,15 @@ public class AssignmentResponseMappers {
}
return response;
}
/**
* map assignmetn to detailed response
*
* @param assignments list of assignments to map
* @return list of responses
*/
public static final List<AssignmentDetailedResponse> mapAssignmetnToDetialedResponse(List<Assignment> assignments) {
return assignments.stream().filter(Objects::nonNull).map(AssignmentDetailedResponse::new)
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,71 @@
package com.plannaplan.responses.models;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Groups;
/**
* Assignment detailed response for api
*/
public class AssignmentDetailedResponse {
public Long id;
public String name;
private int day;
private String time;
private String endTime;
private String lecturer;
/**
* @param assignment Assignment instance to map
*/
public AssignmentDetailedResponse(Assignment assignment) {
final Groups group = assignment.getGroup();
this.id = assignment.getId();
this.name = group.getCourseId().getName();
this.day = group.getDay().label;
this.time = group.getTimeString();
this.endTime = group.getEndTimeString();
this.lecturer = group.getLecturer().toString();
}
/**
* @return day as a value from 0-6
*/
public int getDay() {
return day;
}
/**
* @return lecturer string fromated
*/
public String getLecturer() {
return lecturer;
}
/**
* @return time formated string
*/
public String getEndTime() {
return endTime;
}
/**
* @return time formated string
*/
public String getTime() {
return time;
}
/**
* @return name of course that group belongs to
*/
public String getName() {
return this.name;
}
/**
* @return db id of assignment
*/
public Long getId() {
return this.id;
}
}