diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/CommonContext.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/CommonContext.swift index 4af33da..ddad30b 100644 --- a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/CommonContext.swift +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/CommonContext.swift @@ -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 {} diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/ContextBuilder.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/ContextBuilder.swift index f8b101a..96bf911 100644 --- a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/ContextBuilder.swift +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Common/Context/ContextBuilder.swift @@ -16,6 +16,7 @@ final class ContextBuilder { let nodesService = NodesService(nodesProvider: nodesProvider) return CommonContext( + nodesProvider: nodesProvider, nodesService: nodesService ) } diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Nodes/NodesRouteCollection.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Nodes/NodesRouteCollection.swift index 6bf4972..4abf670 100644 --- a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Nodes/NodesRouteCollection.swift +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Nodes/NodesRouteCollection.swift @@ -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) @@ -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) 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) 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) 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)) + } + } + }) } } diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/NodesService/NodesService.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/NodesService/NodesService.swift index 89e0af4..089bc99 100644 --- a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/NodesService/NodesService.swift +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/NodesService/NodesService.swift @@ -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) 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) 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) 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)) - } - } - }) - } } diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/NodesService/NodesServiceType.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/NodesService/NodesServiceType.swift index 354e87e..847c67e 100644 --- a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/NodesService/NodesServiceType.swift +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Services/NodesService/NodesServiceType.swift @@ -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 }