Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a use case for fetching dive logs #9

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Sources/BadaData/PersistentStore.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Foundation
import SwiftData
import BadaDomain

@Repository
enum PersistentStore {
case main
case mainTest
Expand All @@ -25,6 +27,7 @@ enum PersistentStore {
}

extension PersistentStore {
@Repository
private static let _mainContainer: ModelContainer = {
do {
let container = try ModelContainer(
Expand All @@ -45,10 +48,12 @@ extension PersistentStore {
}
}()

@Repository
private static let _mainContext: ModelContext = {
ModelContext(_mainContainer)
}()

@Repository
private static let _mainTestContainer: ModelContainer = {
do {
let container = try ModelContainer(
Expand All @@ -69,6 +74,7 @@ extension PersistentStore {
}
}()

@Repository
private static let _mainTestContext: ModelContext = {
ModelContext(_mainTestContainer)
}()
Expand Down
1 change: 1 addition & 0 deletions Sources/BadaDomain/Interfaces/DiveLogRepositoryType.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

@Repository
package protocol DiveLogRepositoryType {
func insert(diveLog: DiveLog) -> Result<Void, DiveLogRepositoryError>
func diveLogs() -> Result<[DiveLog], DiveLogRepositoryError>
Expand Down
4 changes: 4 additions & 0 deletions Sources/BadaDomain/Repository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@globalActor
package actor Repository {
package static let shared = Repository()
}
15 changes: 15 additions & 0 deletions Sources/BadaDomain/UseCases/GetDiveLogsUseCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Foundation

package struct GetDiveLogsUseCase {
private let diveLogs: () -> Result<[DiveLog], DiveLogRepositoryError>

package init(
diveLogs: @escaping () -> Result<[DiveLog], DiveLogRepositoryError>
) {
self.diveLogs = diveLogs
}

package func execute() -> Result<[DiveLog], DiveLogRepositoryError> {
diveLogs()
}
}
10 changes: 5 additions & 5 deletions Tests/BadaDataTests/DiveLogRepositoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import BadaDomain
final class DiveLogRepositoryTests: XCTestCase {
var sut: DiveLogRepository!

override func setUp() {
sut = DiveLogRepository(persistentStore: PersistentStore.mainTest)
override func setUp() async throws {
sut = await DiveLogRepository(persistentStore: PersistentStore.mainTest)
}

func test_insert() {
func test_insert() async {
let diveLog = DiveLog(
location: DiveLog.Location(
latitude: 1,
Expand All @@ -29,14 +29,14 @@ final class DiveLogRepositoryTests: XCTestCase {
notes: "notes1"
)

switch sut.insert(diveLog: diveLog) {
switch await sut.insert(diveLog: diveLog) {
case .success:
XCTAssert(true)
case let .failure(error):
XCTFail(error.localizedDescription)
}

switch sut.diveLogs() {
switch await sut.diveLogs() {
case let .success(diveLogs):
XCTAssertEqual(diveLogs.count, 1)
XCTAssertEqual(diveLogs.first!, diveLog)
Expand Down
Loading