-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JsonApiResourceModel and refactor some json classes
- Loading branch information
Koray Koska
committed
May 1, 2017
1 parent
8c13be3
commit 8e47395
Showing
7 changed files
with
130 additions
and
51 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
Sources/VaporJsonApi/Resources/JsonApiResourceController.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// JsonApiResourceController.swift | ||
// VaporJsonApi | ||
// | ||
// Created by Koray Koska on 30/04/2017. | ||
// | ||
// | ||
|
||
import Vapor | ||
import HTTP | ||
|
||
public protocol JsonApiResourceController { | ||
associatedtype Resource: JsonApiResourceModel | ||
|
||
func getResources(_ req: Request) throws -> ResponseRepresentable | ||
} | ||
|
||
public extension JsonApiResourceController { | ||
|
||
/** | ||
* The `getResources` method is responsible for get requests to the resource collection. | ||
* | ||
* Example: `/articles` for the article resource. | ||
* | ||
* - parameter req: The `Request` which fired this method. | ||
*/ | ||
func getResources(_ req: Request) throws -> ResponseRepresentable { | ||
|
||
guard req.fulfillsJsonApiAcceptResponsibilities() else { | ||
throw JsonApiNotAcceptableError(mediaType: req.acceptHeaderValue() ?? "*No Accept header*") | ||
} | ||
|
||
let query = req.jsonApiQuery() | ||
|
||
let pageCount = query["page"]?["size"]?.string?.int ?? JsonApiConfig.defaultPageSize | ||
if pageCount > JsonApiConfig.maximumPageSize { | ||
throw JsonApiInvalidPageValueError(page: "page[size]", value: query["page"]?["size"]?.string ?? "*Nothing*") | ||
} | ||
let pageNumber = query["page"]?["number"]?.string?.int ?? 1 | ||
if pageNumber < 1 { | ||
throw JsonApiInvalidPageValueError(page: "page[number]", value: query["page"]?["number"]?.string ?? "*Nothing*") | ||
} | ||
let resources = try Resource.query().limit(pageCount, withOffset: (pageNumber * pageCount) - pageCount).all() | ||
|
||
return "Hello" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// JsonApiResourceModel.swift | ||
// VaporJsonApi | ||
// | ||
// Created by Koray Koska on 01/05/2017. | ||
// | ||
// | ||
|
||
import Vapor | ||
import HTTP | ||
|
||
public protocol JsonApiResourceModel: Model { | ||
|
||
typealias JsonApiAttributes = [String: JSON?] | ||
typealias JsonApiRelationships = [String: JsonApiResourceModel.Type] | ||
|
||
var resourceType: JsonApiResourceType { get } | ||
|
||
func attributes() throws -> JsonApiAttributes | ||
|
||
func relationships() throws -> JsonApiRelationships | ||
} | ||
|
||
public extension JsonApiResourceModel { | ||
|
||
public func makeResourceObject() throws -> JsonApiResourceObject { | ||
guard let id = self.id?.string else { | ||
throw JsonApiInternalServerError(title: "Internal Server Error", detail: "A fetched model does not seem to have a valid id.") | ||
} | ||
let attributes = JsonApiAttributesObject(attributes: try JSON(node: self.attributes())) | ||
/* | ||
let relationships = JsonApiRelationshipsObject(relationshipObjects: []) | ||
let resourceObject = JsonApiResourceObject(id: id, type: resourceType, attributes: attributes, relationships: relationships, links: links, meta: meta) | ||
*/ | ||
|
||
// TODO: Finish implementing makeResourceObject | ||
throw JsonApiInternalServerError() | ||
} | ||
} |
This file contains 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
This file contains 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