Skip to content

Commit

Permalink
feat: get specific stock ownership (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
izaiasmachado authored Mar 6, 2024
1 parent 0bd23f7 commit 8bb50c2
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@

import com.mandacarubroker.domain.position.ResponseStockOwnershipDTO;
import com.mandacarubroker.service.PortfolioService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;
import java.util.Optional;

@Tag(name = "Portfólio de Ativos", description = "Operações relacionadas com portfólios de ativos")
@RestController
@RequestMapping("/portfolio")
public class PortfolioController {
Expand All @@ -17,8 +26,28 @@ public PortfolioController(final PortfolioService receivedPortfolioService) {
this.portfolioService = receivedPortfolioService;
}

@Operation(summary = "Listagem do portfólio de ativos do usuário", description = "Listagem do portfólio de ativos do usuário autenticado")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Portfólio encontrado")
})
@GetMapping
public List<ResponseStockOwnershipDTO> getAuthenticatedUserStockPortfolio() {
return portfolioService.getAuthenticatedUserStockPortfolio();
}

@Operation(summary = "Detalhes sobre a posse de ações de uma empresa", description = "Mostra detalhes sobre a posse de ações de uma empresa por um usuário autenticado")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Ação encontrada"),
@ApiResponse(responseCode = "404", description = "Ação não encontrada")
})
@GetMapping("/stock/{stockId}")
public ResponseStockOwnershipDTO getStockPositionById(@PathVariable final String stockId) {
Optional<ResponseStockOwnershipDTO> stockPosition = portfolioService.getAuthenticatedUserStockOwnershipByStockId(stockId);

if (stockPosition.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Stock ownership not found");
}

return stockPosition.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@
import com.mandacarubroker.domain.stock.Stock;

public record ResponseStockOwnershipDTO(
String id,
ResponseStockDTO stock,
int totalShares,
double positionValue
) {
ResponseStockDTO stock,
int totalShares,
double positionValue) {
public static ResponseStockOwnershipDTO fromStockPosition(final StockOwnership stockPosition) {
final Stock stock = stockPosition.getStock();

return new ResponseStockOwnershipDTO(
stockPosition.getId(),
ResponseStockDTO.fromStock(stock),
stockPosition.getShares(),
stockPosition.getTotalValue()
);
ResponseStockDTO.fromStock(stock),
stockPosition.getShares(),
stockPosition.getTotalValue());
}

public static ResponseStockOwnershipDTO fromStock(final Stock stock) {
return new ResponseStockOwnershipDTO(
ResponseStockDTO.fromStock(stock),
0,
0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

public interface StockOwnershipRepository extends JpaRepository<StockOwnership, String> {
List<StockOwnership> findByUserId(String userId);
StockOwnership findByUserIdAndStockId(String userId, String stockId);
}
31 changes: 30 additions & 1 deletion src/main/java/com/mandacarubroker/service/PortfolioService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
import com.mandacarubroker.domain.position.ResponseStockOwnershipDTO;
import com.mandacarubroker.domain.position.StockOwnership;
import com.mandacarubroker.domain.position.StockOwnershipRepository;
import com.mandacarubroker.domain.stock.Stock;
import com.mandacarubroker.domain.stock.StockRepository;
import com.mandacarubroker.domain.user.User;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class PortfolioService {
private final StockOwnershipRepository stockPositionRepository;
private final StockRepository stockRepository;

public PortfolioService(final StockOwnershipRepository receivedStockPositionRepository) {

public PortfolioService(final StockOwnershipRepository receivedStockPositionRepository, final StockRepository receivedStockRepository) {
this.stockPositionRepository = receivedStockPositionRepository;
this.stockRepository = receivedStockRepository;
}

public List<ResponseStockOwnershipDTO> getAuthenticatedUserStockPortfolio() {
Expand All @@ -29,4 +35,27 @@ public List<ResponseStockOwnershipDTO> getPortfolioByUserId(final String userId)
.map(ResponseStockOwnershipDTO::fromStockPosition)
.toList();
}

public Optional<ResponseStockOwnershipDTO> getAuthenticatedUserStockOwnershipByStockId(final String stockId) {
User user = AuthService.getAuthenticatedUser();
return getStockOwnershipByStockId(user, stockId);
}

public Optional<ResponseStockOwnershipDTO> getStockOwnershipByStockId(final User user, final String stockId) {
String userId = user.getId();
Stock stock = stockRepository.findById(stockId).orElse(null);

if (stock == null) {
return Optional.empty();
}

StockOwnership stockPosition = stockPositionRepository.findByUserIdAndStockId(userId, stockId);

if (stockPosition == null) {
return Optional.of(ResponseStockOwnershipDTO.fromStock(stock));
}

ResponseStockOwnershipDTO responseStockOwnershipDTO = ResponseStockOwnershipDTO.fromStockPosition(stockPosition);
return Optional.of(responseStockOwnershipDTO);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.mandacarubroker.controller;

import com.mandacarubroker.domain.position.ResponseStockOwnershipDTO;
import com.mandacarubroker.domain.stock.RequestStockDTO;
import com.mandacarubroker.domain.stock.ResponseStockDTO;
import com.mandacarubroker.domain.stock.Stock;
import com.mandacarubroker.security.SecuritySecretsMock;
import com.mandacarubroker.service.PortfolioService;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -14,19 +12,19 @@
import java.util.List;

import static com.mandacarubroker.domain.stock.StockUtils.assertResponseStockDTOEqualsStock;
import static com.mandacarubroker.domain.stock.StockUtils.assertStocksAreEqual;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class PortfolioControllerTest {
@MockBean
private PortfolioService portfolioService;
private final ResponseStockDTO responseAppleStockDTO = new ResponseStockDTO("id-apple","AAPL", "Apple Inc", 100.00);
private final ResponseStockDTO responseGoogleStockDTO = new ResponseStockDTO("id-google", "GOOGL", "Alphabet Inc", 2000.00);
private final ResponseStockDTO responseAppleStockDTO = new ResponseStockDTO("id-apple", "AAPL", "Apple Inc",
100.00);
private final ResponseStockDTO responseGoogleStockDTO = new ResponseStockDTO("id-google", "GOOGL", "Alphabet Inc",
2000.00);

private final List<ResponseStockOwnershipDTO> storedStockPortfolio = List.of(
new ResponseStockOwnershipDTO("apple-stock-id", responseAppleStockDTO, 100, 10000.00),
new ResponseStockOwnershipDTO("google-stock-id", responseGoogleStockDTO, 200, 400000.00)
);
new ResponseStockOwnershipDTO(responseAppleStockDTO, 100, 10000.00),
new ResponseStockOwnershipDTO(responseGoogleStockDTO, 200, 400000.00));

@BeforeEach
void setUp() {
Expand All @@ -50,7 +48,6 @@ void itShouldReturnTheAuthenticatedUserStockPortfolio() {
ResponseStockDTO expectedStock = expectedStockOwnership.stock();
ResponseStockDTO actualStock = actualStockOwnership.stock();

assertEquals(expectedStockOwnership.id(), actualStockOwnership.id());
assertEquals(expectedStockOwnership.totalShares(), actualStockOwnership.totalShares());
assertEquals(expectedStockOwnership.positionValue(), actualStockOwnership.positionValue());
assertResponseStockDTOEqualsStock(expectedStock, actualStock);
Expand Down
25 changes: 14 additions & 11 deletions src/test/java/com/mandacarubroker/service/PortfolioServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.mandacarubroker.domain.stock.RequestStockDTO;
import com.mandacarubroker.domain.stock.ResponseStockDTO;
import com.mandacarubroker.domain.stock.Stock;
import com.mandacarubroker.domain.stock.StockRepository;
import com.mandacarubroker.domain.user.RequestUserDTO;
import com.mandacarubroker.domain.user.User;
import com.mandacarubroker.security.SecuritySecretsMock;
Expand All @@ -19,14 +20,16 @@
import java.util.List;

import static com.mandacarubroker.domain.stock.StockUtils.assertResponseStockDTOEqualsStock;
import static com.mandacarubroker.domain.stock.StockUtils.assertStocksAreEqual;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mockStatic;

public class PortfolioServiceTest {
@MockBean
private StockOwnershipRepository stockPositionRepository;

@MockBean
private StockRepository stockRepository;

private PortfolioService portfolioService;

private final RequestUserDTO validRequestUserDTO = new RequestUserDTO(
Expand All @@ -36,26 +39,25 @@ public class PortfolioServiceTest {
"Marcos",
"Loiola",
LocalDate.of(2002, 2, 26),
0.25
);
0.25);

private final User validUser = new User(validRequestUserDTO);

private final RequestStockDTO requestAppleStockDTO = new RequestStockDTO("AAPL", "Apple Inc", 100.00);
private final RequestStockDTO requestGoogleStockDTO = new RequestStockDTO("GOOGL", "Alphabet Inc", 2000.00);
private final ResponseStockDTO responseAppleStockDTO = new ResponseStockDTO("apple-id", "AAPL", "Apple Inc", 100.00);
private final ResponseStockDTO responseGoogleStockDTO = new ResponseStockDTO("google-id", "GOOGL", "Alphabet Inc", 2000.00);
private final ResponseStockDTO responseAppleStockDTO = new ResponseStockDTO("apple-id", "AAPL", "Apple Inc",
100.00);
private final ResponseStockDTO responseGoogleStockDTO = new ResponseStockDTO("google-id", "GOOGL", "Alphabet Inc",
2000.00);
private final Stock appleStock = new Stock(requestAppleStockDTO);
private final Stock googleStock = new Stock(requestGoogleStockDTO);
private final List<ResponseStockOwnershipDTO> storedStockPortfolio = List.of(
new ResponseStockOwnershipDTO("apple-stock-id", responseAppleStockDTO, 100, 10000.00),
new ResponseStockOwnershipDTO("google-stock-id", responseGoogleStockDTO, 200, 400000.00)
);
new ResponseStockOwnershipDTO(responseAppleStockDTO, 100, 10000.00),
new ResponseStockOwnershipDTO(responseGoogleStockDTO, 200, 400000.00));

private final List<StockOwnership> givenStockOwnerships = List.of(
new StockOwnership(new RequestStockOwnershipDTO(100), appleStock, validUser),
new StockOwnership(new RequestStockOwnershipDTO(200), googleStock, validUser)
);
new StockOwnership(new RequestStockOwnershipDTO(200), googleStock, validUser));

@BeforeEach
void setUp() {
Expand All @@ -65,9 +67,10 @@ void setUp() {
googleStock.setId("google-id");

stockPositionRepository = Mockito.mock(StockOwnershipRepository.class);
stockRepository = Mockito.mock(StockRepository.class);
Mockito.when(stockPositionRepository.findByUserId(validUser.getId())).thenReturn(givenStockOwnerships);

portfolioService = new PortfolioService(stockPositionRepository);
portfolioService = new PortfolioService(stockPositionRepository, stockRepository);
}

@Test
Expand Down

0 comments on commit 8bb50c2

Please sign in to comment.