feat: Adds sensible heat ratio for projects, adds initial view / forms to the rooms tab.

This commit is contained in:
2026-01-06 12:19:14 -05:00
parent 5fcc5b88fa
commit 8fb313fddc
12 changed files with 165 additions and 100 deletions

View File

@@ -10,6 +10,7 @@ extension DatabaseClient {
public var create: @Sendable (User.ID, Project.Create) async throws -> Project
public var delete: @Sendable (Project.ID) async throws -> Void
public var get: @Sendable (Project.ID) async throws -> Project?
public var getSensibleHeatRatio: @Sendable (Project.ID) async throws -> Double?
public var fetch: @Sendable (User.ID, PageRequest) async throws -> Page<Project>
public var update: @Sendable (Project.Update) async throws -> Project
}
@@ -34,6 +35,12 @@ extension DatabaseClient.Projects: TestDependencyKey {
get: { id in
try await ProjectModel.find(id, on: database).map { try $0.toDTO() }
},
getSensibleHeatRatio: { id in
guard let model = try await ProjectModel.find(id, on: database) else {
throw NotFoundError()
}
return model.sensibleHeatRatio
},
fetch: { userID, request in
try await ProjectModel.query(on: database)
.sort(\.$createdAt, .descending)
@@ -86,6 +93,14 @@ extension Project.Create {
guard !zipCode.isEmpty else {
throw ValidationError("Project zipCode should not be empty.")
}
if let sensibleHeatRatio {
guard sensibleHeatRatio >= 0 else {
throw ValidationError("Project sensible heat ratio should be greater than 0.")
}
guard sensibleHeatRatio <= 1 else {
throw ValidationError("Project sensible heat ratio should be less than 1.")
}
}
}
}
@@ -117,6 +132,14 @@ extension Project.Update {
throw ValidationError("Project zipCode should not be empty.")
}
}
if let sensibleHeatRatio {
guard sensibleHeatRatio >= 0 else {
throw ValidationError("Project sensible heat ratio should be greater than 0.")
}
guard sensibleHeatRatio <= 1 else {
throw ValidationError("Project sensible heat ratio should be less than 1.")
}
}
}
}
@@ -132,6 +155,7 @@ extension Project {
.field("city", .string, .required)
.field("state", .string, .required)
.field("zipCode", .string, .required)
.field("sensibleHeatRatio", .double)
.field("createdAt", .datetime)
.field("updatedAt", .datetime)
.field("userID", .uuid, .required, .references(UserModel.schema, "id"))
@@ -168,6 +192,9 @@ final class ProjectModel: Model, @unchecked Sendable {
@Field(key: "zipCode")
var zipCode: String
@Field(key: "sensibleHeatRatio")
var sensibleHeatRatio: Double?
@Timestamp(key: "createdAt", on: .create, format: .iso8601)
var createdAt: Date?
@@ -189,6 +216,7 @@ final class ProjectModel: Model, @unchecked Sendable {
city: String,
state: String,
zipCode: String,
sensibleHeatRatio: Double? = nil,
userID: User.ID,
createdAt: Date? = nil,
updatedAt: Date? = nil
@@ -199,6 +227,7 @@ final class ProjectModel: Model, @unchecked Sendable {
self.city = city
self.state = state
self.zipCode = zipCode
self.sensibleHeatRatio = sensibleHeatRatio
$user.id = userID
self.createdAt = createdAt
self.updatedAt = updatedAt
@@ -212,6 +241,7 @@ final class ProjectModel: Model, @unchecked Sendable {
city: city,
state: state,
zipCode: zipCode,
sensibleHeatRatio: sensibleHeatRatio,
createdAt: createdAt!,
updatedAt: updatedAt!
)
@@ -239,6 +269,12 @@ final class ProjectModel: Model, @unchecked Sendable {
hasUpdates = true
self.zipCode = zipCode
}
if let sensibleHeatRatio = updates.sensibleHeatRatio,
sensibleHeatRatio != self.sensibleHeatRatio
{
hasUpdates = true
self.sensibleHeatRatio = sensibleHeatRatio
}
return hasUpdates
}
}

View File

@@ -9,6 +9,7 @@ public struct Project: Codable, Equatable, Identifiable, Sendable {
public let city: String
public let state: String
public let zipCode: String
public let sensibleHeatRatio: Double?
public let createdAt: Date
public let updatedAt: Date
@@ -19,6 +20,7 @@ public struct Project: Codable, Equatable, Identifiable, Sendable {
city: String,
state: String,
zipCode: String,
sensibleHeatRatio: Double? = nil,
createdAt: Date,
updatedAt: Date
) {
@@ -28,6 +30,7 @@ public struct Project: Codable, Equatable, Identifiable, Sendable {
self.city = city
self.state = state
self.zipCode = zipCode
self.sensibleHeatRatio = sensibleHeatRatio
self.createdAt = createdAt
self.updatedAt = updatedAt
}
@@ -42,6 +45,7 @@ extension Project {
public let city: String
public let state: String
public let zipCode: String
public let sensibleHeatRatio: Double?
public init(
name: String,
@@ -49,12 +53,14 @@ extension Project {
city: String,
state: String,
zipCode: String,
sensibleHeatRatio: Double? = nil,
) {
self.name = name
self.streetAddress = streetAddress
self.city = city
self.state = state
self.zipCode = zipCode
self.sensibleHeatRatio = sensibleHeatRatio
}
}
@@ -66,6 +72,7 @@ extension Project {
public let city: String?
public let state: String?
public let zipCode: String?
public let sensibleHeatRatio: Double?
public init(
id: Project.ID,
@@ -73,7 +80,8 @@ extension Project {
streetAddress: String? = nil,
city: String? = nil,
state: String? = nil,
zipCode: String? = nil
zipCode: String? = nil,
sensibleHeatRatio: Double? = nil
) {
self.id = id
self.name = name
@@ -81,6 +89,7 @@ extension Project {
self.city = city
self.state = state
self.zipCode = zipCode
self.sensibleHeatRatio = sensibleHeatRatio
}
}
}

View File

@@ -57,6 +57,11 @@ extension SiteRoute.View {
Field("city", .string)
Field("state", .string)
Field("zipCode", .string)
Optionally {
Field("sensibleHeatRatio", default: nil) {
Double.parser()
}
}
}
.map(.memberwise(Project.Create.init))
}
@@ -125,6 +130,11 @@ extension SiteRoute.View {
Optionally {
Field("zipCode", .string)
}
Optionally {
Field("sensibleHeatRatio", default: nil) {
Double.parser()
}
}
}
.map(.memberwise(Project.Update.init))
}
@@ -178,6 +188,7 @@ extension SiteRoute.View.ProjectRoute {
case index
case submit(Room.Create)
case update(Room.Update)
case updateSensibleHeatRatio(SHRUpdate)
static let rootPath = "rooms"
@@ -250,6 +261,27 @@ extension SiteRoute.View.ProjectRoute {
.map(.memberwise(Room.Update.init))
}
}
Route(.case(Self.updateSensibleHeatRatio)) {
Path {
rootPath
"update-shr"
}
Method.patch
Body {
FormData {
Field("projectID") { Project.ID.parser() }
Optionally {
Field("sensibleHeatRatio") { Double.parser() }
}
}
.map(.memberwise(SHRUpdate.init))
}
}
}
public struct SHRUpdate: Codable, Equatable, Sendable {
public let projectID: Project.ID
public let sensibleHeatRatio: Double?
}
}

View File

@@ -29,3 +29,9 @@ extension HTMLAttribute where Tag == HTMLTag.input {
value(double == nil ? "" : "\(double!)")
}
}
extension HTMLAttribute where Tag == HTMLTag.button {
public static func showModal(id: String) -> Self {
.on(.click, "\(id).showModal()")
}
}

View File

@@ -187,6 +187,14 @@ extension SiteRoute.View.ProjectRoute.RoomRoute {
case .update(let form):
_ = try await database.rooms.update(form)
return ProjectView(projectID: projectID, activeTab: .rooms)
case .updateSensibleHeatRatio(let form):
let _ = try await database.projects.update(
.init(id: form.projectID, sensibleHeatRatio: form.sensibleHeatRatio)
)
return request.view {
ProjectView(projectID: projectID, activeTab: .rooms)
}
}
}
}

