Forked from pallavtrivedi03/FlightsDataController.swift
Created
February 11, 2022 18:16
-
-
Save EvolverSwiftUI/940593c27ebae555600ac64c42fb8379 to your computer and use it in GitHub Desktop.
FlightsDataController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import PerfectLib | |
import PerfectHTTP | |
import PerfectHTTPServer | |
class FlightsDetailController { | |
func handleFlightsDetailRequest(request: HTTPRequest, response: HTTPResponse) { | |
do { | |
guard let flightId: Int = Int(request.urlVariables["id"] ?? "") else { | |
response.setBody(string: "Id is missing") | |
.completed(status: .badRequest) | |
return | |
} | |
let flightDetails = getFlightsDetailData().filter { details in | |
if let id = details["id"] as? Int { | |
return id == flightId | |
} | |
return false | |
} | |
try response.setBody(json: flightDetails) | |
.setHeader(.contentType, value: "application/json") | |
.completed(status: .ok) | |
} catch { | |
response.setBody(string: "Something went wrong") | |
.completed(status: .internalServerError) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment