feat: Updates form routes and database routes to use id's in the url path.
This commit is contained in:
@@ -12,6 +12,9 @@ extension DatabaseClient {
|
||||
public var delete: @Sendable (ComponentPressureLoss.ID) async throws -> Void
|
||||
public var fetch: @Sendable (Project.ID) async throws -> [ComponentPressureLoss]
|
||||
public var get: @Sendable (ComponentPressureLoss.ID) async throws -> ComponentPressureLoss?
|
||||
public var update:
|
||||
@Sendable (ComponentPressureLoss.ID, ComponentPressureLoss.Update) async throws ->
|
||||
ComponentPressureLoss
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +46,17 @@ extension DatabaseClient.ComponentLoss {
|
||||
},
|
||||
get: { id in
|
||||
try await ComponentLossModel.find(id, on: database).map { try $0.toDTO() }
|
||||
},
|
||||
update: { id, updates in
|
||||
try updates.validate()
|
||||
guard let model = try await ComponentLossModel.find(id, on: database) else {
|
||||
throw NotFoundError()
|
||||
}
|
||||
model.applyUpdates(updates)
|
||||
if model.hasChanges {
|
||||
try await model.save(on: database)
|
||||
}
|
||||
return try model.toDTO()
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -68,6 +82,24 @@ extension ComponentPressureLoss.Create {
|
||||
}
|
||||
}
|
||||
|
||||
extension ComponentPressureLoss.Update {
|
||||
func validate() throws(ValidationError) {
|
||||
if let name {
|
||||
guard !name.isEmpty else {
|
||||
throw ValidationError("Component loss name should not be empty.")
|
||||
}
|
||||
}
|
||||
if let value {
|
||||
guard value > 0 else {
|
||||
throw ValidationError("Component loss value should be greater than 0.")
|
||||
}
|
||||
guard value < 1.0 else {
|
||||
throw ValidationError("Component loss value should be less than 1.0.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension ComponentPressureLoss {
|
||||
struct Migrate: AsyncMigration {
|
||||
let name = "CreateComponentLoss"
|
||||
@@ -142,4 +174,13 @@ final class ComponentLossModel: Model, @unchecked Sendable {
|
||||
updatedAt: updatedAt!
|
||||
)
|
||||
}
|
||||
|
||||
func applyUpdates(_ updates: ComponentPressureLoss.Update) {
|
||||
if let name = updates.name, name != self.name {
|
||||
self.name = name
|
||||
}
|
||||
if let value = updates.value, value != self.value {
|
||||
self.value = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,8 @@ extension DatabaseClient {
|
||||
public var fetch: @Sendable (Project.ID) async throws -> [EffectiveLength]
|
||||
public var fetchMax: @Sendable (Project.ID) async throws -> EffectiveLength.MaxContainer
|
||||
public var get: @Sendable (EffectiveLength.ID) async throws -> EffectiveLength?
|
||||
public var update: @Sendable (EffectiveLength.Update) async throws -> EffectiveLength
|
||||
public var update:
|
||||
@Sendable (EffectiveLength.ID, EffectiveLength.Update) async throws -> EffectiveLength
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,11 +60,12 @@ extension DatabaseClient.EffectiveLengthClient: TestDependencyKey {
|
||||
get: { id in
|
||||
try await EffectiveLengthModel.find(id, on: database).map { try $0.toDTO() }
|
||||
},
|
||||
update: { updates in
|
||||
guard let model = try await EffectiveLengthModel.find(updates.id, on: database) else {
|
||||
update: { id, updates in
|
||||
guard let model = try await EffectiveLengthModel.find(id, on: database) else {
|
||||
throw NotFoundError()
|
||||
}
|
||||
if try model.applyUpdates(updates) {
|
||||
try model.applyUpdates(updates)
|
||||
if model.hasChanges {
|
||||
try await model.save(on: database)
|
||||
}
|
||||
return try model.toDTO()
|
||||
@@ -184,24 +186,18 @@ final class EffectiveLengthModel: Model, @unchecked Sendable {
|
||||
)
|
||||
}
|
||||
|
||||
func applyUpdates(_ updates: EffectiveLength.Update) throws -> Bool {
|
||||
var hasUpdates = false
|
||||
func applyUpdates(_ updates: EffectiveLength.Update) throws {
|
||||
if let name = updates.name, name != self.name {
|
||||
hasUpdates = true
|
||||
self.name = name
|
||||
}
|
||||
if let type = updates.type, type.rawValue != self.type {
|
||||
hasUpdates = true
|
||||
self.type = type.rawValue
|
||||
}
|
||||
if let straightLengths = updates.straightLengths, straightLengths != self.straightLengths {
|
||||
hasUpdates = true
|
||||
self.straightLengths = straightLengths
|
||||
}
|
||||
if let groups = updates.groups {
|
||||
hasUpdates = true
|
||||
self.groups = try JSONEncoder().encode(groups)
|
||||
}
|
||||
return hasUpdates
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,8 @@ extension DatabaseClient {
|
||||
public var delete: @Sendable (EquipmentInfo.ID) async throws -> Void
|
||||
public var fetch: @Sendable (Project.ID) async throws -> EquipmentInfo?
|
||||
public var get: @Sendable (EquipmentInfo.ID) async throws -> EquipmentInfo?
|
||||
public var update: @Sendable (EquipmentInfo.Update) async throws -> EquipmentInfo
|
||||
public var update:
|
||||
@Sendable (EquipmentInfo.ID, EquipmentInfo.Update) async throws -> EquipmentInfo
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,13 +47,15 @@ extension DatabaseClient.Equipment {
|
||||
get: { id in
|
||||
try await EquipmentModel.find(id, on: database).map { try $0.toDTO() }
|
||||
},
|
||||
update: { request in
|
||||
guard let model = try await EquipmentModel.find(request.id, on: database) else {
|
||||
update: { id, updates in
|
||||
guard let model = try await EquipmentModel.find(id, on: database) else {
|
||||
throw NotFoundError()
|
||||
}
|
||||
guard request.hasUpdates else { return try model.toDTO() }
|
||||
try model.applyUpdates(request)
|
||||
try await model.save(on: database)
|
||||
try updates.validate()
|
||||
model.applyUpdates(updates)
|
||||
if model.hasChanges {
|
||||
try await model.save(on: database)
|
||||
}
|
||||
return try model.toDTO()
|
||||
}
|
||||
)
|
||||
@@ -196,8 +199,7 @@ final class EquipmentModel: Model, @unchecked Sendable {
|
||||
)
|
||||
}
|
||||
|
||||
func applyUpdates(_ updates: EquipmentInfo.Update) throws {
|
||||
try updates.validate()
|
||||
func applyUpdates(_ updates: EquipmentInfo.Update) {
|
||||
if let staticPressure = updates.staticPressure {
|
||||
self.staticPressure = staticPressure
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ extension DatabaseClient {
|
||||
public var getCompletedSteps: @Sendable (Project.ID) async throws -> Project.CompletedSteps
|
||||
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
|
||||
public var update: @Sendable (Project.ID, Project.Update) async throws -> Project
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,12 +85,13 @@ extension DatabaseClient.Projects: TestDependencyKey {
|
||||
.paginate(request)
|
||||
.map { try $0.toDTO() }
|
||||
},
|
||||
update: { updates in
|
||||
guard let model = try await ProjectModel.find(updates.id, on: database) else {
|
||||
update: { id, updates in
|
||||
guard let model = try await ProjectModel.find(id, on: database) else {
|
||||
throw NotFoundError()
|
||||
}
|
||||
try updates.validate()
|
||||
if model.applyUpdates(updates) {
|
||||
model.applyUpdates(updates)
|
||||
if model.hasChanges {
|
||||
try await model.save(on: database)
|
||||
}
|
||||
return try model.toDTO()
|
||||
@@ -283,34 +284,26 @@ final class ProjectModel: Model, @unchecked Sendable {
|
||||
)
|
||||
}
|
||||
|
||||
func applyUpdates(_ updates: Project.Update) -> Bool {
|
||||
var hasUpdates = false
|
||||
func applyUpdates(_ updates: Project.Update) {
|
||||
if let name = updates.name, name != self.name {
|
||||
hasUpdates = true
|
||||
self.name = name
|
||||
}
|
||||
if let streetAddress = updates.streetAddress, streetAddress != self.streetAddress {
|
||||
hasUpdates = true
|
||||
self.streetAddress = streetAddress
|
||||
}
|
||||
if let city = updates.city, city != self.city {
|
||||
hasUpdates = true
|
||||
self.city = city
|
||||
}
|
||||
if let state = updates.state, state != self.state {
|
||||
hasUpdates = true
|
||||
self.state = state
|
||||
}
|
||||
if let zipCode = updates.zipCode, zipCode != self.zipCode {
|
||||
hasUpdates = true
|
||||
self.zipCode = zipCode
|
||||
}
|
||||
if let sensibleHeatRatio = updates.sensibleHeatRatio,
|
||||
sensibleHeatRatio != self.sensibleHeatRatio
|
||||
{
|
||||
hasUpdates = true
|
||||
self.sensibleHeatRatio = sensibleHeatRatio
|
||||
}
|
||||
return hasUpdates
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ extension DatabaseClient {
|
||||
public var delete: @Sendable (Room.ID) async throws -> Void
|
||||
public var get: @Sendable (Room.ID) async throws -> Room?
|
||||
public var fetch: @Sendable (Project.ID) async throws -> [Room]
|
||||
public var update: @Sendable (Room.Update) async throws -> Room
|
||||
public var update: @Sendable (Room.ID, Room.Update) async throws -> Room
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,13 +41,14 @@ extension DatabaseClient.Rooms: TestDependencyKey {
|
||||
.all()
|
||||
.map { try $0.toDTO() }
|
||||
},
|
||||
update: { updates in
|
||||
guard let model = try await RoomModel.find(updates.id, on: database) else {
|
||||
update: { id, updates in
|
||||
guard let model = try await RoomModel.find(id, on: database) else {
|
||||
throw NotFoundError()
|
||||
}
|
||||
|
||||
try updates.validate()
|
||||
if model.applyUpdates(updates) {
|
||||
model.applyUpdates(updates)
|
||||
if model.hasChanges {
|
||||
try await model.save(on: database)
|
||||
}
|
||||
return try model.toDTO()
|
||||
@@ -218,30 +219,24 @@ final class RoomModel: Model, @unchecked Sendable {
|
||||
)
|
||||
}
|
||||
|
||||
func applyUpdates(_ updates: Room.Update) -> Bool {
|
||||
var hasUpdates = false
|
||||
func applyUpdates(_ updates: Room.Update) {
|
||||
|
||||
if let name = updates.name, name != self.name {
|
||||
hasUpdates = true
|
||||
self.name = name
|
||||
}
|
||||
if let heatingLoad = updates.heatingLoad, heatingLoad != self.heatingLoad {
|
||||
hasUpdates = true
|
||||
self.heatingLoad = heatingLoad
|
||||
}
|
||||
if let coolingTotal = updates.coolingTotal, coolingTotal != self.coolingTotal {
|
||||
hasUpdates = true
|
||||
self.coolingTotal = coolingTotal
|
||||
}
|
||||
if let coolingSensible = updates.coolingSensible, coolingSensible != self.coolingSensible {
|
||||
hasUpdates = true
|
||||
self.coolingSensible = coolingSensible
|
||||
}
|
||||
if let registerCount = updates.registerCount, registerCount != self.registerCount {
|
||||
hasUpdates = true
|
||||
self.registerCount = registerCount
|
||||
}
|
||||
return hasUpdates
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user