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
1 change: 1 addition & 0 deletions Sources/Grodt/Application/routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func routes(_ app: Application) async throws {
performanceRepository: brokerageAccountDailyPerformanceRepository,
performanceDTOMapper: DatedPerformanceDTOMapper(),
currencyMapper: currencyDTOMapper,
transactionDTOMapper: transactionDTOMapper,
currencyRepository: currencyRepository))
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Grodt/DTOs/DTOMappers/PortfolioDTOMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class PortfolioDTOMapper {
guard portfolio.$historicalDailyPerformance.value != nil,
let latest = portfolio.historicalDailyPerformance.max(by: { $0.date < $1.date })
else {
return PerformanceDTO(moneyIn: 0, moneyOut: 0, profit: 0, totalReturn: 0)
return PerformanceDTO.zero
}

let moneyIn = latest.moneyIn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ struct BrokerageAccountController: RouteCollection {
private let currencyMapper: CurrencyDTOMapper
private let performanceDTOMapper: DatedPerformanceDTOMapper
private let currencyRepository: CurrencyRepository
private let transactionDTOMapper: TransactionDTOMapper

init(brokerageAccountRepository: BrokerageAccountRepository,
performanceRepository: PostgresBrokerageAccountDailyPerformanceRepository,
performanceDTOMapper: DatedPerformanceDTOMapper,
currencyMapper: CurrencyDTOMapper,
transactionDTOMapper: TransactionDTOMapper,
currencyRepository: CurrencyRepository) {
self.brokerageAccountRepository = brokerageAccountRepository
self.performanceRepository = performanceRepository
self.performanceDTOMapper = performanceDTOMapper
self.currencyMapper = currencyMapper
self.transactionDTOMapper = transactionDTOMapper
self.currencyRepository = currencyRepository
}

Expand All @@ -32,13 +35,13 @@ struct BrokerageAccountController: RouteCollection {
}
}

private func list(req: Request) async throws -> [BrokerageAccountDTO] {
private func list(req: Request) async throws -> [BrokerageAccountInfoDTO] {
let userID = try req.requireUserID()
let items = try await brokerageAccountRepository.all(for: userID)
return try await items.asyncMap { model in
let performance = try await brokerageAccountRepository.performance(for: model.requireID())
let brokerage = try await model.$brokerage.get(on: req.db)
return BrokerageAccountDTO(
return BrokerageAccountInfoDTO(
id: try model.requireID(),
brokerageId: try brokerage.requireID(),
brokerageName: brokerage.name,
Expand Down Expand Up @@ -76,20 +79,23 @@ struct BrokerageAccountController: RouteCollection {
brokerageName: brokerage.name,
displayName: model.displayName,
baseCurrency: currencyMapper.currency(from: model.baseCurrency),
performance: PerformanceDTO.zero)
performance: PerformanceDTO.zero,
transacitons: [])
}

private func detail(req: Request) async throws -> BrokerageAccountDTO {
let userID = try req.requireUserID()
let model = try await requireAccount(req, userID: userID)
let brokerage = try await model.$brokerage.get(on: req.db)
let transactions = try await model.$transactions.get(on: req.db)
let performance = try await brokerageAccountRepository.performance(for: model.requireID())
return BrokerageAccountDTO(id: try model.requireID(),
return try await BrokerageAccountDTO(id: try model.requireID(),
brokerageId: try brokerage.requireID(),
brokerageName: brokerage.name,
displayName: model.displayName,
baseCurrency: currencyMapper.currency(from: model.baseCurrency),
performance: performance)
performance: performance,
transacitons: transactions.asyncMap { try await transactionDTOMapper.transaction(from: $0) })
}

private func update(req: Request) async throws -> HTTPStatus {
Expand Down Expand Up @@ -131,3 +137,4 @@ struct BrokerageAccountController: RouteCollection {
}

extension BrokerageAccountDTO: Content { }
extension BrokerageAccountInfoDTO: Content { }
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ struct BrokerageAccountDTO: Codable {
let displayName: String
let baseCurrency: CurrencyDTO
let performance: PerformanceDTO
let transacitons: [TransactionDTO]
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ struct BrokerageAccountDTOMapper {
self.database = database
}

func brokerageAccount(from brokerageAccount: BrokerageAccount) async throws -> BrokerageAccountDTO {
func brokerageAccountInfo(from brokerageAccount: BrokerageAccount) async throws -> BrokerageAccountInfoDTO {
try await brokerageAccount.$brokerage.load(on: database)
let performance = try await brokerageAccountRepository.performance(for: brokerageAccount.requireID())

return try BrokerageAccountDTO(id: brokerageAccount.requireID(),
return try BrokerageAccountInfoDTO(id: brokerageAccount.requireID(),
brokerageId: brokerageAccount.brokerage.requireID(),
brokerageName: brokerageAccount.brokerage.name,
displayName: brokerageAccount.displayName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ struct BrokerageAccountInfoDTO: Codable, Equatable {
let brokerageId: UUID
let brokerageName: String
let displayName: String
let baseCurrency: CurrencyDTO
let performance: PerformanceDTO
}
2 changes: 1 addition & 1 deletion Sources/Grodt/Endpoints/brokerages/DTO/BrokerageDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import Foundation
struct BrokerageDTO: Codable {
let id: UUID
let name: String
let accounts: [BrokerageAccountDTO]
let accounts: [BrokerageAccountInfoDTO]
let performance: PerformanceDTO
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct BrokerageDTOMapper {
func brokerage(from brokerage: Brokerage) async throws -> BrokerageDTO {
try await brokerage.$accounts.load(on: database)
let accountDTOs = try await brokerage.accounts.asyncMap {
try await accountDTOMapper.brokerageAccount(from: $0)
try await accountDTOMapper.brokerageAccountInfo(from: $0)
}
let performance = try await brokerageRepository.performance(for: brokerage.requireID())
return try BrokerageDTO(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ class TransactionDTOMapper {
brokerageAccount = BrokerageAccountInfoDTO(id: try brokerAcc.requireID(),
brokerageId: try brokerage.requireID(),
brokerageName: brokerAcc.brokerage.name,
displayName: brokerAcc.displayName)
displayName: brokerAcc.displayName,
baseCurrency: currencyDTOMapper.currency(from: brokerAcc.baseCurrency),
performance: PerformanceDTO.zero)
}
let portfolio = try await transaction.$portfolio.get(on: database)

return TransactionDTO(id: transaction.id?.uuidString ?? "",
portfolioName: transaction.portfolio.name,
portfolioName: portfolio.name,
purchaseDate: transaction.purchaseDate,
ticker: transaction.ticker,
currency: currencyDTOMapper.currency(from: transaction.currency),
Expand Down
2 changes: 1 addition & 1 deletion Tests/GrodtTests/TestConstant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum TestConstant {
}

enum PerformanceDTOs {
static let zero = PerformanceDTO(moneyIn: 0, moneyOut: 0, profit: 0, totalReturn: 0)
static let zero = PerformanceDTO.zero
}

enum Currencies {
Expand Down
Loading