WIP: Exploring different routes.
This commit is contained in:
@@ -34,7 +34,6 @@ public struct Room: Codable, Equatable, Identifiable, Sendable {
|
|||||||
|
|
||||||
extension Room {
|
extension Room {
|
||||||
|
|
||||||
// TODO: Maybe remove project ID, and make dependencies that retrieves current project id??
|
|
||||||
public struct Create: Codable, Equatable, Sendable {
|
public struct Create: Codable, Equatable, Sendable {
|
||||||
public let projectID: Project.ID
|
public let projectID: Project.ID
|
||||||
public let name: String
|
public let name: String
|
||||||
@@ -55,6 +54,38 @@ extension Room {
|
|||||||
self.coolingLoad = coolingLoad
|
self.coolingLoad = coolingLoad
|
||||||
self.registerCount = registerCount
|
self.registerCount = registerCount
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public init(
|
||||||
|
form: Room.Form,
|
||||||
|
projectID: Project.ID
|
||||||
|
) {
|
||||||
|
self.init(
|
||||||
|
projectID: projectID,
|
||||||
|
name: form.name,
|
||||||
|
heatingLoad: form.heatingLoad,
|
||||||
|
coolingLoad: form.coolingLoad,
|
||||||
|
registerCount: form.registerCount
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct Form: Codable, Equatable, Sendable {
|
||||||
|
public let name: String
|
||||||
|
public let heatingLoad: Double
|
||||||
|
public let coolingLoad: Double
|
||||||
|
public let registerCount: Int
|
||||||
|
|
||||||
|
public init(
|
||||||
|
name: String,
|
||||||
|
heatingLoad: Double,
|
||||||
|
coolingLoad: Double,
|
||||||
|
registerCount: Int
|
||||||
|
) {
|
||||||
|
self.name = name
|
||||||
|
self.heatingLoad = heatingLoad
|
||||||
|
self.coolingLoad = coolingLoad
|
||||||
|
self.registerCount = registerCount
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -106,41 +106,42 @@ extension SiteRoute.View {
|
|||||||
public enum RoomRoute: Equatable, Sendable {
|
public enum RoomRoute: Equatable, Sendable {
|
||||||
case form(Project.ID, dismiss: Bool = false)
|
case form(Project.ID, dismiss: Bool = false)
|
||||||
case index(Project.ID)
|
case index(Project.ID)
|
||||||
case submit(Room.Create)
|
case submit(Project.ID, Room.Form)
|
||||||
|
|
||||||
static let rootPath = "rooms"
|
static let rootPath = Path {
|
||||||
|
ProjectRoute.rootPath
|
||||||
|
Project.ID.parser()
|
||||||
|
"rooms"
|
||||||
|
}
|
||||||
|
|
||||||
public static let router = OneOf {
|
public static let router = OneOf {
|
||||||
Route(.case(Self.form)) {
|
Route(.case(Self.form)) {
|
||||||
Path {
|
Path {
|
||||||
rootPath
|
ProjectRoute.rootPath
|
||||||
|
Project.ID.parser()
|
||||||
|
"rooms"
|
||||||
"create"
|
"create"
|
||||||
}
|
}
|
||||||
Method.get
|
Method.get
|
||||||
Query {
|
Query {
|
||||||
Field("projectID") { Project.ID.parser() }
|
|
||||||
Field("dismiss", default: false) { Bool.parser() }
|
Field("dismiss", default: false) { Bool.parser() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Route(.case(Self.index)) {
|
Route(.case(Self.index)) {
|
||||||
Path { rootPath }
|
rootPath
|
||||||
Method.get
|
Method.get
|
||||||
Query {
|
|
||||||
Field("projectID") { Project.ID.parser() }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Route(.case(Self.submit)) {
|
Route(.case(Self.submit)) {
|
||||||
Path { rootPath }
|
rootPath
|
||||||
Method.post
|
Method.post
|
||||||
Body {
|
Body {
|
||||||
FormData {
|
FormData {
|
||||||
Field("projectID") { Project.ID.parser() }
|
|
||||||
Field("name", .string)
|
Field("name", .string)
|
||||||
Field("heatingLoad") { Double.parser() }
|
Field("heatingLoad") { Double.parser() }
|
||||||
Field("coolingLoad") { Double.parser() }
|
Field("coolingLoad") { Double.parser() }
|
||||||
Field("registerCount") { Digits() }
|
Field("registerCount") { Digits() }
|
||||||
}
|
}
|
||||||
.map(.memberwise(Room.Create.init))
|
.map(.memberwise(Room.Form.init))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -132,13 +132,13 @@ extension SiteRoute.View.RoomRoute {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case .submit(let form):
|
case .submit(let projectID, let form):
|
||||||
request.logger.debug("New room form submitted.")
|
request.logger.debug("New room form submitted.")
|
||||||
let _ = try await database.rooms.create(form)
|
let _ = try await database.rooms.create(.init(form: form, projectID: projectID))
|
||||||
let rooms = try await database.rooms.fetch(form.projectID)
|
let rooms = try await database.rooms.fetch(projectID)
|
||||||
return request.view {
|
return request.view {
|
||||||
ProjectView(projectID: form.projectID, activeTab: .rooms) {
|
ProjectView(projectID: projectID, activeTab: .rooms) {
|
||||||
RoomsView(projectID: form.projectID, rooms: rooms)
|
RoomsView(projectID: projectID, rooms: rooms)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,9 +19,6 @@ struct RoomForm: HTML, Sendable {
|
|||||||
.method(.post),
|
.method(.post),
|
||||||
.action(route: .room(.index(projectID)))
|
.action(route: .room(.index(projectID)))
|
||||||
) {
|
) {
|
||||||
div(.class("hidden")) {
|
|
||||||
input(.name("projectID"), .id("projectID"), .value("\(projectID)"))
|
|
||||||
}
|
|
||||||
div {
|
div {
|
||||||
label(.for("name")) { "Name:" }
|
label(.for("name")) { "Name:" }
|
||||||
Input(id: "name", placeholder: "Room Name")
|
Input(id: "name", placeholder: "Room Name")
|
||||||
|
|||||||
Reference in New Issue
Block a user