Checkpoints: Part 1 of Added docs

Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
2021-01-06 16:23:18 +01:00
parent f24e62a335
commit df7701ebc8
4 changed files with 57 additions and 29 deletions

View File

@ -8,11 +8,10 @@ import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Exchange;
import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User;
import com.plannaplan.repositories.ExchangeRepository;
import com.plannaplan.exceptions.UserNotFoundException;
import com.plannaplan.services.AssignmentService;
import com.plannaplan.services.ExchangeService;
import com.plannaplan.services.GroupService;
import com.plannaplan.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
@ -30,10 +29,13 @@ import io.swagger.annotations.ApiOperation;
@CrossOrigin
@RequestMapping("/api/" + App.API_VERSION + "/exchanges")
@Api(tags = { "Exchange" }, value = "Exchange", description = "Endpoint to exchange with accepted assignments.")
public class ExchangeController {
public class ExchangeController extends TokenBasedController{
@Autowired
private UserService userService;
/*
* Endpoint do wylistowania exchangow użytkownika
* Endpoint do usuwania exchanga
*/
@Autowired
private GroupService groupService;
@ -46,28 +48,33 @@ public class ExchangeController {
@PostMapping("/exchange")
@ApiOperation(value = "Return all courses")
public ResponseEntity<String> createExchange(@RequestBody Map<String, Long> exchangeRequest){
public ResponseEntity<String> createExchange(@RequestBody Map<String, Long> exchangeRequest)
throws UserNotFoundException {
final Long userId = exchangeRequest.get("user");
final User asker = this.getCurrentUser()
.orElseThrow(() -> new UserNotFoundException("Invalid token"));
final Long assignmentId = exchangeRequest.get("assignment");
final Long groupId = exchangeRequest.get("group");
if(userId == null || assignmentId == null|| groupId == null ){
if(assignmentId == null || groupId == null ){
return new ResponseEntity<>("Some of values are missing", HttpStatus.BAD_REQUEST);
}
final Optional<User> user = this.userService.getById(userId);
final Optional<Assignment> assignment = this.assignmentService.getById(assignmentId);
final Optional<Groups> group = this.groupService.getGroupById(groupId);
if(user.isEmpty() || assignment.isEmpty() || group.isEmpty()){
if(assignment.isEmpty() || group.isEmpty()){
return new ResponseEntity<>("Some of provided value does not exist.", HttpStatus.BAD_REQUEST);
}
this.exchangeService.save(new Exchange(user.get(), assignment.get(), group.get()));
final Assignment assignmentInstance = assignment.get();
//System.out.println("KUPA");
if(!(assignmentInstance.getCommision().getCommisionOwner().getId() == asker.getId() && assignmentInstance.isAccepted())){
return new ResponseEntity<>("Some of problems appeared. Check if you have access to given assignment and if it is accepted", HttpStatus.BAD_REQUEST);
}
return null;
this.exchangeService.save(new Exchange(assignment.get(), group.get()));
return new ResponseEntity<>("Success", HttpStatus.OK);
}
}