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

70 lines
2.5 KiB
Java
Raw Normal View History

package com.plannaplan.controllers;
import org.springframework.web.bind.annotation.CrossOrigin;
2020-09-30 17:46:04 +02:00
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
2020-09-30 17:46:04 +02:00
import java.util.List;
import com.plannaplan.App;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User;
2020-10-01 16:46:45 +02:00
import com.plannaplan.exceptions.UserNotFoundException;
2020-10-15 17:29:40 +02:00
import com.plannaplan.responses.mappers.CommisionResponseMappers;
import com.plannaplan.responses.models.CommisionResponse;
import com.plannaplan.services.AssignmentService;
import com.plannaplan.services.CommisionService;
import com.plannaplan.services.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
2020-09-30 17:46:04 +02:00
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
@CrossOrigin
@RequestMapping("/api/" + App.API_VERSION + "/commisions")
2020-09-30 19:15:32 +02:00
public class CommisionController extends TokenBasedController {
@Autowired
private CommisionService commisionService;
@Autowired
private GroupService groupServcicxe;
@Autowired
private AssignmentService assignmentService;
public CommisionController() {
}
@PostMapping("/user")
2020-10-01 16:46:45 +02:00
public ResponseEntity<String> addCommision(@RequestBody List<Long> groups) throws UserNotFoundException {
2020-09-30 17:46:04 +02:00
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
Commision com = new Commision(user);
this.commisionService.save(com);
2020-09-30 17:46:04 +02:00
groups.stream().forEach((groupId) -> {
Groups group = this.groupServcicxe.getGroupById(groupId).orElseThrow(() -> new NullPointerException());
Assignment a = new Assignment(group, com);
this.assignmentService.save(a);
});
return new ResponseEntity<>("Succes", HttpStatus.OK);
}
@GetMapping("/user")
2020-10-15 17:29:40 +02:00
public ResponseEntity<List<CommisionResponse>> getAlCommisions() throws UserNotFoundException {
2020-09-30 17:46:04 +02:00
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
2020-10-15 17:29:40 +02:00
List<CommisionResponse> result = CommisionResponseMappers
.mapToResponse(this.commisionService.getUsersCommisions(user));
2020-09-30 17:46:04 +02:00
return new ResponseEntity<>(result, HttpStatus.OK);
}
}