package com.plannaplan.controllers; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; import com.plannaplan.App; import com.plannaplan.Controller; import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Commision; import com.plannaplan.entities.Groups; import com.plannaplan.entities.User; import com.plannaplan.models.ConfigData; import com.plannaplan.services.AssignmentService; import com.plannaplan.services.CommisionService; import com.plannaplan.services.GroupService; import com.plannaplan.services.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.springframework.security.authentication.AnonymousAuthenticationToken; import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @RestController @CrossOrigin @RequestMapping("/api/" + App.API_VERSION + "/commisions") public class CommisionController { @Autowired private CommisionService commisionService; @Autowired private GroupService groupServcicxe; @Autowired private AssignmentService assignmentService; @Autowired private UserService userService; public CommisionController() { } @PostMapping("/add") public ResponseEntity addCommision(@RequestParam("id") Long id) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); User user; if (!(authentication instanceof AnonymousAuthenticationToken)) { user = this.userService.getUserByEmail(authentication.getName()); } else { return new ResponseEntity<>("Succes", HttpStatus.UNAUTHORIZED); } Groups group = this.groupServcicxe.getGroupById(id).orElseThrow(() -> new NullPointerException()); Commision com = new Commision(user); Assignment a = new Assignment(group, com); this.commisionService.save(com); this.assignmentService.save(a); return new ResponseEntity<>("Succes", HttpStatus.OK); } }