simonedelmann / crud-kit

CRUD 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.
MIT License
56 stars 8 forks source link

app.crud for longer endpoint #6

Closed alazirakaclan closed 3 years ago

alazirakaclan commented 3 years ago

I'd like to get responses on e.g. "/site/api/todos". And it seems it's not possible to do it right now, cause "app.crud(...)" accepts only one string.

I thought about something like this:

` extension RoutesBuilder { public func crud<T: Model & CRUDModel>(_ endpoints: [String], model: T.Type, custom: ((RoutesBuilder, CRUDController) -> ())? = nil) where T.IDValue: LosslessStringConvertible { let endpoint = endpoints.last! let modelComponents = endpoints.map{endpoint in PathComponent(stringLiteral: endpoint)} let idComponents = modelComponents + [PathComponent(stringLiteral: ":(endpoint)")]

    let routes = self.grouped(modelComponents)
    let idRoutes = routes.grouped(idComponents)

    let controller = CRUDController<T>(idComponentKey: endpoint)
    controller.setup(self, on: endpoint)

    custom?(idRoutes, controller)
}

} `

But I can't use it, because CRUDController is internal.

simonedelmann commented 3 years ago

Thanks for your issue! If I understood correctly, you can - and should - use Vapors route groups for this. (See here)

let api = app.grouped("site", "api")
api.crud("todos", ...)

Anyhow I can make CRUDController public if you want to extend RoutesBuilder for this. Feel free to make a PR!

simonedelmann commented 3 years ago

@alazirakaclan Can I close this issue?