Skip to content

Commit

Permalink
migrate persistence module
Browse files Browse the repository at this point in the history
  • Loading branch information
rcaos committed Nov 2, 2024
1 parent ed30266 commit 3a135cc
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
//
// ShowsVisitedLocalDataSource.swift
//
//
// Created by Jeans Ruiz on 11/05/22.
//

import Combine
import Shared

public protocol ShowsVisitedLocalDataSource {
func saveShow(id: Int, pathImage: String, userId: Int) -> AnyPublisher<Void, ErrorEnvelope>
func saveShow(id: Int, pathImage: String, userId: Int)

func fetchVisitedShows(userId: Int) -> AnyPublisher<[ShowVisitedDLO], ErrorEnvelope>
func fetchVisitedShows(userId: Int) -> [ShowVisitedDLO]

// Use AsyncStream instead
func recentVisitedShowsDidChange() -> AnyPublisher<Bool, Never>
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//
// ShowsVisitedLocalRepository.swift
//
//
// Created by Jeans Ruiz on 11/05/22.
//

Expand All @@ -19,18 +16,14 @@ public final class ShowsVisitedLocalRepository {
}

extension ShowsVisitedLocalRepository: ShowsVisitedLocalRepositoryProtocol {
public func saveShow(id: Int, pathImage: String) -> AnyPublisher<Void, ErrorEnvelope> {
public func saveShow(id: Int, pathImage: String) {
let userId = loggedUserRepository.getUser()?.id ?? 0
return dataSource.saveShow(id: id, pathImage: pathImage, userId: userId)
}

public func fetchVisitedShows() -> AnyPublisher<[ShowVisited], ErrorEnvelope> {
public func fetchVisitedShows() -> [ShowVisited] {
let userId = loggedUserRepository.getUser()?.id ?? 0
return dataSource.fetchVisitedShows(userId: userId)
.map {
return $0.map { ShowVisited(id: $0.id, pathImage: $0.pathImage) }
}
.eraseToAnyPublisher()
return dataSource.fetchVisitedShows(userId: userId).map { ShowVisited(id: $0.id, pathImage: $0.pathImage) }
}

public func recentVisitedShowsDidChange() -> AnyPublisher<Bool, Never> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
//
// ShowsVisitedLocalRepositoryProtocol.swift
// Persistence
//
// Created by Jeans Ruiz on 7/2/20.
// Copyright © 2020 Jeans. All rights reserved.
//

import Combine
import Shared

public protocol ShowsVisitedLocalRepositoryProtocol {
func saveShow(id: Int, pathImage: String) -> AnyPublisher<Void, ErrorEnvelope>

func fetchVisitedShows() -> AnyPublisher<[ShowVisited], ErrorEnvelope>

func saveShow(id: Int, pathImage: String)
func fetchVisitedShows() -> [ShowVisited]
func recentVisitedShowsDidChange() -> AnyPublisher<Bool, Never>
}
7 changes: 2 additions & 5 deletions Sources/Persistence/UseCases/FetchVisitedShowsUseCase.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
//
// FetchVisitedShowsUseCase.swift
// Persistence
//
// Created by Jeans Ruiz on 7/3/20.
//

import Combine
import Shared

public protocol FetchVisitedShowsUseCase {
func execute(requestValue: FetchVisitedShowsUseCaseRequestValue) -> AnyPublisher<[ShowVisited], ErrorEnvelope>
func execute(requestValue: FetchVisitedShowsUseCaseRequestValue) -> [ShowVisited]
}

public struct FetchVisitedShowsUseCaseRequestValue {
Expand All @@ -24,7 +21,7 @@ public final class DefaultFetchVisitedShowsUseCase: FetchVisitedShowsUseCase {
self.showsVisitedLocalRepository = showsVisitedLocalRepository
}

public func execute(requestValue: FetchVisitedShowsUseCaseRequestValue) -> AnyPublisher<[ShowVisited], ErrorEnvelope> {
public func execute(requestValue: FetchVisitedShowsUseCaseRequestValue) -> [ShowVisited] {
return showsVisitedLocalRepository.fetchVisitedShows()
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
//
// CoreDataShowVisitedStorage.swift
// PersistenceLive
//
// Created by Jeans Ruiz on 7/2/20.
// Copyright © 2020 Jeans. All rights reserved.
//

import Combine
Expand All @@ -26,26 +22,14 @@ final class CoreDataShowVisitedStorage {

extension CoreDataShowVisitedStorage: ShowsVisitedLocalDataSource {

public func saveShow(id: Int, pathImage: String, userId: Int) -> AnyPublisher<Void, ErrorEnvelope> {
return Deferred { [store, limitStorage] in
return Future<Void, ErrorEnvelope> { promise in
store.delete(showId: id)
store.deleteLimitStorage(userId: userId, until: limitStorage)
store.insert(id: id, pathImage: pathImage, userId: userId)
promise(.success(()))
}
}
.eraseToAnyPublisher()
public func saveShow(id: Int, pathImage: String, userId: Int) {
store.delete(showId: id)
store.deleteLimitStorage(userId: userId, until: limitStorage)
store.insert(id: id, pathImage: pathImage, userId: userId)
}

public func fetchVisitedShows(userId: Int) -> AnyPublisher<[ShowVisitedDLO], ErrorEnvelope> {
return Deferred { [store] in
return Future<[ShowVisitedDLO], ErrorEnvelope> { promise in
let results = store.findAll(for: userId).map { $0.toDomain() }
promise(.success(results))
}
}
.eraseToAnyPublisher()
public func fetchVisitedShows(userId: Int) -> [ShowVisitedDLO] {
return store.findAll(for: userId).map { $0.toDomain() }
}

public func recentVisitedShowsDidChange() -> AnyPublisher<Bool, Never> {
Expand Down

0 comments on commit 3a135cc

Please sign in to comment.