Skip to content

Commit 71ec5e8

Browse files
authored
Add endpoint for all investments (#10)
1 parent 9d6ec38 commit 71ec5e8

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Sources/Grodt/Controllers/InvestmentController.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,24 @@ struct InvestmentController: RouteCollection {
1313

1414
func boot(routes: Vapor.RoutesBuilder) throws {
1515
let investments = routes.grouped("investments")
16+
investments.get(use: allInvestments)
1617

1718
investments.group(":ticker") { investment in
1819
investment.get(use: invesetmentDetail)
1920
}
2021
}
2122

23+
func allInvestments(req: Request) async throws -> [InvestmentDTO] {
24+
guard let userID = req.auth.get(User.self)?.id else {
25+
throw Abort(.badRequest)
26+
}
27+
28+
let transactions = try await portfolioRepository.allPortfolios(for: userID)
29+
.flatMap { $0.transactions }
30+
31+
return try await dataMapper.investments(from: transactions)
32+
}
33+
2234
func invesetmentDetail(req: Request) async throws -> InvestmentDetailDTO {
2335
let ticker: String = try req.requiredParameter(named: "ticker")
2436

@@ -35,3 +47,4 @@ struct InvestmentController: RouteCollection {
3547
}
3648

3749
extension InvestmentDetailDTO: Content { }
50+
extension InvestmentDTO: Content { }

Sources/Grodt/DTOs/DTOMappers/InvestmentDTOMapper.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ class InvestmentDTOMapper {
5353
)
5454
}
5555

56-
return investments
56+
return investments.sorted { lft, rgh in
57+
lft.totalReturn > rgh.totalReturn
58+
}
5759
}
5860

5961
func investmentDetail(from transactions: [Transaction])async throws -> InvestmentDetailDTO {

0 commit comments

Comments
 (0)