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

67 lines
2.5 KiB
Java
Raw Normal View History

2020-09-30 19:15:32 +02:00
package com.plannaplan.controllers;
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.services.AssignmentService;
import com.plannaplan.services.CommisionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
@CrossOrigin
@RequestMapping("/api/" + App.API_VERSION + "/assignments")
public class AssignmentsController extends TokenBasedController {
@Autowired
private CommisionService commisionService;
@Autowired
private AssignmentService assignmentService;
@GetMapping("/getCurrentAssignments")
2020-10-01 16:46:45 +02:00
public ResponseEntity<List<Object>> getCurrentAssignments() throws Exception {
2020-09-30 19:15:32 +02:00
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
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<>(null, HttpStatus.OK);
}
}