Skip to content

Commit

Permalink
fix: responses of routes
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasFortes12 committed Mar 5, 2024
1 parent d1b5a5d commit 8a8b600
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.mandacarubroker.domain.position.RequestStockOwnershipDTO;
import com.mandacarubroker.domain.position.ResponseStockOwnershipDTO;
import com.mandacarubroker.domain.user.ResponseUserDTO;
import com.mandacarubroker.service.PortfolioService;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -30,18 +29,18 @@ public List<ResponseStockOwnershipDTO> getAuthenticatedUserStockPortfolio() {
}

@PostMapping("/{stockId}/buy")
public ResponseEntity<ResponseUserDTO> buyStock(
public ResponseEntity<ResponseStockOwnershipDTO> buyStock(
@PathVariable final String stockId,
@RequestBody @Valid final RequestStockOwnershipDTO shares) {
ResponseUserDTO user = portfolioService.buyStock(stockId, shares);
return ResponseEntity.ok(user);
ResponseStockOwnershipDTO stockPosition = portfolioService.buyStock(stockId, shares);
return ResponseEntity.ok(stockPosition);
}

@PostMapping("/{stockId}/sell")
public ResponseEntity<ResponseUserDTO> sellStock(
public ResponseEntity<ResponseStockOwnershipDTO> sellStock(
@PathVariable final String stockId,
@RequestBody @Valid final RequestStockOwnershipDTO shares) {
ResponseUserDTO user = portfolioService.sellStock(stockId, shares);
return ResponseEntity.ok(user);
ResponseStockOwnershipDTO stockPosition = portfolioService.sellStock(stockId, shares);
return ResponseEntity.ok(stockPosition);
}
}
27 changes: 17 additions & 10 deletions src/main/java/com/mandacarubroker/service/PortfolioService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.mandacarubroker.domain.position.StockOwnership;
import com.mandacarubroker.domain.position.StockOwnershipRepository;
import com.mandacarubroker.domain.stock.Stock;
import com.mandacarubroker.domain.user.ResponseUserDTO;
import com.mandacarubroker.domain.user.User;
import com.mandacarubroker.domain.user.UserRepository;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -116,7 +115,7 @@ private void removeStockPositionFromPortfolio(final String stockPositionId) {
stockPositionRepository.deleteById(stockPositionId);
}

public ResponseUserDTO buyStock(final String stockId, final RequestStockOwnershipDTO shares) {
public ResponseStockOwnershipDTO buyStock(final String stockId, final RequestStockOwnershipDTO shares) {
User user = AuthService.getAuthenticatedUser();
Optional<Stock> stock = stockService.getStockById(stockId);

Expand All @@ -128,14 +127,13 @@ public ResponseUserDTO buyStock(final String stockId, final RequestStockOwnershi
if (userBalance < stockBoughtPrice) {
throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY, "Insufficient balance");
}

addStockPositionInPortfolio(stock.get(), user, shares);
ResponseStockOwnershipDTO boughtStockPosition = addStockPositionInPortfolio(stock.get(), user, shares);
user.setBalance(userBalance - stockBoughtPrice);

return ResponseUserDTO.fromUser(userRepository.save(user));
userRepository.save(user);
return boughtStockPosition;
}

public ResponseUserDTO sellStock(final String stockId, final RequestStockOwnershipDTO shares) {
public ResponseStockOwnershipDTO sellStock(final String stockId, final RequestStockOwnershipDTO shares) {
User user = AuthService.getAuthenticatedUser();
Optional<Stock> stock = stockService.getStockById(stockId);
Optional<ResponseStockOwnershipDTO> userStockPosition = getStockPositionByUserIdAndStockId(stockId);
Expand All @@ -146,21 +144,30 @@ public ResponseUserDTO sellStock(final String stockId, final RequestStockOwnersh
int sharesToSell = shares.shares();
int userShares = userStockPosition.get().totalShares();
if (sharesToSell > userShares) {
throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY, "User with insufficient shares to sell");
throw new ResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY,
"User with insufficient shares to sell");
}
double userBalance = user.getBalance();
double stocksSoldPrice = stock.get().getPrice() * sharesToSell;
int remainingShares = userShares - sharesToSell;
updateStockPositionShares(

Optional<StockOwnership> updatedStockPosition = updateStockPositionShares(
userStockPosition.get().id(),
new RequestStockOwnershipDTO(
remainingShares
)
);

if (updatedStockPosition.isEmpty()) {
throw new IllegalStateException("Error on update shares in portfolio");
}

if (remainingShares == 0) {
removeStockPositionFromPortfolio(userStockPosition.get().id());
}
user.setBalance(userBalance + stocksSoldPrice);
return ResponseUserDTO.fromUser(userRepository.save(user));
userRepository.save(user);

return ResponseStockOwnershipDTO.fromStockPosition(updatedStockPosition.get());
}
}

0 comments on commit 8a8b600

Please sign in to comment.