feat working on vendor views.
This commit is contained in:
@@ -31,7 +31,7 @@ struct VendorApiController: RouteCollection {
|
|||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
func update(req: Request) async throws -> Vendor {
|
func update(req: Request) async throws -> Vendor {
|
||||||
return try await vendors.update(req.ensureIDPathComponent(), req.content.decode(Vendor.Update.self))
|
return try await vendors.update(req.ensureIDPathComponent(), with: req.content.decode(Vendor.Update.self))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Sendable
|
@Sendable
|
||||||
|
|||||||
@@ -1,108 +1,86 @@
|
|||||||
// import Fluent
|
import DatabaseClient
|
||||||
// import Vapor
|
import Dependencies
|
||||||
//
|
import Elementary
|
||||||
// struct VendorViewController: RouteCollection {
|
import SharedModels
|
||||||
// private let api = VendorApiController()
|
import Vapor
|
||||||
//
|
import VaporElementary
|
||||||
// func boot(routes: any RoutesBuilder) throws {
|
|
||||||
// let vendors = routes.protected.grouped("vendors")
|
struct VendorViewController: RouteCollection {
|
||||||
//
|
|
||||||
// vendors.get(use: index(req:))
|
@Dependency(\.database.vendors) var vendors
|
||||||
// vendors.post(use: create(req:))
|
@Dependency(\.database.vendorBranches) var vendorBranches
|
||||||
// vendors.group(":vendorID") {
|
|
||||||
// $0.delete(use: delete(req:))
|
func boot(routes: any RoutesBuilder) throws {
|
||||||
// $0.put(use: update(req:))
|
let route = routes.grouped("vendors")
|
||||||
// }
|
route.get(use: index)
|
||||||
// }
|
route.post(use: create)
|
||||||
//
|
route.get("create", use: form)
|
||||||
// @Sendable
|
route.group(":id") {
|
||||||
// func index(req: Request) async throws -> View {
|
$0.get(use: detail)
|
||||||
// return try await req.view.render("vendors/index", makeCtx(req: req))
|
$0.put(use: update)
|
||||||
// }
|
$0.post("branches", use: createBranch(req:))
|
||||||
//
|
}
|
||||||
// @Sendable
|
}
|
||||||
// func create(req: Request) async throws -> View {
|
|
||||||
// let ctx = try req.content.decode(CreateVendorCTX.self)
|
@Sendable
|
||||||
// req.logger.debug("CTX: \(ctx)")
|
func index(req: Request) async throws -> HTMLResponse {
|
||||||
// let vendor = Vendor.Create(name: ctx.name).toModel()
|
try await req.render {
|
||||||
// try await vendor.save(on: req.db)
|
try await mainPage(VendorForm())
|
||||||
//
|
}
|
||||||
// if let branchString = ctx.branches {
|
}
|
||||||
// let branches = branchString.split(separator: ",")
|
|
||||||
// .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
|
@Sendable
|
||||||
//
|
func create(req: Request) async throws -> HTMLResponse {
|
||||||
// for branch in branches {
|
let vendor = try await vendors.create(req.content.decode(Vendor.Create.self))
|
||||||
// try await vendor.$branches.create(
|
return await req.render { VendorDetail(vendor: vendor) }
|
||||||
// VendorBranch(name: String(branch), vendorId: vendor.requireID()),
|
}
|
||||||
// on: req.db
|
|
||||||
// )
|
@Sendable
|
||||||
// }
|
func form(req: Request) async throws -> HTMLResponse {
|
||||||
// }
|
await req.render { VendorForm(.float(shouldShow: true)) }
|
||||||
//
|
}
|
||||||
// return try await req.view.render("vendors/table", makeCtx(req: req))
|
|
||||||
// }
|
@Sendable
|
||||||
//
|
func detail(req: Request) async throws -> HTMLResponse {
|
||||||
// @Sendable
|
guard let vendor = try await vendors.get(req.ensureIDPathComponent(), .withBranches) else {
|
||||||
// func delete(req: Request) async throws -> HTTPStatus {
|
throw Abort(.badRequest, reason: "Vendor does not exist.")
|
||||||
// try await api.delete(req: req)
|
}
|
||||||
// }
|
let html = VendorDetail(vendor: vendor)
|
||||||
//
|
guard req.isHtmxRequest else {
|
||||||
// @Sendable
|
return try await req.render { try await mainPage(html) }
|
||||||
// func update(req: Request) async throws -> View {
|
}
|
||||||
// _ = try await api.update(req: req)
|
return await req.render { html }
|
||||||
// return try await req.view.render("vendors/table", makeCtx(req: req, oob: true))
|
}
|
||||||
// }
|
|
||||||
//
|
@Sendable
|
||||||
// private func makeCtx(req: Request, vendor: Vendor? = nil, oob: Bool = false) async throws -> VendorsCTX {
|
func update(req: Request) async throws -> HTMLResponse {
|
||||||
// let vendors = try await Vendor.query(on: req.db)
|
let vendor = try await vendors.update(
|
||||||
// .with(\.$branches)
|
req.ensureIDPathComponent(),
|
||||||
// .sort(\.$name, .ascending)
|
with: req.content.decode(Vendor.Update.self),
|
||||||
// .all()
|
returnWithBranches: true
|
||||||
// .map { $0.toDTO() }
|
)
|
||||||
//
|
return await req.render { VendorDetail(vendor: vendor) }
|
||||||
// return .init(
|
}
|
||||||
// vendors: vendors,
|
|
||||||
// form: .init(vendor: vendor, oob: oob)
|
@Sendable
|
||||||
// )
|
func createBranch(req: Request) async throws -> HTMLResponse {
|
||||||
// }
|
let vendorID = try req.ensureIDPathComponent()
|
||||||
// }
|
let create = try req.content.decode(CreateBranch.self)
|
||||||
//
|
let branch = try await vendorBranches.create(.init(name: create.name, vendorID: vendorID))
|
||||||
// struct VendorFormCTX: Content {
|
return await req.render { VendorDetail.BranchTable.Row(branch: branch) }
|
||||||
// let htmxForm: HtmxFormCTX<Context>
|
}
|
||||||
//
|
|
||||||
// init(vendor: Vendor? = nil, oob: Bool = false) {
|
private func mainPage<C: HTML>(_ html: C) async throws -> some SendableHTMLDocument where C: Sendable {
|
||||||
// self.htmxForm = .init(
|
let vendors = try await vendors.fetchAll(.withBranches)
|
||||||
// formClass: "vendor-form",
|
return MainPage(displayNav: true, route: .vendors) {
|
||||||
// formId: "vendor-form",
|
div(.class("container")) {
|
||||||
// htmxTargetUrl: vendor == nil ? .post("/vendors") : .put("/vendors"),
|
html
|
||||||
// htmxTarget: "#vendor-table",
|
VendorTable(vendors: vendors)
|
||||||
// htmxPushUrl: false,
|
}
|
||||||
// htmxResetAfterRequest: true,
|
}
|
||||||
// htmxSwapOob: oob ? .outerHTML : nil,
|
}
|
||||||
// htmxSwap: oob ? nil : .outerHTML,
|
}
|
||||||
// context: .init(vendor: vendor)
|
|
||||||
// )
|
struct CreateBranch: Content {
|
||||||
// }
|
let name: String
|
||||||
//
|
}
|
||||||
// struct Context: Content {
|
|
||||||
// let vendor: Vendor?
|
|
||||||
// let branches: String?
|
|
||||||
// let buttonLabel: String
|
|
||||||
//
|
|
||||||
// init(vendor: Vendor? = nil) {
|
|
||||||
// self.vendor = vendor
|
|
||||||
// self.branches = vendor?.branches.map(\.name).joined(separator: ", ")
|
|
||||||
// self.buttonLabel = vendor == nil ? "Create" : "Update"
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private struct VendorsCTX: Content {
|
|
||||||
// let vendors: [Vendor.DTO]
|
|
||||||
// let form: VendorFormCTX
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private struct CreateVendorCTX: Content {
|
|
||||||
// let name: String
|
|
||||||
// let branches: String?
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -3,12 +3,65 @@ import ElementaryHTMX
|
|||||||
import SharedModels
|
import SharedModels
|
||||||
|
|
||||||
struct VendorDetail: HTML {
|
struct VendorDetail: HTML {
|
||||||
let vendor: Vendor?
|
|
||||||
|
let vendor: Vendor
|
||||||
|
|
||||||
var content: some HTML {
|
var content: some HTML {
|
||||||
div(.class("container")) {
|
Float(shouldDisplay: true) {
|
||||||
VendorForm(vendor: vendor)
|
VendorForm(.formOnly(vendor))
|
||||||
// TODO: Branch table + form.
|
BranchTable(branches: vendor.branches ?? [])
|
||||||
|
div(.class("row btn-row")) {
|
||||||
|
button(.class("btn-done")) { "Done" }
|
||||||
|
button(.class("danger")) { "Delete" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct BranchTable: HTML {
|
||||||
|
let branches: [VendorBranch]
|
||||||
|
|
||||||
|
var content: some HTML {
|
||||||
|
div {
|
||||||
|
form(
|
||||||
|
.id("branch-form"),
|
||||||
|
.custom(name: "hx-on::after-request", value: "if(event.detail.successful) this.reset();")
|
||||||
|
) {
|
||||||
|
div(.class("row"), .style("margin: 0;")) {
|
||||||
|
input(.type(.text), .class("col-10"), .placeholder("Branch Name"), .required)
|
||||||
|
button(
|
||||||
|
.type(.submit),
|
||||||
|
.class("btn-secondary"),
|
||||||
|
.style("float: right; padding: 10px 50px;"),
|
||||||
|
.hx.target("#branch-table"),
|
||||||
|
.hx.swap(.beforeEnd)
|
||||||
|
) { "+" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
table {
|
||||||
|
thead {
|
||||||
|
tr {
|
||||||
|
th(.class("label col-11")) { "Branch Location" }
|
||||||
|
th(.class("col-1")) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tbody(.id("branch-table")) {
|
||||||
|
for branch in branches {
|
||||||
|
Row(branch: branch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Row: HTML {
|
||||||
|
let branch: VendorBranch
|
||||||
|
|
||||||
|
var content: some HTML<HTMLTag.tr> {
|
||||||
|
tr {
|
||||||
|
td(.class("col-11")) { branch.name }
|
||||||
|
td(.class("col-1")) { Button.danger { "Delete" } }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,31 +3,70 @@ import ElementaryHTMX
|
|||||||
import SharedModels
|
import SharedModels
|
||||||
|
|
||||||
struct VendorForm: HTML {
|
struct VendorForm: HTML {
|
||||||
let vendor: Vendor?
|
|
||||||
|
|
||||||
var content: some HTML<HTMLTag.form> {
|
let context: Context
|
||||||
|
var vendor: Vendor? { context.vendor }
|
||||||
|
|
||||||
|
init(
|
||||||
|
_ context: Context
|
||||||
|
) {
|
||||||
|
self.context = context
|
||||||
|
}
|
||||||
|
|
||||||
|
init() { self.init(.float(nil)) }
|
||||||
|
|
||||||
|
enum Context {
|
||||||
|
case float(Vendor? = nil, shouldShow: Bool = false)
|
||||||
|
case formOnly(Vendor)
|
||||||
|
|
||||||
|
var vendor: Vendor? {
|
||||||
|
switch self {
|
||||||
|
case let .float(vendor, _): return vendor
|
||||||
|
case let .formOnly(vendor): return vendor
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var content: some HTML {
|
||||||
|
switch context {
|
||||||
|
case let .float(vendor, shouldDisplay):
|
||||||
|
Float(shouldDisplay: shouldDisplay) {
|
||||||
|
makeForm(vendor: vendor)
|
||||||
|
}
|
||||||
|
case let .formOnly(vendor):
|
||||||
|
makeForm(vendor: vendor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeForm(vendor: Vendor?) -> some HTML {
|
||||||
form(
|
form(
|
||||||
.id("vendor-form"),
|
.id("vendor-form"),
|
||||||
vendor != nil ? .hx.put(targetURL) : .hx.post(targetURL),
|
vendor != nil ? .hx.put(targetURL) : .hx.post(targetURL),
|
||||||
.hx.target("this"),
|
.hx.target("#float"),
|
||||||
.hx.swap(.outerHTML)
|
.hx.swap(.outerHTML)
|
||||||
) {
|
) {
|
||||||
div(.class("row")) {
|
div(.class("row")) {
|
||||||
input(
|
input(
|
||||||
|
.type(.text),
|
||||||
|
.class("col-9"),
|
||||||
.id("vendor-name"),
|
.id("vendor-name"),
|
||||||
.name("name"),
|
.name("name"),
|
||||||
.value(vendor?.name ?? ""),
|
.value(vendor?.name ?? ""),
|
||||||
.placeholder("Vendor Name"),
|
.placeholder("Vendor Name"),
|
||||||
.required
|
.required
|
||||||
)
|
)
|
||||||
button(.type(.submit), .class("btn-primary")) { buttonLabel }
|
button(
|
||||||
|
.type(.submit),
|
||||||
|
.class("col-1 btn-primary"),
|
||||||
|
.style("float: right")
|
||||||
|
) { buttonLabel }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var buttonLabel: String {
|
private var buttonLabel: String {
|
||||||
guard vendor != nil else { return "Update" }
|
guard vendor != nil else { return "Create" }
|
||||||
return "Create"
|
return "Update"
|
||||||
}
|
}
|
||||||
|
|
||||||
var targetURL: String {
|
var targetURL: String {
|
||||||
|
|||||||
@@ -8,9 +8,19 @@ struct VendorTable: HTML {
|
|||||||
var content: some HTML {
|
var content: some HTML {
|
||||||
table {
|
table {
|
||||||
thead {
|
thead {
|
||||||
|
tr {
|
||||||
th { "Name" }
|
th { "Name" }
|
||||||
th {}
|
th { "Branches" }
|
||||||
th { Button.add() }
|
th(.style("width: 100px;")) {
|
||||||
|
Button.add()
|
||||||
|
.attributes(
|
||||||
|
.style("padding: 0px 10px;"),
|
||||||
|
.hx.get("/vendors/create"),
|
||||||
|
.hx.target("#float"),
|
||||||
|
.hx.swap(.outerHTML)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
tbody(.id("vendor-table")) {
|
tbody(.id("vendor-table")) {
|
||||||
for vendor in vendors {
|
for vendor in vendors {
|
||||||
@@ -27,7 +37,16 @@ struct VendorTable: HTML {
|
|||||||
tr(.id("vendor_\(vendor.id)")) {
|
tr(.id("vendor_\(vendor.id)")) {
|
||||||
td { vendor.name.capitalized }
|
td { vendor.name.capitalized }
|
||||||
td { "(\(vendor.branches?.count ?? 0)) Branches" }
|
td { "(\(vendor.branches?.count ?? 0)) Branches" }
|
||||||
td {}
|
td {
|
||||||
|
Button.detail()
|
||||||
|
.attributes(
|
||||||
|
.style("padding-left: 15px;"),
|
||||||
|
.hx.get("/vendors/\(vendor.id)"),
|
||||||
|
.hx.target("#float"),
|
||||||
|
.hx.pushURL(true),
|
||||||
|
.hx.swap(.outerHTML)
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,9 +8,10 @@ import VaporElementary
|
|||||||
func routes(_ app: Application) throws {
|
func routes(_ app: Application) throws {
|
||||||
try app.register(collection: ApiController())
|
try app.register(collection: ApiController())
|
||||||
try app.register(collection: UserViewController())
|
try app.register(collection: UserViewController())
|
||||||
|
try app.register(collection: VendorViewController())
|
||||||
// try app.register(collection: ViewController())
|
// try app.register(collection: ViewController())
|
||||||
|
|
||||||
app.get("test") { _ in
|
app.get { _ in
|
||||||
HTMLResponse {
|
HTMLResponse {
|
||||||
MainPage(displayNav: false, route: .purchaseOrders) {
|
MainPage(displayNav: false, route: .purchaseOrders) {
|
||||||
div(.class("container")) {
|
div(.class("container")) {
|
||||||
|
|||||||
@@ -9,25 +9,35 @@ public extension DatabaseClient {
|
|||||||
public var create: @Sendable (Vendor.Create) async throws -> Vendor
|
public var create: @Sendable (Vendor.Create) async throws -> Vendor
|
||||||
public var delete: @Sendable (Vendor.ID) async throws -> Void
|
public var delete: @Sendable (Vendor.ID) async throws -> Void
|
||||||
public var fetchAll: @Sendable (FetchRequest) async throws -> [Vendor]
|
public var fetchAll: @Sendable (FetchRequest) async throws -> [Vendor]
|
||||||
public var get: @Sendable (Vendor.ID, GetRequest) async throws -> Vendor?
|
public var get: @Sendable (Vendor.ID, GetRequest?) async throws -> Vendor?
|
||||||
public var update: @Sendable (Vendor.ID, Vendor.Update) async throws -> Vendor
|
public var update: @Sendable (Vendor.ID, Vendor.Update, GetRequest?) async throws -> Vendor
|
||||||
|
|
||||||
public enum FetchRequest {
|
public enum FetchRequest: Sendable {
|
||||||
case all
|
case all
|
||||||
case withBranches
|
case withBranches
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum GetRequest {
|
public enum GetRequest: Sendable {
|
||||||
case all
|
|
||||||
case withBranches
|
case withBranches
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Sendable
|
||||||
public func fetchAll() async throws -> [Vendor] {
|
public func fetchAll() async throws -> [Vendor] {
|
||||||
try await fetchAll(.all)
|
try await fetchAll(.all)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Sendable
|
||||||
public func get(_ id: Vendor.ID) async throws -> Vendor? {
|
public func get(_ id: Vendor.ID) async throws -> Vendor? {
|
||||||
try await get(id, .all)
|
try await get(id, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Sendable
|
||||||
|
public func update(
|
||||||
|
_ id: Vendor.ID,
|
||||||
|
with updates: Vendor.Update,
|
||||||
|
returnWithBranches: Bool = false
|
||||||
|
) async throws -> Vendor {
|
||||||
|
try await update(id, updates, returnWithBranches ? GetRequest.withBranches : nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,14 +37,21 @@ public extension DatabaseClient.Vendors {
|
|||||||
query = query.with(\.$branches)
|
query = query.with(\.$branches)
|
||||||
}
|
}
|
||||||
return try await query.first().map { try $0.toDTO(includeBranches: withBranches) }
|
return try await query.first().map { try $0.toDTO(includeBranches: withBranches) }
|
||||||
} update: { id, updates in
|
} update: { id, updates, withBranches in
|
||||||
guard let model = try await VendorModel.find(id, on: db) else {
|
guard let model = try await VendorModel.find(id, on: db) else {
|
||||||
throw NotFoundError()
|
throw NotFoundError()
|
||||||
}
|
}
|
||||||
try model.applyUpdates(updates)
|
try model.applyUpdates(updates)
|
||||||
try await model.save(on: db)
|
try await model.save(on: db)
|
||||||
|
if withBranches != .withBranches {
|
||||||
return try model.toDTO()
|
return try model.toDTO()
|
||||||
}
|
}
|
||||||
|
return try await VendorModel.query(on: db)
|
||||||
|
.filter(\.$id == id)
|
||||||
|
.with(\.$branches)
|
||||||
|
.first()!
|
||||||
|
.toDTO()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user