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;
|
|
|
|
import java.util.Optional;
|
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-09-29 17:21:34 +02:00
|
|
|
import com.plannaplan.services.AssignmentService;
|
|
|
|
import com.plannaplan.services.CommisionService;
|
|
|
|
import com.plannaplan.services.GroupService;
|
2020-09-29 19:06:29 +02:00
|
|
|
import com.plannaplan.services.UserService;
|
2020-09-29 17:21:34 +02:00
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
2020-09-29 19:06:29 +02:00
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
2020-09-29 17:21:34 +02:00
|
|
|
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;
|
2020-09-29 19:06:29 +02:00
|
|
|
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
2020-09-29 17:21:34 +02:00
|
|
|
|
|
|
|
@RestController
|
|
|
|
@CrossOrigin
|
|
|
|
@RequestMapping("/api/" + App.API_VERSION + "/commisions")
|
|
|
|
public class CommisionController {
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private CommisionService commisionService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private GroupService groupServcicxe;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private AssignmentService assignmentService;
|
|
|
|
|
2020-09-29 19:06:29 +02:00
|
|
|
@Autowired
|
|
|
|
private UserService userService;
|
|
|
|
|
2020-09-29 17:21:34 +02:00
|
|
|
public CommisionController() {
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("/add")
|
2020-09-30 17:46:04 +02:00
|
|
|
public ResponseEntity<String> addCommision(@RequestBody List<Long> groups) {
|
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-09-30 17:46:04 +02:00
|
|
|
@GetMapping("/getAllCommisions")
|
|
|
|
public ResponseEntity<List<Commision>> getAlCommisions() {
|
|
|
|
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
|
|
|
|
List<Commision> result = this.commisionService.getUsersCommisions(user);
|
|
|
|
return new ResponseEntity<>(result, HttpStatus.OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Optional<User> getCurrentUser() {
|
|
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
|
|
|
|
|
if (!(authentication instanceof AnonymousAuthenticationToken)) {
|
|
|
|
return Optional.of(this.userService.getUserByEmail(authentication.getName()));
|
|
|
|
} else {
|
|
|
|
return Optional.empty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 17:21:34 +02:00
|
|
|
}
|