I wrote a little app after watching your tutorial videos. It models a JSON array of dictionaries:
[
{
“__type”: “CollectionSet:#MobileServices”,
“id”: 1,
“operationSuccess”: true,
“SetName”: “Spring 2 2018”
},
{
“__type”: “CollectionSet:#MobileServices”,
“id”: 2,
“operationSuccess”: true,
“SetName”: “Summer 1 2018”
},
{
“__type”: “CollectionSet:#MobileServices”,
“id”: 3,
“operationSuccess”: true,
“SetName”: “Fall 1 2018”
}
]
The model is:
final class CollectionSet: Codable {
var id: Int?
var __type: String
var operationSuccess: Bool
var SetName: String
init(__type: String, operationSuccess: Bool, SetName: String) {
self.__type = __type
self.operationSuccess = operationSuccess
self.SetName = SetName
}
}
extension CollectionSet: SQLiteModel {}
extension CollectionSet: Content {}
extension CollectionSet: Migration {}
And the controller is:
struct CollectionSetsController: RouteCollection {
func boot(router: Router) throws {
let collectionsRoute = router.grouped(“api”, “getcollections”)
collectionsRoute.get(use: getAllHandler)
collectionsRoute.post(use: createHandler)
}
func getAllHandler(_ req: Request) throws -> Future<[CollectionSet]> {
return CollectionSet.query(on: req).all()
}
func createHandler(_ req: Request) throws -> Future<CollectionSet> {
let collectionsSet = try req.content.decode(CollectionSet.self)
return collectionsSet.save(on: req)
}
}
extension CollectionSet: Parameter {}
THIS CODE WORKS AS EXPECTED
I want to model the following JSON:
{
“getCollectionsResult”: [
{
“__type”: “CollectionSet:#MobileServices”,
“operationSuccess”: true,
“SetName”: “Spring 2 2018”
},
{
“__type”: “CollectionSet:#MobileServices”,
“operationSuccess”: true,
“SetName”: “Summer 1 2018”
}
]
}
What would the model and handlers look like to do this? I’ve tried several things that did not work that I won’t take up space with here. Thank you.