Checkpoint: return all exchanges works

Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2021-01-07 16:51:35 +01:00
parent 56120c4724
commit 842e38898a
Signed by: y0rune
GPG Key ID: F204C385F57EB348
6 changed files with 109 additions and 11 deletions

View File

@ -8,7 +8,7 @@ import javax.persistence.JoinColumn;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
/** /**
* Entity that keeps user exchange offer. * Entity that keeps user exchange offer.
*/ */
@Entity @Entity
public class Exchange { public class Exchange {
@ -20,29 +20,49 @@ public class Exchange {
@OneToOne @OneToOne
@JoinColumn(name = "owned_id", unique = true) @JoinColumn(name = "owned_id", unique = true)
private Assignment ownedAssignment; private Assignment ownedAssignment;
@OneToOne @OneToOne
@JoinColumn(name = "desired_id") @JoinColumn(name = "desired_id")
private Groups desiredAssignment; private Groups desiredAssignment;
public Exchange() {
private Long ownerId;
public Exchange(){
} }
public Exchange(Assignment ownedAssignment, Groups desiredAssignment){ /**
* @return Long ID of exchange trader
*/
public Long getOwnerId() {
return ownerId;
}
/**
* @param ownerId ID of exchange trader
*/
public void setOwnerId(Long ownerId) {
this.ownerId = ownerId;
}
/**
*
* @param ownedAssignment Assignment which owner would like to trade
* @param desiredAssignment Groups instance that trader wants
*/
public Exchange(Assignment ownedAssignment, Groups desiredAssignment) {
this.ownedAssignment = ownedAssignment; this.ownedAssignment = ownedAssignment;
this.desiredAssignment = desiredAssignment; this.desiredAssignment = desiredAssignment;
this.ownerId = this.ownedAssignment.getCommision().getCommisionOwner().getId();
} }
/** /**
* @return Long ID in database * @return Long ID in database
*/ */
public Long getId(){ public Long getId(){
return this.id; return this.id;
} }
/** /**
* @return Groups Target group * @return Groups Target group
*/ */
@ -50,7 +70,6 @@ public class Exchange {
return desiredAssignment; return desiredAssignment;
} }
/** /**
* @param desiredAssignment Target group * @param desiredAssignment Target group
*/ */
@ -58,7 +77,6 @@ public class Exchange {
this.desiredAssignment = desiredAssignment; this.desiredAssignment = desiredAssignment;
} }
/** /**
* @return Assignment Owned assignment * @return Assignment Owned assignment
*/ */
@ -66,7 +84,6 @@ public class Exchange {
return ownedAssignment; return ownedAssignment;
} }
/** /**
* @param ownedAssignment Owned assignment * @param ownedAssignment Owned assignment
*/ */

View File

@ -1,5 +1,6 @@
package com.plannaplan.repositories; package com.plannaplan.repositories;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
@ -16,5 +17,8 @@ public interface ExchangeRepository extends JpaRepository<Exchange, Long>{
@Query("FROM Exchange WHERE owned_id = ?1 AND desired_id = ?2") @Query("FROM Exchange WHERE owned_id = ?1 AND desired_id = ?2")
Optional<Exchange> checkForExchange(@Param("owned_id") Assignment assignment, @Param("desired_id") Groups group); Optional<Exchange> checkForExchange(@Param("owned_id") Assignment assignment, @Param("desired_id") Groups group);
@Query("FROM Exchange WHERE ownerId = ?1")
List<Exchange> getByUserId(@Param("id") Long id);
} }

View File

@ -1,5 +1,6 @@
package com.plannaplan.services; package com.plannaplan.services;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
@ -32,6 +33,14 @@ public class ExchangeService {
return this.repo.findById(id); return this.repo.findById(id);
} }
/**
* @param id Id of user
* @return List of exchanges that belong to user
*/
public List<Exchange> getByUserId(Long id){
return this.repo.getByUserId(id);
}
/** /**
* @param entity Exchange entity which we would like to delete * @param entity Exchange entity which we would like to delete
*/ */

View File

@ -1,5 +1,6 @@
package com.plannaplan.controllers; package com.plannaplan.controllers;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@ -9,6 +10,8 @@ import com.plannaplan.entities.Exchange;
import com.plannaplan.entities.Groups; import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User; import com.plannaplan.entities.User;
import com.plannaplan.exceptions.UserNotFoundException; 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.AssignmentService;
import com.plannaplan.services.ExchangeService; import com.plannaplan.services.ExchangeService;
import com.plannaplan.services.GroupService; 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.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping; 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.PathVariable;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -104,4 +108,18 @@ public class ExchangeController extends TokenBasedController{
this.exchangeService.deleteExchange(exchangeToDelete); this.exchangeService.deleteExchange(exchangeToDelete);
return new ResponseEntity<>("Success", HttpStatus.OK); 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);
}
} }

View File

@ -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());
}
}

View File

@ -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;
}
}