diff --git a/AutoCat.xcodeproj/project.pbxproj b/AutoCat.xcodeproj/project.pbxproj index 9dddb81..469cafa 100644 --- a/AutoCat.xcodeproj/project.pbxproj +++ b/AutoCat.xcodeproj/project.pbxproj @@ -148,6 +148,7 @@ 7AA514E02D0B75B3001CAC50 /* StorageService+Events.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA514DF2D0B75B3001CAC50 /* StorageService+Events.swift */; }; 7AA515D02D9ABCC800EB3418 /* RecordPlayerService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA515CF2D9ABCC800EB3418 /* RecordPlayerService.swift */; }; 7AA515D22D9ABCE600EB3418 /* RecordPlayerServiceProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA515D12D9ABCE600EB3418 /* RecordPlayerServiceProtocol.swift */; }; + 7AA515DA2D9ADEF000EB3418 /* StorageError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA515D92D9ADEF000EB3418 /* StorageError.swift */; }; 7AA7BC3525A5DFB80053A5D5 /* ExceptionCatcher in Frameworks */ = {isa = PBXBuildFile; productRef = 7A813DC02508C4D900CC93B9 /* ExceptionCatcher */; }; 7AA7BC3625A5DFB80053A5D5 /* PKHUD in Frameworks */ = {isa = PBXBuildFile; productRef = 7AABDE1C2532F3EB0041AFC6 /* PKHUD */; }; 7AAAFAD32C4D0FD00050410D /* ACImageSliderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AAAFAD22C4D0FD00050410D /* ACImageSliderView.swift */; }; @@ -448,6 +449,7 @@ 7AA514DF2D0B75B3001CAC50 /* StorageService+Events.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "StorageService+Events.swift"; sourceTree = ""; }; 7AA515CF2D9ABCC800EB3418 /* RecordPlayerService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecordPlayerService.swift; sourceTree = ""; }; 7AA515D12D9ABCE600EB3418 /* RecordPlayerServiceProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecordPlayerServiceProtocol.swift; sourceTree = ""; }; + 7AA515D92D9ADEF000EB3418 /* StorageError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageError.swift; sourceTree = ""; }; 7AAAFAD22C4D0FD00050410D /* ACImageSliderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ACImageSliderView.swift; sourceTree = ""; }; 7AAAFAD92C4D1AFE0050410D /* Zoomable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Zoomable.swift; sourceTree = ""; }; 7AAAFADB2C4D1E130050410D /* ACImageSliderView+Modifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ACImageSliderView+Modifier.swift"; sourceTree = ""; }; @@ -1085,6 +1087,7 @@ 7AA514DF2D0B75B3001CAC50 /* StorageService+Events.swift */, 7ABDA80E2D8723F90083C715 /* StorageService+AudioRecords.swift */, 7A54BFD22D43B95E00176D6D /* DbUpdatePolicy.swift */, + 7AA515D92D9ADEF000EB3418 /* StorageError.swift */, ); path = StorageService; sourceTree = ""; @@ -1663,6 +1666,7 @@ 7AB4E4382D3D0C5C0006D052 /* VehiclesArchive.swift in Sources */, 7A64A21C2C19E87B00284124 /* OsagoDto.swift in Sources */, 7AA515D22D9ABCE600EB3418 /* RecordPlayerServiceProtocol.swift in Sources */, + 7AA515DA2D9ADEF000EB3418 /* StorageError.swift in Sources */, 7A809F392D66755B00CF1B3C /* Error+Canceled.swift in Sources */, 7AF6D21D2677C1680086EA64 /* Osago.swift in Sources */, 7A1CF81629A42117007962DA /* Realm.swift in Sources */, diff --git a/AutoCatCore/Services/StorageService/StorageError.swift b/AutoCatCore/Services/StorageService/StorageError.swift new file mode 100644 index 0000000..ccc0148 --- /dev/null +++ b/AutoCatCore/Services/StorageService/StorageError.swift @@ -0,0 +1,26 @@ +// +// StorageError.swift +// AutoCatCore +// +// Created by Selim Mustafaev on 31.03.2025. +// Copyright © 2025 Selim Mustafaev. All rights reserved. +// + +import Foundation + +public enum StorageError: LocalizedError { + + case vehicleNotFound + case noteNotFound + case eventNotFound + case recordNotFound + + public var errorDescription: String? { + switch self { + case .vehicleNotFound: "Vehicle not found in realm database" + case .noteNotFound: "Vehicle note not found in realm database" + case .eventNotFound: "Event not found in realm database" + case .recordNotFound: "Audio record not found in realm database" + } + } +} diff --git a/AutoCatCore/Services/StorageService/StorageService+AudioRecords.swift b/AutoCatCore/Services/StorageService/StorageService+AudioRecords.swift index 7ad28ee..f949801 100644 --- a/AutoCatCore/Services/StorageService/StorageService+AudioRecords.swift +++ b/AutoCatCore/Services/StorageService/StorageService+AudioRecords.swift @@ -23,4 +23,26 @@ public extension StorageService { .sorted(byKeyPath: "addedDate", ascending: false) .map(\.dto) } + + func deleteRecord(id: TimeInterval) async throws { + + guard let record = realm.object(ofType: AudioRecord.self, forPrimaryKey: id) else { + throw StorageError.recordNotFound + } + + try await realm.asyncWrite { + realm.delete(record) + } + } + + func updateRecord(_ record: AudioRecordDto) async throws { + + guard let record = realm.object(ofType: AudioRecord.self, forPrimaryKey: record.id) else { + throw StorageError.recordNotFound + } + + try await realm.asyncWrite { + realm.add(record, update: .modified) + } + } } diff --git a/AutoCatCore/Services/StorageService/StorageService.swift b/AutoCatCore/Services/StorageService/StorageService.swift index aebaa24..4106b84 100644 --- a/AutoCatCore/Services/StorageService/StorageService.swift +++ b/AutoCatCore/Services/StorageService/StorageService.swift @@ -9,21 +9,6 @@ import Foundation import RealmSwift -public enum StorageError: LocalizedError { - - case vehicleNotFound - case noteNotFound - case eventNotFound - - public var errorDescription: String? { - switch self { - case .vehicleNotFound: "Vehicle not found in realm database" - case .noteNotFound: "Vehicle note not found in realm database" - case .eventNotFound: "Event not found in realm database" - } - } -} - public actor StorageService: StorageServiceProtocol { let settingsService: SettingsServiceProtocol diff --git a/AutoCatCore/Services/StorageService/StorageServiceProtocol.swift b/AutoCatCore/Services/StorageService/StorageServiceProtocol.swift index 63416ee..7c83173 100644 --- a/AutoCatCore/Services/StorageService/StorageServiceProtocol.swift +++ b/AutoCatCore/Services/StorageService/StorageServiceProtocol.swift @@ -35,4 +35,6 @@ public protocol StorageServiceProtocol: Sendable { // Audio records func add(record: AudioRecordDto) async throws func loadRecords() async throws -> [AudioRecordDto] + func deleteRecord(id: TimeInterval) async throws + func updateRecord(_ record: AudioRecordDto) async throws }