Skip to content

Commit

Permalink
#9: Add GET countriesByContinent request
Browse files Browse the repository at this point in the history
  • Loading branch information
VictoriaKostyleva committed Oct 4, 2022
1 parent aa4b26d commit 474cef7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct NodesRouteCollection: RouteCollection {
routes.post("nodesByAddress", use: postNodesByAddress)
routes.get("countries", use: getCountries)
routes.get("continents", use: getContinents)
routes.get("countriesByContinent", use: getCountriesByContinent)
}
}

Expand Down Expand Up @@ -90,4 +91,17 @@ extension NodesRouteCollection {
Encoder.encode(model: response, continuation: continuation)
})
}

func getCountriesByContinent(_ req: Request) async throws -> String {
let continentCode = req.query[String.self, at: "continent"]

guard let continent = Continent(rawValue: continentCode ?? "") else {
throw Abort(.badRequest)
}

return try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<String, Error>) in
let response = context.nodesService.countriesInContinents[continent]
Encoder.encode(model: response, continuation: continuation)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ extension NodesService: NodesServiceType {
}
}

var countriesInContinents: [Continent: [Country]] {
_countriesInContinents
}

func getCountries(completion: @escaping (Result<[Country], Error>) -> Void) {
nodesProvider.getCountries() { [weak self] result in
switch result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ protocol NodesServiceType {

// MARK: - Countries & Continents

var countriesInContinents: [Continent: [Country]] { get }
var nodesInContinentsCount: [Continent: Int] { get }
}

0 comments on commit 474cef7

Please sign in to comment.