backend/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java
Marcin Woźniak 56120c4724
Checkpoint: deleting is working
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
2021-01-07 16:01:25 +01:00

108 lines
4.3 KiB
Java

package com.plannaplan.controllers;
import java.util.Map;
import java.util.Optional;
import com.plannaplan.App;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Exchange;
import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User;
import com.plannaplan.exceptions.UserNotFoundException;
import com.plannaplan.services.AssignmentService;
import com.plannaplan.services.ExchangeService;
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.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@RestController
@CrossOrigin
@RequestMapping("/api/" + App.API_VERSION + "/exchanges")
@Api(tags = { "Exchange" }, value = "Exchange", description = "Endpoint to exchange with accepted assignments.")
public class ExchangeController extends TokenBasedController{
/*
* Endpoint do wylistowania exchangow użytkownika
* Endpoint do usuwania exchanga
*/
@Autowired
private GroupService groupService;
@Autowired
private AssignmentService assignmentService;
@Autowired
private ExchangeService exchangeService;
@PostMapping("/exchange")
@ApiOperation(value = "Return all courses")
public ResponseEntity<String> createExchange(@RequestBody Map<String, Long> exchangeRequest)
throws UserNotFoundException {
final User asker = this.getCurrentUser()
.orElseThrow(() -> new UserNotFoundException("Invalid token"));
final Long assignmentId = exchangeRequest.get("assignment");
final Long groupId = exchangeRequest.get("group");
if(assignmentId == null || groupId == null ){
return new ResponseEntity<>("Some of values are missing", HttpStatus.BAD_REQUEST);
}
final Optional<Assignment> assignment = this.assignmentService.getById(assignmentId);
final Optional<Groups> group = this.groupService.getGroupById(groupId);
if(assignment.isEmpty() || group.isEmpty()){
return new ResponseEntity<>("Some of provided value does not exist.", HttpStatus.BAD_REQUEST);
}
final Assignment assignmentInstance = assignment.get();
final Groups groupInstance = group.get();
if(!(assignmentInstance.getCommision().getCommisionOwner().getId().equals(asker.getId()) && assignmentInstance.isAccepted())){
return new ResponseEntity<>("Some of problems appeared. Check if you have access to given assignment and if it is accepted or the exchange has not been already added.", HttpStatus.BAD_REQUEST);
}
this.exchangeService.save(new Exchange(assignmentInstance, groupInstance));
return new ResponseEntity<>("Success", HttpStatus.OK);
}
@DeleteMapping("/exchange/{id}")
@ApiOperation(value = "Delete exchange offer")
public ResponseEntity<String> deleteExchange(@PathVariable(name = "id", required = false) Long offerId)
throws UserNotFoundException {
final User asker = this.getCurrentUser()
.orElseThrow(() -> new UserNotFoundException("Invalid token"));
final Optional<Exchange> exchange = this.exchangeService.getById(offerId);
if(exchange.isEmpty()){
return new ResponseEntity<>("Given offer does not exist.", HttpStatus.BAD_REQUEST);
}
final Exchange exchangeToDelete = exchange.get();
if(!(exchangeToDelete.getOwnedAssignment().getCommision().getCommisionOwner().getId().equals(asker.getId()))){
return new ResponseEntity<>("You have not permission for that action.", HttpStatus.BAD_REQUEST);
}
this.exchangeService.deleteExchange(exchangeToDelete);
return new ResponseEntity<>("Success", HttpStatus.OK);
}
}