Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Commit

Permalink
Rename Crud -> CRUD, Crudable -> CRUDModel
Browse files Browse the repository at this point in the history
  • Loading branch information
simonedelmann committed Apr 1, 2020
1 parent 808cbef commit a531f36
Show file tree
Hide file tree
Showing 32 changed files with 79 additions and 79 deletions.
48 changes: 24 additions & 24 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@
"repositoryURL": "https://github.com/swift-server/async-http-client.git",
"state": {
"branch": null,
"revision": "e2636a4c24e646d3e480fc666da0c090818beb09",
"version": "1.1.0"
"revision": "037b70291941fe43de668066eb6fb802c5e181d2",
"version": "1.1.1"
}
},
{
"package": "async-kit",
"repositoryURL": "https://github.com/vapor/async-kit.git",
"state": {
"branch": null,
"revision": "094dad1142e4e272a328ab44edd39730f246fb3e",
"version": "1.0.0-rc.1"
"revision": "1f145cffe2109d0fc420bc03b31dacf9de2c7573",
"version": "1.0.0"
}
},
{
"package": "console-kit",
"repositoryURL": "https://github.com/vapor/console-kit.git",
"state": {
"branch": null,
"revision": "de99d83c9f3c298371002729cc0601e4b5d9e0c9",
"version": "4.0.0-rc.1"
"revision": "7a97a5ea7fefe61cf2c943242113125b0f396a98",
"version": "4.1.0"
}
},
{
Expand All @@ -42,8 +42,8 @@
"repositoryURL": "https://github.com/vapor/fluent-kit.git",
"state": {
"branch": null,
"revision": "dc7554d4850663d7a1c58583117e1032c397d88e",
"version": "1.0.0-rc.1.8"
"revision": "638232e98a9bcd2087c6fef22a0af178c1ba6dd1",
"version": "1.0.0-rc.1.13"
}
},
{
Expand All @@ -60,8 +60,8 @@
"repositoryURL": "https://github.com/vapor/routing-kit.git",
"state": {
"branch": null,
"revision": "dbc3eafdbe243e9a46eeb6fd7d99266bee52f89f",
"version": "4.0.0-rc.1"
"revision": "35da702471cbcc308f58d38aa8fea971fa3fb166",
"version": "4.0.0"
}
},
{
Expand Down Expand Up @@ -96,17 +96,17 @@
"repositoryURL": "https://github.com/swift-server/swift-backtrace.git",
"state": {
"branch": null,
"revision": "eaf2cef011c0c23d1701aa60b364def8015dc3c7",
"version": "1.1.1"
"revision": "f2fd8c4845a123419c348e0bc4b3839c414077d5",
"version": "1.2.0"
}
},
{
"package": "swift-crypto",
"repositoryURL": "https://github.com/apple/swift-crypto.git",
"state": {
"branch": null,
"revision": "9be4a93a76d4b80105044747b35d456de7289c87",
"version": "1.0.0"
"revision": "d67ac68d09a95443303e9d6e37b34e7ba101d5f1",
"version": "1.0.1"
}
},
{
Expand All @@ -132,8 +132,8 @@
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "16ab4d657e1ad4e77bd5f8b94af8538561643053",
"version": "2.14.0"
"revision": "a27a07719ca785bcaca019a5b9fe1814b981b4a2",
"version": "2.15.0"
}
},
{
Expand All @@ -150,35 +150,35 @@
"repositoryURL": "https://github.com/apple/swift-nio-http2.git",
"state": {
"branch": null,
"revision": "65ebab9db70623bf1b58305edaa26ee5629856ed",
"version": "1.9.1"
"revision": "82eb3fa0974b838358ee46bc6c5381e5ae5de6b9",
"version": "1.11.0"
}
},
{
"package": "swift-nio-ssl",
"repositoryURL": "https://github.com/apple/swift-nio-ssl.git",
"state": {
"branch": null,
"revision": "af46d9b58fafbb76f9b01177568d435a1b024f99",
"version": "2.6.2"
"revision": "ae213938e151964aa691f0e902462fbe06baeeb6",
"version": "2.7.1"
}
},
{
"package": "vapor",
"repositoryURL": "https://github.com/vapor/vapor.git",
"state": {
"branch": null,
"revision": "6a365261bfb86308b0e170107a9da6a68b65ff93",
"version": "4.0.0-rc.3.1"
"revision": "ad2aeccfbdd1275c0a8817a8024f0b98d65e3880",
"version": "4.0.0-rc.3.12"
}
},
{
"package": "websocket-kit",
"repositoryURL": "https://github.com/vapor/websocket-kit.git",
"state": {
"branch": null,
"revision": "aac462481bcd3039d5db9a0fbe7f4e47eefe2295",
"version": "2.0.0-rc.1"
"revision": "f46778e3f1452cc80a0a6c422cdc52a5d0258795",
"version": "2.0.0"
}
}
]
Expand Down
8 changes: 4 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
.macOS(.v10_15)
],
products: [
.library(name: "CrudKit", targets: ["CrudKit"]),
.library(name: "CRUDKit", targets: ["CRUDKit"]),
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-beta"),
Expand All @@ -16,15 +16,15 @@ let package = Package(
],
targets: [
.target(
name: "CrudKit",
name: "CRUDKit",
dependencies: [
.product(name: "Vapor", package: "vapor"),
.product(name: "Fluent", package: "fluent")
]),
.testTarget(
name: "CrudKitTests",
name: "CRUDKitTests",
dependencies: [
.target(name: "CrudKit"),
.target(name: "CRUDKit"),
.product(name: "XCTVapor", package: "vapor"),
.product(name: "FluentSQLiteDriver", package: "fluent-sqlite-driver")
]),
Expand Down
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# CrudKit for Vapor 4
# CRUDKit for Vapor 4

We all write CRUD (Create-Read-Update-Delete) routes all the time. The intention of this package is to reduce repeating code and to provide a fast start for an API.

Expand All @@ -12,17 +12,17 @@ dependencies: [
],
targets: [
.target(name: "App", dependencies: [
.product(name: "CrudKit", package: "crud-kit")
.product(name: "CRUDKit", package: "crud-kit")
])
]
```

## Basic Usage

### Conform you model to `Crudable`
### Conform you model to `CRUDModel`

```swift
import CrudKit
import CRUDKit

final class Todo: Model, Content {
@ID()
Expand All @@ -37,7 +37,7 @@ final class Todo: Model, Content {
// ...
}

extension Todo: Crudable { }
extension Todo: CRUDModel { }
```

### Registering routes in `routes.swift`
Expand All @@ -46,7 +46,7 @@ extension Todo: Crudable { }
app.crud("todos", model: Todo.self)
```

This will register basic crud routes:
This will register basic CRUD routes:

```
POST /todos # create todo
Expand All @@ -65,7 +65,7 @@ Please note! The endpoints name (e.g. "todos") will be used as name for the name
You can return a custom struct as public instance, which will be returned from all CRUD routes then.

```swift
extension Todo: Crudable {
extension Todo: CRUDModel {
struct Public: Content {
var title: String
var done: Bool
Expand All @@ -82,7 +82,7 @@ extension Todo: Crudable {
You can add specific logic while create / replace. This is especially helpful, if your create / replace request should take a subset of the models properties or if you need to do special stuff while creating / replacing.

```swift
extension Todo: Crudable {
extension Todo: CRUDModel {
struct Create: Content {
var title: String
}
Expand Down Expand Up @@ -201,16 +201,16 @@ final class Tag: Model, Content {
// ...
}

extension Todo: Crudable { }
extension Tag: Crudable { }
extension Todo: CRUDModel { }
extension Tag: CRUDModel { }

// routes.swift
app.crud("todos", model: Todo.self) { routes, parentController in
routes.crud("tags", children: Tag.self, on: parentController, via: \.$tags)
}
```

This will register crud routes for tags:
This will register CRUD routes for tags:

```
POST /todos/:todos/tags # create tag
Expand Down Expand Up @@ -240,7 +240,7 @@ final class Tag: Model, Content {
}
}

extension Tag: Crudable { }
extension Tag: CRUDModel { }
```
This requires a create payload like this:
```
Expand Down Expand Up @@ -272,7 +272,7 @@ final class Tag: Model, Content {
}
}

extension Tag: Crudable {
extension Tag: CRUDModel {
struct Create: Content {
var title: String
var todo_id: Todo.IDValue?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Vapor
import Fluent

public struct CrudChildrenController<T: Model & Crudable, ParentT: Model> where T.IDValue: LosslessStringConvertible, ParentT.IDValue: LosslessStringConvertible {
public struct CRUDChildrenController<T: Model & CRUDModel, ParentT: Model> where T.IDValue: LosslessStringConvertible, ParentT.IDValue: LosslessStringConvertible {
var idComponentKey: String
var parentIdComponentKey: String

var children: KeyPath<ParentT, ChildrenProperty<ParentT, T>>
}

extension CrudChildrenController {
extension CRUDChildrenController {
var parent: KeyPath<T, ParentProperty<T, ParentT>> {
switch ParentT()[keyPath: self.children].parentKey {
case .required(let required):
Expand Down Expand Up @@ -84,7 +84,7 @@ extension CrudChildrenController {
}
}

extension CrudChildrenController where T: Patchable {
extension CRUDChildrenController where T: Patchable {
internal func patch(req: Request) throws -> EventLoopFuture<T.Public> {
guard let id = T.getID(from: idComponentKey, on: req) else {
return req.eventLoop.future(error: Abort(.notFound))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Vapor
import Fluent

public struct CrudController<T: Model & Crudable> where T.IDValue: LosslessStringConvertible {
public struct CRUDController<T: Model & CRUDModel> where T.IDValue: LosslessStringConvertible {
var idComponentKey: String
}

extension CrudController {
extension CRUDController {
internal func indexAll(req: Request) -> EventLoopFuture<[T.Public]> {
T.query(on: req.db).all().public()
}
Expand Down Expand Up @@ -42,7 +42,7 @@ extension CrudController {
}
}

extension CrudController where T: Patchable {
extension CRUDController where T: Patchable {
internal func patch(req: Request) throws -> EventLoopFuture<T.Public> {
try T.Patch.validate(on: req)
let data = try req.content.decode(T.Patch.self)
Expand Down
4 changes: 4 additions & 0 deletions Sources/CRUDKit/Protocols/CRUDModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Vapor
import Fluent

public protocol CRUDModel: Publicable, Createable, Replaceable {}
16 changes: 8 additions & 8 deletions Sources/CrudKit/Routes.swift → Sources/CRUDKit/Routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import Vapor
import Fluent

extension RoutesBuilder {
public func crud<T: Model & Crudable>(_ endpoint: String, model: T.Type, custom: ((RoutesBuilder, CrudController<T>) -> ())? = nil) where T.IDValue: LosslessStringConvertible {
public func crud<T: Model & CRUDModel>(_ endpoint: String, model: T.Type, custom: ((RoutesBuilder, CRUDController<T>) -> ())? = nil) where T.IDValue: LosslessStringConvertible {
let modelComponent = PathComponent(stringLiteral: endpoint)
let idComponent = PathComponent(stringLiteral: ":\(endpoint)")
let routes = self.grouped(modelComponent)
let idRoutes = routes.grouped(idComponent)

let controller = CrudController<T>(idComponentKey: endpoint)
let controller = CRUDController<T>(idComponentKey: endpoint)
routes.get(use: controller.indexAll)
routes.post(use: controller.create)
idRoutes.get(use: controller.index)
Expand All @@ -18,13 +18,13 @@ extension RoutesBuilder {
custom?(idRoutes, controller)
}

public func crud<T: Model & Crudable & Patchable>(_ endpoint: String, model: T.Type, custom: ((RoutesBuilder, CrudController<T>) -> ())? = nil) where T.IDValue: LosslessStringConvertible {
public func crud<T: Model & CRUDModel & Patchable>(_ endpoint: String, model: T.Type, custom: ((RoutesBuilder, CRUDController<T>) -> ())? = nil) where T.IDValue: LosslessStringConvertible {
let modelComponent = PathComponent(stringLiteral: endpoint)
let idComponent = PathComponent(stringLiteral: ":\(endpoint)")
let routes = self.grouped(modelComponent)
let idRoutes = routes.grouped(idComponent)

let controller = CrudController<T>(idComponentKey: endpoint)
let controller = CRUDController<T>(idComponentKey: endpoint)
routes.get(use: controller.indexAll)
routes.post(use: controller.create)
idRoutes.get(use: controller.index)
Expand All @@ -37,13 +37,13 @@ extension RoutesBuilder {

// MARK: Children

public func crud<T: Model & Crudable, ParentT: Model>(_ endpoint: String, children: T.Type, on parentController: CrudController<ParentT>, via keypath: KeyPath<ParentT, ChildrenProperty<ParentT, T>>, custom: ((RoutesBuilder, CrudChildrenController<T, ParentT>) -> ())? = nil) where T.IDValue: LosslessStringConvertible {
public func crud<T: Model & CRUDModel, ParentT: Model>(_ endpoint: String, children: T.Type, on parentController: CRUDController<ParentT>, via keypath: KeyPath<ParentT, ChildrenProperty<ParentT, T>>, custom: ((RoutesBuilder, CRUDChildrenController<T, ParentT>) -> ())? = nil) where T.IDValue: LosslessStringConvertible {
let modelComponent = PathComponent(stringLiteral: endpoint)
let idComponent = PathComponent(stringLiteral: ":\(endpoint)")
let routes = self.grouped(modelComponent)
let idRoutes = routes.grouped(idComponent)

let controller = CrudChildrenController<T, ParentT>(idComponentKey: endpoint, parentIdComponentKey: parentController.idComponentKey, children: keypath)
let controller = CRUDChildrenController<T, ParentT>(idComponentKey: endpoint, parentIdComponentKey: parentController.idComponentKey, children: keypath)
routes.get(use: controller.indexAll)
routes.post(use: controller.create)
idRoutes.get(use: controller.index)
Expand All @@ -53,13 +53,13 @@ extension RoutesBuilder {
custom?(idRoutes, controller)
}

public func crud<T: Model & Crudable & Patchable, ParentT: Model>(_ endpoint: String, children: T.Type, on parentController: CrudController<ParentT>, via keypath: KeyPath<ParentT, ChildrenProperty<ParentT, T>>, custom: ((RoutesBuilder, CrudChildrenController<T, ParentT>) -> ())? = nil) where T.IDValue: LosslessStringConvertible {
public func crud<T: Model & CRUDModel & Patchable, ParentT: Model>(_ endpoint: String, children: T.Type, on parentController: CRUDController<ParentT>, via keypath: KeyPath<ParentT, ChildrenProperty<ParentT, T>>, custom: ((RoutesBuilder, CRUDChildrenController<T, ParentT>) -> ())? = nil) where T.IDValue: LosslessStringConvertible {
let modelComponent = PathComponent(stringLiteral: endpoint)
let idComponent = PathComponent(stringLiteral: ":\(endpoint)")
let routes = self.grouped(modelComponent)
let idRoutes = routes.grouped(idComponent)

let controller = CrudChildrenController<T, ParentT>(idComponentKey: endpoint, parentIdComponentKey: parentController.idComponentKey, children: keypath)
let controller = CRUDChildrenController<T, ParentT>(idComponentKey: endpoint, parentIdComponentKey: parentController.idComponentKey, children: keypath)
routes.get(use: controller.indexAll)
routes.post(use: controller.create)
idRoutes.get(use: controller.index)
Expand Down
4 changes: 0 additions & 4 deletions Sources/CrudKit/Protocols/Crudable.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@testable import CrudKit
@testable import CRUDKit
import XCTVapor

final class CreateChildrenTests: ApplicationXCTestCase {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@testable import CrudKit
@testable import CRUDKit
import XCTVapor

final class DeleteChildrenTests: ApplicationXCTestCase {
Expand Down
Loading

0 comments on commit a531f36

Please sign in to comment.