Skip to content

Commit

Permalink
Move logic to RouteCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
VictoriaKostyleva committed Oct 3, 2022
1 parent d45a3d9 commit 80c1d4b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@ import SOLARAPI
protocol NoContext {}

final class CommonContext {
// Providers
let nodesProvider: NodesProviderType

// Services
let nodesService: NodesServiceType

init(
nodesProvider: NodesProviderType,
nodesService: NodesServiceType
) {
self.nodesProvider = nodesProvider
self.nodesService = nodesService
}
}

protocol HasNodesProvider { var nodesProvider: NodesProviderType { get } }
extension CommonContext: HasNodesProvider {}

protocol HasNodesService { var nodesService: NodesServiceType { get } }
extension CommonContext: HasNodesService {}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ final class ContextBuilder {
let nodesService = NodesService(nodesProvider: nodesProvider)

return CommonContext(
nodesProvider: nodesProvider,
nodesService: nodesService
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
//

import Vapor
import SOLARAPI

struct NodesRouteCollection: RouteCollection {
let context: HasNodesService
let context: HasNodesProvider

func boot(routes: RoutesBuilder) throws {
routes.get("nodes", use: getNodes)
Expand All @@ -27,27 +28,56 @@ extension NodesRouteCollection {
let query = req.query[String.self, at: GetNodesRequest.CodingKeys.query.rawValue]
let page = req.query[Int.self, at: GetNodesRequest.CodingKeys.page.rawValue]

return try await context.nodesService.loadNodes(
continentCode: continentCode,
countryCode: countryCode,
minPrice: minPrice,
maxPrice: maxPrice,
orderBy: orderBy,
query: query,
page: page
)
return try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<String, Error>) in
context.nodesProvider.getNodes(
.init(
status: .active,
continentCode: continentCode,
countryCode: countryCode,
minPrice: minPrice,
maxPrice: maxPrice,
orderBy: orderBy,
query: query,
page: page
)
) { result in
switch result {
case let .success(response):
Encoder.encode(model: response, continuation: continuation)
case let .failure(error):
continuation.resume(throwing: Abort(.init(statusCode: error.code), reason: error.localizedDescription))
}
}
})
}

func postNodesByAddress(_ req: Request) async throws -> String {
let body = try req.content.decode(NodesByAddressPostBody.self)

return try await context.nodesService.getNodes(
by: body.blockchain_addresses,
page: body.page
)
return try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<String, Error>) in
context.nodesProvider.postNodesByAddress(
.init(addresses: body.blockchain_addresses, page: body.page)
) { result in
switch result {
case let .success(response):
Encoder.encode(model: response, continuation: continuation)
case let .failure(error):
continuation.resume(throwing: Abort(.init(statusCode: error.code), reason: error.localizedDescription))
}
}
})
}

func getCountries(_ req: Request) async throws -> String {
try await context.nodesService.getCountries()
try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<String, Error>) in
context.nodesProvider.getCountries() { result in
switch result {
case let .success(response):
Encoder.encode(model: response, continuation: continuation)
case let .failure(error):
continuation.resume(throwing: Abort(.init(statusCode: error.code), reason: error.localizedDescription))
}
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,64 +22,4 @@ final class NodesService {
// MARK: - NodesServiceType

extension NodesService: NodesServiceType {
func loadNodes(
continentCode: String?,
countryCode: String?,
minPrice: Int?,
maxPrice: Int?,
orderBy: OrderType?,
query: String?,
page: Int?
) async throws -> String {
try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<String, Error>) in
nodesProvider.getNodes(
.init(
status: .active,
continentCode: continentCode,
countryCode: countryCode,
minPrice: minPrice,
maxPrice: maxPrice,
orderBy: orderBy,
query: query,
page: page
)
) { result in
switch result {
case let .success(response):
Encoder.encode(model: response, continuation: continuation)
case let .failure(error):
continuation.resume(throwing: Abort(.init(statusCode: error.code), reason: error.localizedDescription))
}
}
})
}

func getNodes(
by addresses: [String],
page: Int?
) async throws -> String {
try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<String, Error>) in
nodesProvider.postNodesByAddress(.init(addresses: addresses, page: page)) { result in
switch result {
case let .success(response):
Encoder.encode(model: response, continuation: continuation)
case let .failure(error):
continuation.resume(throwing: Abort(.init(statusCode: error.code), reason: error.localizedDescription))
}
}
})
}

func getCountries() async throws -> String {
try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<String, Error>) in
nodesProvider.getCountries() { result in
switch result {
case let .success(response):
Encoder.encode(model: response, continuation: continuation)
case let .failure(error):
continuation.resume(throwing: Abort(.init(statusCode: error.code), reason: error.localizedDescription))
}
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,4 @@ import Foundation
import SOLARAPI

protocol NodesServiceType {
func loadNodes(
continentCode: String?,
countryCode: String?,
minPrice: Int?,
maxPrice: Int?,
orderBy: OrderType?,
query: String?,
page: Int?
) async throws -> String

func getNodes(
by: [String],
page: Int?
) async throws -> String

func getCountries() async throws -> String
}

0 comments on commit 80c1d4b

Please sign in to comment.