2020-09-29 17:21:34 +02:00
|
|
|
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;
|
2020-09-29 17:21:34 +02:00
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
2020-09-30 17:46:04 +02:00
|
|
|
import java.util.List;
|
2020-09-29 17:21:34 +02:00
|
|
|
|
|
|
|
import com.plannaplan.App;
|
|
|
|
import com.plannaplan.entities.Assignment;
|
|
|
|
import com.plannaplan.entities.Commision;
|
|
|
|
import com.plannaplan.entities.Groups;
|
2020-09-29 19:06:29 +02:00
|
|
|
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;
|
2020-09-29 17:21:34 +02:00
|
|
|
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;
|
2020-09-29 17:21:34 +02:00
|
|
|
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 {
|
2020-09-29 17:21:34 +02:00
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private CommisionService commisionService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private GroupService groupServcicxe;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private AssignmentService assignmentService;
|
|
|
|
|
|
|
|
public CommisionController() {
|
|
|
|
}
|
|
|
|
|
2020-10-30 16:42:57 +01:00
|
|
|
@PostMapping("/user")
|
2020-10-01 16:46:45 +02:00
|
|
|
public ResponseEntity<String> addCommision(@RequestBody List<Long> groups) throws UserNotFoundException {
|
2020-09-29 19:06:29 +02:00
|
|
|
|
2020-09-30 17:46:04 +02:00
|
|
|
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
|
2020-09-29 19:06:29 +02:00
|
|
|
Commision com = new Commision(user);
|
2020-09-29 17:21:34 +02:00
|
|
|
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);
|
|
|
|
});
|
2020-09-29 17:21:34 +02:00
|
|
|
|
2020-09-29 19:06:29 +02:00
|
|
|
return new ResponseEntity<>("Succes", HttpStatus.OK);
|
2020-09-29 17:21:34 +02:00
|
|
|
}
|
|
|
|
|
2020-10-30 16:42:57 +01:00
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
2020-09-29 17:21:34 +02:00
|
|
|
}
|