diff --git a/Sources/App/CapServer.swift b/Sources/App/CapServer.swift index e3a6d40..7532c0d 100644 --- a/Sources/App/CapServer.swift +++ b/Sources/App/CapServer.swift @@ -176,7 +176,7 @@ final class CapServer { f = file(of: cap, version: id) } try data.write(to: f) - caps[cap]!.count += 1 + caps[cap]!.count = try count(of: cap) log("Added image \(id) for cap \(cap)") } @@ -192,21 +192,23 @@ final class CapServer { func addOrUpdate(_ cap: Cap) throws { if let existingCap = caps[cap.id] { - try update(existingCap, with: cap) + update(existingCap, with: cap) } else { try add(cap) } } private func add(_ cap: Cap) throws { - guard cap.mainImage == 0, cap.count == 0 else { + guard cap.mainImage == 0 else { throw CapError.invalidData } + var cap = cap + cap.count = 0 caps[cap.id] = cap log("Added cap \(cap.id) '\(cap.name)'") } - private func update(_ existingCap: Cap, with cap: Cap) throws { + private func update(_ existingCap: Cap, with cap: Cap) { var updatedCap = existingCap if cap.name != "" { updatedCap.name = cap.name