-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from onesimus-wiafe/SCRUM-65-frontend-portfoli…
…o-list-page Scrum 65 frontend portfolio list page
- Loading branch information
Showing
68 changed files
with
1,050 additions
and
443 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 65 additions & 16 deletions
81
...rvice/src/main/java/com/joe/trading/order_processing/controllers/PortfolioController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,83 @@ | ||
package com.joe.trading.order_processing.controllers; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.joe.trading.order_processing.entities.dto.PortfolioRequestDTO; | ||
import com.joe.trading.order_processing.entities.dto.PortfolioResponseDTO; | ||
import com.joe.trading.order_processing.services.PortfolioService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
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.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.joe.trading.order_processing.entities.User; | ||
import com.joe.trading.order_processing.entities.dto.CreatePortfolioRequestDTO; | ||
import com.joe.trading.order_processing.entities.dto.PortfolioFilterRequestDto; | ||
import com.joe.trading.order_processing.entities.dto.PortfolioResponseDTO; | ||
import com.joe.trading.order_processing.mappers.PortfolioMapper; | ||
import com.joe.trading.order_processing.services.PortfolioService; | ||
import com.joe.trading.shared.dtos.PaginatedResponseDto; | ||
import com.joe.trading.shared.exceptions.ResourceNotFoundException; | ||
|
||
import lombok.AllArgsConstructor; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/portfolio") | ||
@AllArgsConstructor | ||
@RequestMapping("/api/v1/portfolios") | ||
public class PortfolioController { | ||
|
||
private final PortfolioService portfolioService; | ||
private final PortfolioMapper portfolioMapper; | ||
|
||
@PostMapping | ||
@PreAuthorize("hasRole('USER')") | ||
public ResponseEntity<PortfolioResponseDTO> createPortfolio(@RequestBody CreatePortfolioRequestDTO request) { | ||
var auth = SecurityContextHolder.getContext().getAuthentication(); | ||
var principal = (User) auth.getPrincipal(); | ||
|
||
@Autowired | ||
public PortfolioController(PortfolioService portfolioService) { | ||
this.portfolioService = portfolioService; | ||
var portfolio = portfolioService.createPortfolio(principal.getId(), request); | ||
|
||
return ResponseEntity.status(HttpStatus.CREATED) | ||
.body(portfolioMapper.toPortfolioResponseDTO(portfolio)); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<PortfolioResponseDTO> createPortfolio(@RequestBody PortfolioRequestDTO request) throws JsonProcessingException { | ||
@DeleteMapping("/{id}") | ||
@PreAuthorize("hasRole('USER')") | ||
public ResponseEntity<Boolean> deletePortfolio(@PathVariable("id") Long portfolioId) throws ResourceNotFoundException { | ||
var auth = SecurityContextHolder.getContext().getAuthentication(); | ||
var principal = (User) auth.getPrincipal(); | ||
|
||
return ResponseEntity.ok(portfolioService.deletePortfolio(principal.getId(), portfolioId)); | ||
} | ||
|
||
return ResponseEntity.status(HttpStatus.CREATED).body(portfolioService.saveNewPortfolio(request.getUserId(), request.getPortfolioName())); | ||
@GetMapping("/{id}") | ||
@PreAuthorize("isAuthenticated()") | ||
public ResponseEntity<PortfolioResponseDTO> getPortfolio(@PathVariable("id") Long portfolioId) throws ResourceNotFoundException { | ||
var auth = SecurityContextHolder.getContext().getAuthentication(); | ||
var principal = (User) auth.getPrincipal(); | ||
|
||
var portfolio = portfolioService.getPortfolio(principal.getId(), portfolioId); | ||
|
||
return ResponseEntity.ok(portfolioMapper.toPortfolioResponseDTO(portfolio)); | ||
} | ||
|
||
@DeleteMapping | ||
public ResponseEntity<Boolean> deletePortfolio(PortfolioRequestDTO request){ | ||
@GetMapping | ||
@PreAuthorize("isAuthenticated()") | ||
public ResponseEntity<PaginatedResponseDto<PortfolioResponseDTO>> getPortfolios(PortfolioFilterRequestDto filter) throws ResourceNotFoundException { | ||
var auth = SecurityContextHolder.getContext().getAuthentication(); | ||
var principal = (User) auth.getPrincipal(); | ||
|
||
var portfolios = portfolioService.getPortfoliosByUserId(principal.getId(), filter); | ||
|
||
var response = new PaginatedResponseDto<>( | ||
portfolioMapper.toPortfolioResponseDTOs(portfolios.getContent()), | ||
portfolios.getTotalPages(), | ||
portfolios.getTotalElements(), | ||
portfolios.getNumber() + 1 | ||
); | ||
|
||
return ResponseEntity.ok(portfolioService.deletePortfolio(request)); | ||
return ResponseEntity.ok(response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 19 additions & 16 deletions
35
...ing/entities/dto/PortfolioRequestDTO.java → ...tities/dto/CreatePortfolioRequestDTO.java
100755 → 100644
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
package com.joe.trading.order_processing.entities.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class PortfolioRequestDTO { | ||
private Long userId; | ||
private String portfolioName; | ||
private Long portfolioId; | ||
} | ||
package com.joe.trading.order_processing.entities.dto; | ||
|
||
import jakarta.validation.constraints.NotEmpty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
@Getter | ||
@Setter | ||
@ToString | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class CreatePortfolioRequestDTO { | ||
@NotEmpty(message = "Name is required") | ||
private String name; | ||
private String description; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...rc/main/java/com/joe/trading/order_processing/entities/dto/PortfolioFilterRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.joe.trading.order_processing.entities.dto; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import jakarta.validation.constraints.Min; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@ToString | ||
public class PortfolioFilterRequestDto { | ||
private String name; | ||
private String description; | ||
private LocalDateTime createdFrom; | ||
private LocalDateTime createdTo; | ||
|
||
@Min(value = 1, message = "Page number must be greater than 0") | ||
private int page = 1; | ||
|
||
@Min(value = 1, message = "Page size must be greater than 0") | ||
private int size = 10; | ||
private String sortBy = "id"; | ||
|
||
private String sortDir = "asc"; | ||
} |
15 changes: 10 additions & 5 deletions
15
...ice/src/main/java/com/joe/trading/order_processing/entities/dto/PortfolioResponseDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,25 @@ | ||
package com.joe.trading.order_processing.entities.dto; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
import com.joe.trading.order_processing.entities.Stock; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class PortfolioResponseDTO { | ||
private Long portfolioId; | ||
private String portfolioName; | ||
private Double portfolioValue; | ||
private Long id; | ||
private String name; | ||
private String description; | ||
private Double value; | ||
private String state; | ||
private LocalDateTime createdAt; | ||
private List<Stock> stocks; | ||
} |
25 changes: 25 additions & 0 deletions
25
...essingService/src/main/java/com/joe/trading/order_processing/mappers/PortfolioMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.joe.trading.order_processing.mappers; | ||
|
||
import java.util.List; | ||
|
||
import org.mapstruct.Mapper; | ||
import org.mapstruct.factory.Mappers; | ||
|
||
import com.joe.trading.order_processing.entities.Portfolio; | ||
import com.joe.trading.order_processing.entities.dto.PortfolioResponseDTO; | ||
import com.joe.trading.shared.dtos.PortfolioEventDto; | ||
|
||
@Mapper(componentModel = "spring") | ||
public interface PortfolioMapper { | ||
PortfolioMapper INSTANCE = Mappers.getMapper(PortfolioMapper.class); | ||
|
||
Portfolio toPortfolio(PortfolioEventDto portfolioEventDto); | ||
|
||
Portfolio toPortfolio(PortfolioResponseDTO portfolioResponseDTO); | ||
|
||
PortfolioResponseDTO toPortfolioResponseDTO(Portfolio portfolio); | ||
|
||
List<PortfolioResponseDTO> toPortfolioResponseDTOs(List<Portfolio> portfolios); | ||
|
||
PortfolioEventDto toPortfolioEventDto(Portfolio portfolio); | ||
} |
Oops, something went wrong.