Checkpoint: Added ExchangeServiceTest
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
@ -30,11 +30,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import io.swagger.annotations.Example;
|
||||
import io.swagger.annotations.ExampleProperty;
|
||||
|
||||
@RestController
|
||||
@CrossOrigin
|
||||
@ -47,13 +44,13 @@ public class ExchangeController extends TokenBasedController{
|
||||
|
||||
@Autowired
|
||||
private AssignmentService assignmentService;
|
||||
|
||||
|
||||
@Autowired
|
||||
private ExchangeService exchangeService;
|
||||
|
||||
@PostMapping("/exchange")
|
||||
@ApiOperation(value = "Creates exchange offer.")
|
||||
public ResponseEntity<String> createExchange(
|
||||
public ResponseEntity<String> createExchange(
|
||||
@ApiParam( value = "Json object that contains assignment to trade and desired group")
|
||||
@RequestBody
|
||||
Map<String, Long> exchangeRequest)
|
||||
@ -63,7 +60,7 @@ public class ExchangeController extends TokenBasedController{
|
||||
.orElseThrow(() -> new UserNotFoundException("Invalid token"));
|
||||
final Long assignmentId = exchangeRequest.get("assignment");
|
||||
final Long groupId = exchangeRequest.get("group");
|
||||
final List<Long> ownedGroups = asker.getStudentRegisteredGrups().stream().map(Groups::getId).collect(Collectors.toList());
|
||||
final List<Long> ownedGroups = asker.getStudentRegisteredGrups().stream().map(Groups::getId).collect(Collectors.toList());
|
||||
|
||||
if(ownedGroups.contains(groupId)){
|
||||
return new ResponseEntity<>("User has already got this group.", HttpStatus.BAD_REQUEST);
|
||||
@ -75,7 +72,7 @@ public class ExchangeController extends TokenBasedController{
|
||||
|
||||
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);
|
||||
}
|
||||
@ -101,7 +98,7 @@ public class ExchangeController extends TokenBasedController{
|
||||
.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);
|
||||
}
|
||||
@ -123,10 +120,10 @@ public class ExchangeController extends TokenBasedController{
|
||||
|
||||
final User asker = this.getCurrentUser()
|
||||
.orElseThrow(() -> new UserNotFoundException("Invalid token"));
|
||||
|
||||
|
||||
final List<Exchange> response = exchangeService.getByUserId(asker.getId());
|
||||
final List<ExchangeResponse> listOfResponses = ExchangeResponseMappers.mapToDefaultResponse(response);
|
||||
|
||||
|
||||
return new ResponseEntity<>(listOfResponses, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@ -137,7 +134,7 @@ public class ExchangeController extends TokenBasedController{
|
||||
|
||||
final User asker = this.getCurrentUser()
|
||||
.orElseThrow(() -> new UserNotFoundException("Invalid token"));
|
||||
|
||||
|
||||
final Optional<Exchange> exchange = this.exchangeService.getById(offerId);
|
||||
|
||||
if(exchange.isEmpty()){
|
||||
@ -146,9 +143,9 @@ public class ExchangeController extends TokenBasedController{
|
||||
|
||||
final Exchange exchangeInstance = exchange.get();
|
||||
if(!exchangeInstance.getOwnerId().equals(asker.getId())){
|
||||
return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
|
||||
return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
|
||||
return new ResponseEntity<>(new ExchangeResponse(exchangeInstance), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user