Checkpoint: return all exchanges works
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
package com.plannaplan.controllers;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@ -9,6 +10,8 @@ import com.plannaplan.entities.Exchange;
|
||||
import com.plannaplan.entities.Groups;
|
||||
import com.plannaplan.entities.User;
|
||||
import com.plannaplan.exceptions.UserNotFoundException;
|
||||
import com.plannaplan.responses.mappers.ExchangeResponseMappers;
|
||||
import com.plannaplan.responses.models.ExchangeResponse;
|
||||
import com.plannaplan.services.AssignmentService;
|
||||
import com.plannaplan.services.ExchangeService;
|
||||
import com.plannaplan.services.GroupService;
|
||||
@ -19,6 +22,7 @@ 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.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@ -104,4 +108,18 @@ public class ExchangeController extends TokenBasedController{
|
||||
this.exchangeService.deleteExchange(exchangeToDelete);
|
||||
return new ResponseEntity<>("Success", HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping("/exchange/all")
|
||||
@ApiOperation(value = "Get exchange offers")
|
||||
public ResponseEntity<List<ExchangeResponse>> getExchange()
|
||||
throws UserNotFoundException {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,14 @@
|
||||
package com.plannaplan.responses.mappers;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.plannaplan.entities.Exchange;
|
||||
import com.plannaplan.responses.models.ExchangeResponse;
|
||||
|
||||
public class ExchangeResponseMappers {
|
||||
public static final List<ExchangeResponse> mapToDefaultResponse(List<Exchange> exchanges) {
|
||||
return exchanges.stream().filter(Objects::nonNull).map(ExchangeResponse::new).collect(Collectors.toList());
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.plannaplan.responses.models;
|
||||
|
||||
import com.plannaplan.entities.Exchange;
|
||||
|
||||
public class ExchangeResponse {
|
||||
private Long id;
|
||||
private GroupDefaultResponse ownedAssignment;
|
||||
private GroupDefaultResponse desiredGroup;
|
||||
|
||||
public ExchangeResponse(Exchange exchange){
|
||||
this.id = exchange.getId();
|
||||
this.ownedAssignment = new GroupDefaultResponse(exchange.getOwnedAssignment().getGroup());
|
||||
this.desiredGroup = new GroupDefaultResponse(exchange.getDesiredAssignment());
|
||||
}
|
||||
|
||||
public GroupDefaultResponse getDesiredGroup() {
|
||||
return desiredGroup;
|
||||
}
|
||||
public void setDesiredGroup(GroupDefaultResponse desiredGroup) {
|
||||
this.desiredGroup = desiredGroup;
|
||||
}
|
||||
public GroupDefaultResponse getOwnedAssignment() {
|
||||
return ownedAssignment;
|
||||
}
|
||||
public void setOwnedAssignment(GroupDefaultResponse ownedAssignment) {
|
||||
this.ownedAssignment = ownedAssignment;
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user