Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Sources/Grodt/Controllers/InvestmentController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -35,3 +47,4 @@ struct InvestmentController: RouteCollection {
}

extension InvestmentDetailDTO: Content { }
extension InvestmentDTO: Content { }
4 changes: 3 additions & 1 deletion Sources/Grodt/DTOs/DTOMappers/InvestmentDTOMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down