View File

@@ -5,6 +5,8 @@ import Styleguide
// TODO: Have form hold onto equipment info model to edit.
struct EquipmentInfoForm: HTML, Sendable {
static let id = "equipmentForm"
let dismiss: Bool
let projectID: Project.ID
let equipmentInfo: EquipmentInfo?
@@ -31,7 +33,7 @@ struct EquipmentInfoForm: HTML, Sendable {
}
var body: some HTML {
ModalForm(id: "equipmentForm", dismiss: dismiss) {
ModalForm(id: Self.id, dismiss: dismiss) {
h1(.class("text-3xl font-bold pb-6 ps-2")) { "Equipment Info" }
form(
.class("space-y-4 p-4"),
@@ -64,21 +66,9 @@ struct EquipmentInfoForm: HTML, Sendable {
Input(id: "coolingCFM", placeholder: "CFM")
.attributes(.type(.number), .min("0"), .value(coolingCFM))
}
Row {
div {}
div(.class("space-x-4")) {
CancelButton()
.attributes(
.hx.get(
route: .project(
.detail(projectID, .equipment(.form(dismiss: true)))
)
),
.hx.target("#equipmentForm"),
.hx.swap(.outerHTML)
)
SubmitButton(title: "Save")
}
div {
SubmitButton(title: "Save")
.attributes(.class("btn-block"))
}
}
}

View File

@@ -15,14 +15,10 @@ struct EquipmentInfoView: HTML, Sendable {
Row {
h1(.class("text-2xl font-bold")) { "Equipment Info" }
if equipmentInfo != nil {
EditButton()
.attributes(
.hx.get(route: .project(.detail(projectID, .equipment(.form(dismiss: false))))),
.hx.target("#equipmentForm"),
.hx.swap(.outerHTML)
)
}
EditButton()
.attributes(
.on(.click, "\(EquipmentInfoForm.id).showModal()")
)
}
if let equipmentInfo {
@@ -45,10 +41,10 @@ struct EquipmentInfoView: HTML, Sendable {
}
.attributes(.class("border-b border-gray-200"))
EquipmentInfoForm(dismiss: true, projectID: projectID, equipmentInfo: nil)
} else {
EquipmentInfoForm(dismiss: false, projectID: projectID, equipmentInfo: nil)
}
EquipmentInfoForm(
dismiss: true, projectID: projectID, equipmentInfo: equipmentInfo
)
}
}
}

View File

@@ -36,7 +36,11 @@ struct ProjectView: HTML, Sendable {
}
}
case .rooms:
try await RoomsView(projectID: projectID, rooms: database.rooms.fetch(projectID))
try await RoomsView(
projectID: projectID,
rooms: database.rooms.fetch(projectID),
sensibleHeatRatio: database.projects.getSensibleHeatRatio(projectID)
)
case .effectiveLength:
try await EffectiveLengthsView(

View File

@@ -8,12 +8,14 @@ import Styleguide
// TODO: Need to hold the project ID in hidden input field.
struct RoomForm: HTML, Sendable {
static let id = "roomForm"
let dismiss: Bool
let projectID: Project.ID
let room: Room?
var body: some HTML {
ModalForm(id: "roomForm", dismiss: dismiss) {
ModalForm(id: Self.id, dismiss: dismiss) {
h1(.class("text-3xl font-bold pb-6")) { "Room" }
// TODO: Use htmx here.
form(

View File

@@ -10,6 +10,7 @@ import Styleguide
struct RoomsView: HTML, Sendable {
let projectID: Project.ID
let rooms: [Room]
let sensibleHeatRatio: Double?
var body: some HTML {
div {
@@ -20,10 +21,7 @@ struct RoomsView: HTML, Sendable {
.data("tip", value: "Add room")
) {
button(
// .hx.get(route: .project(.detail(projectID, .rooms(.form(dismiss: false))))),
// .hx.target("#roomForm"),
// .hx.swap(.outerHTML),
.on(.click, "roomForm.showModal()"),
.showModal(id: RoomForm.id),
.class("btn btn-primary w-[40px] text-2xl")
) {
"+"
@@ -32,6 +30,23 @@ struct RoomsView: HTML, Sendable {
}
.attributes(.class("pb-6"))
div(.class("border rounded-lg mb-6")) {
Row {
div(.class("space-x-6")) {
Label("Sensible Heat Ratio")
if let sensibleHeatRatio {
Number(sensibleHeatRatio)
}
}
EditButton()
.attributes(.showModal(id: SHRForm.id))
}
.attributes(.class("m-4"))
SHRForm(projectID: projectID, sensibleHeatRatio: sensibleHeatRatio)
}
div(.class("overflow-x-auto rounded-box border")) {
table(.class("table table-zebra"), .id("roomsTable")) {
thead {
@@ -114,6 +129,34 @@ struct RoomsView: HTML, Sendable {
}
}
struct SHRForm: HTML, Sendable {
static let id = "shrForm"
let projectID: Project.ID
let sensibleHeatRatio: Double?
var body: some HTML {
ModalForm(id: Self.id, dismiss: true) {
form(
.class("space-y-6"),
.hx.patch("/projects/\(projectID)/rooms/update-shr"),
.hx.target("body"),
.hx.swap(.outerHTML)
) {
input(.class("hidden"), .name("projectID"), .value("\(projectID)"))
div {
label(.for("sensibleHeatRatio")) { "Sensible Heat Ratio" }
Input(id: "sensibleHeatRatio", placeholder: "Sensible Heat Ratio")
.attributes(.min("0"), .max("1"), .step("0.01"), .value(sensibleHeatRatio))
}
div {
SubmitButton()
.attributes(.class("btn-block"))
}
}
}
}
}
}
extension Array where Element == Room {