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
3 changes: 3 additions & 0 deletions Sources/Grodt/Application/routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func routes(_ app: Application) async throws {
let investmentsController = InvestmentController(portfolioRepository: portfolioRepository,
dataMapper: investmentDTOMapper)

let accountController = AccountController(userRepository: PostgresUserRepository(database: app.db), dataMapper: UserDTOMapper())

let globalRateLimiter = RateLimiterMiddleware(maxRequests: 100, perSeconds: 60)
let loginRateLimiter = RateLimiterMiddleware(maxRequests: 3, perSeconds: 60)

Expand Down Expand Up @@ -75,6 +77,7 @@ func routes(_ app: Application) async throws {

try routeBuilder.register(collection: tickersController)
try routeBuilder.register(collection: investmentsController)
try routeBuilder.register(collection: accountController)
}

if app.environment != .testing {
Expand Down
34 changes: 34 additions & 0 deletions Sources/Grodt/Controllers/AccountController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Vapor
import Fluent

struct AccountController: RouteCollection {
private let userRepository: UserRepository
private let dataMapper: UserDTOMapper

init(userRepository: UserRepository,
dataMapper: UserDTOMapper) {
self.userRepository = userRepository
self.dataMapper = dataMapper
}

func boot(routes: Vapor.RoutesBuilder) throws {
let account = routes.grouped("account")
account.group("me") { me in
me.get(use: userInfo)
}
}

func userInfo(req: Request) async throws -> UserInfoDTO {
guard let userID = req.auth.get(User.self)?.id else {
throw Abort(.badRequest)
}

guard let user = try await userRepository.user(for: userID) else {
throw Abort(.notFound)
}

return dataMapper.userInfo(from: user)
}
}

extension UserInfoDTO: Content { }
5 changes: 5 additions & 0 deletions Sources/Grodt/DTOs/DTOMappers/UserDTOMapper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class UserDTOMapper {
func userInfo(from user: User) -> UserInfoDTO {
return UserInfoDTO(name: user.name, email: user.email)
}
}
6 changes: 6 additions & 0 deletions Sources/Grodt/DTOs/UserInfoDTO.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation

struct UserInfoDTO: Codable, Equatable {
let name: String
let email: String
}