Skip to content

ZewoGraveyard/Resource

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Resource

Swift Zewo Platform License Slack Travis Codebeat

Resource provides RESTful resources for Zewo's Router.

Usage

import Zewo

public struct Todo {
    public let id: String?
    public let title: String
    public let done: Bool

    public init(id: String?, title: String, done: Bool) {
        self.id = id
        self.title = title
        self.done = done
    }
}

extension Todo: ContentMappable {
    public init(mapper: Mapper) throws {
        self.id = mapper.map(optionalFrom: "id")
        self.title = try mapper.map(from: "title")
        self.done = try mapper.map(from: "done")
    }
}

extension Todo: StructuredDataRepresentable {
    public var structuredData: StructuredData {
        return [
            "id": id.map({StructuredData.from($0)}) ?? nil,
            "title": StructuredData.from(title),
            "done": StructuredData.from(done)
        ]
    }
}

let todoResources = Resource(mediaTypes: [JSONMediaType(), URLEncodedFormMediaType()]) { todo in
    // GET /todos
    todo.index { request in
        let todos = try app.getAllTodos()
        return Response(content: ["todos": todos.content])
    }

	// POST /todos
    todo.create(content: Todo.self) { request, todo in
        let newTodo = try app.createTodo(title: todo.title, done: todo.done)
        return Response(content: newTodo)
    }

	// GET /todos/:id
    todo.show { request, id in
        let todo = try app.getTodo(id: id)
        return Response(content: todo)
    }

	// PUT /todos/:id
    todo.update(content: Todo.self) { request, id, todo in
        let newTodo = try app.updateTodo(id: id, title: todo.title, done: todo.done)
        return Response(content: newTodo)
    }

	// DELETE /todos/:id
    todo.destroy { request, id in
        try app.removeTodo(id: id)
        return Response(status: .noContent)
    }
}

let app = Router { route in
    route.resources("/todos", resources: todoResources)
}

try Server(app).start()

Installation

  • Add Resource to your Package.swift
import PackageDescription

let package = Package(
	dependencies: [
		.Package(url: "https://github.com/Zewo/Resource.git", majorVersion: 0, minor: 5),
	]
)

Support

If you need any help you can join our Slack and go to the #help channel. Or you can create a Github issue in our main repository. When stating your issue be sure to add enough details, specify what module is causing the problem and reproduction steps.

Community

Slack

The entire Zewo code base is licensed under MIT. By contributing to Zewo you are contributing to an open and engaged community of brilliant Swift programmers. Join us on Slack to get to know us!

License

This project is released under the MIT license. See LICENSE for details.