Made ASsignm,ents Conmtroller more readable
This commit is contained in:
@ -4,17 +4,15 @@ import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Dictionary;
|
||||
import java.util.Hashtable;
|
||||
import java.util.List;
|
||||
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;
|
||||
import com.plannaplan.responses.mappers.AssignmentResponseMappers;
|
||||
import com.plannaplan.responses.models.AssignmentResponse;
|
||||
import com.plannaplan.services.AssignmentService;
|
||||
import com.plannaplan.services.CommisionService;
|
||||
|
||||
@ -35,30 +33,14 @@ public class AssignmentsController extends TokenBasedController {
|
||||
private AssignmentService assignmentService;
|
||||
|
||||
@GetMapping("/getCurrentAssignments")
|
||||
public ResponseEntity<List<Object>> getCurrentAssignments() throws Exception {
|
||||
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
|
||||
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());
|
||||
|
||||
List<Object> finalResponse = new ArrayList<>();
|
||||
for (Assignment a : respone) {
|
||||
Dictionary<String, Object> elem = new Hashtable<>();
|
||||
Dictionary<String, Object> groupInfo = new Hashtable<>();
|
||||
elem.put("id", a.getId());
|
||||
Groups g = a.getGroup();
|
||||
groupInfo.put("id", g.getId());
|
||||
groupInfo.put("day", g.getDay().label);
|
||||
groupInfo.put("time", g.getTimeString());
|
||||
groupInfo.put("lecturer", g.getLecturer().toString());
|
||||
groupInfo.put("room", g.getRoom());
|
||||
groupInfo.put("capacity", g.getCapacity());
|
||||
groupInfo.put("type", g.getType());
|
||||
elem.put("group", groupInfo);
|
||||
finalResponse.add(elem);
|
||||
}
|
||||
|
||||
return new ResponseEntity<>(finalResponse, HttpStatus.OK);
|
||||
return new ResponseEntity<>(AssignmentResponseMappers.mapAssignmentsListToAssignmentResponseList(respone),
|
||||
HttpStatus.OK);
|
||||
}
|
||||
|
||||
return new ResponseEntity<>(null, HttpStatus.OK);
|
||||
|
@ -0,0 +1,20 @@
|
||||
package com.plannaplan.responses.mappers;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.plannaplan.entities.Assignment;
|
||||
import com.plannaplan.entities.Groups;
|
||||
import com.plannaplan.responses.models.AssignmentResponse;
|
||||
|
||||
public class AssignmentResponseMappers {
|
||||
public static final List<AssignmentResponse> mapGroupsListToAssignmentResponseList(List<Groups> groups) {
|
||||
return groups.stream().filter(Objects::nonNull).map(AssignmentResponse::new).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static final List<AssignmentResponse> mapAssignmentsListToAssignmentResponseList(
|
||||
List<Assignment> assignments) {
|
||||
return assignments.stream().filter(Objects::nonNull).map(AssignmentResponse::new).collect(Collectors.toList());
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package com.plannaplan.responses.models;
|
||||
|
||||
import com.plannaplan.entities.Assignment;
|
||||
import com.plannaplan.entities.Groups;
|
||||
import com.plannaplan.types.GroupType;
|
||||
|
||||
@ -14,13 +15,17 @@ public class AssignmentResponse {
|
||||
private GroupType type;
|
||||
|
||||
public AssignmentResponse(Groups group) {
|
||||
this.id = group.getId();
|
||||
this.day = group.getDay().label;
|
||||
this.time = group.getTimeString();
|
||||
this.lecturer = group.getLecturer().toString();
|
||||
this.room = group.getRoom();
|
||||
this.id = group.getId() != null ? group.getId() : null;
|
||||
this.day = group.getDay() != null ? group.getDay().label : -1;
|
||||
this.time = group.getTimeString() != null ? group.getTimeString() : "";
|
||||
this.lecturer = group.getLecturer() != null ? group.getLecturer().toString() : "";
|
||||
this.room = group.getRoom() != null ? group.getRoom() : "";
|
||||
this.capacity = group.getCapacity();
|
||||
this.type = group.getType();
|
||||
this.type = group.getType() != null ? group.getType() : null;
|
||||
}
|
||||
|
||||
public AssignmentResponse(Assignment assignment) {
|
||||
this(assignment.getGroup());
|
||||
}
|
||||
|
||||
public GroupType getType() {
|
||||
|
Reference in New Issue
Block a user