diff --git a/Sources/Grodt/Controllers/InvestmentController.swift b/Sources/Grodt/Controllers/InvestmentController.swift index 5e2189f..cf4a90a 100644 --- a/Sources/Grodt/Controllers/InvestmentController.swift +++ b/Sources/Grodt/Controllers/InvestmentController.swift @@ -13,12 +13,24 @@ struct InvestmentController: RouteCollection { func boot(routes: Vapor.RoutesBuilder) throws { let investments = routes.grouped("investments") + investments.get(use: allInvestments) investments.group(":ticker") { investment in investment.get(use: invesetmentDetail) } } + func allInvestments(req: Request) async throws -> [InvestmentDTO] { + guard let userID = req.auth.get(User.self)?.id else { + throw Abort(.badRequest) + } + + let transactions = try await portfolioRepository.allPortfolios(for: userID) + .flatMap { $0.transactions } + + return try await dataMapper.investments(from: transactions) + } + func invesetmentDetail(req: Request) async throws -> InvestmentDetailDTO { let ticker: String = try req.requiredParameter(named: "ticker") @@ -35,3 +47,4 @@ struct InvestmentController: RouteCollection { } extension InvestmentDetailDTO: Content { } +extension InvestmentDTO: Content { } diff --git a/Sources/Grodt/DTOs/DTOMappers/InvestmentDTOMapper.swift b/Sources/Grodt/DTOs/DTOMappers/InvestmentDTOMapper.swift index 1c71e68..890d68b 100644 --- a/Sources/Grodt/DTOs/DTOMappers/InvestmentDTOMapper.swift +++ b/Sources/Grodt/DTOs/DTOMappers/InvestmentDTOMapper.swift @@ -53,7 +53,9 @@ class InvestmentDTOMapper { ) } - return investments + return investments.sorted { lft, rgh in + lft.totalReturn > rgh.totalReturn + } } func investmentDetail(from transactions: [Transaction])async throws -> InvestmentDetailDTO {