This repository was archived by the owner on Nov 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWebPageApiController.swift
More file actions
55 lines (45 loc) · 1.88 KB
/
WebPageApiController.swift
File metadata and controls
55 lines (45 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// File.swift
//
//
// Created by Tibor Bodecs on 2021. 12. 06..
//
import Vapor
import FeatherCore
import Fluent
import WebObjects
extension Web.Page.List: Content {}
extension Web.Page.Detail: Content {}
struct WebPageApiController: ApiController {
typealias ApiModel = Web.Page
typealias DatabaseModel = WebPageModel
func listOutput(_ req: Request, _ models: [DatabaseModel]) async throws -> [Web.Page.List] {
models.map { model in .init(id: model.uuid, title: model.title, metadata: model.featherMetadata) }
}
func detailOutput(_ req: Request, _ model: DatabaseModel) async throws -> Web.Page.Detail {
let content = try await model.filter(model.content, req)
return .init(id: model.uuid, title: model.title, content: content, metadata: model.featherMetadata)
}
func createInput(_ req: Request, _ model: DatabaseModel, _ input: Web.Page.Create) async throws {
model.title = input.title
model.content = input.content
}
func updateInput(_ req: Request, _ model: DatabaseModel, _ input: Web.Page.Update) async throws {
model.title = input.title
model.content = input.content
}
func patchInput(_ req: Request, _ model: DatabaseModel, _ input: Web.Page.Patch) async throws {
model.title = input.title ?? model.title
model.content = input.content ?? model.content
}
@AsyncValidatorBuilder
func validators(optional: Bool) -> [AsyncValidator] {
KeyedContentValidator<String>.required("title", optional: optional)
KeyedContentValidator<String>("title", "Title must be unique", optional: optional) { req, value in
guard ApiModel.getIdParameter(req) == nil else {
return true
}
return try await req.web.page.repository.isUnique(\.$title == value, Web.Page.getIdParameter(req))
}
}
}