2019-10-12 19:03:50 +00:00
|
|
|
//
|
|
|
|
// CategoryRepository.swift
|
|
|
|
// Budget
|
|
|
|
//
|
|
|
|
// Created by Billy Brawner on 10/1/19.
|
|
|
|
// Copyright © 2019 William Brawner. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
|
|
|
|
|
|
|
protocol CategoryRepository {
|
2021-09-19 20:24:27 +00:00
|
|
|
func getCategories(budgetId: String?, expense: Bool?, archived: Bool?, count: Int?, page: Int?) -> AnyPublisher<[Category], NetworkError>
|
2021-09-10 04:47:57 +00:00
|
|
|
func getCategory(_ categoryId: String) -> AnyPublisher<Category, NetworkError>
|
2019-10-12 19:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class NetworkCategoryRepository: CategoryRepository {
|
2019-10-19 22:18:06 +00:00
|
|
|
let apiService: BudgetAppApiService
|
|
|
|
let cacheService: BudgetAppInMemoryCacheService?
|
|
|
|
|
|
|
|
init(_ apiService: BudgetAppApiService, cacheService: BudgetAppInMemoryCacheService? = nil) {
|
2019-10-12 19:03:50 +00:00
|
|
|
self.apiService = apiService
|
2019-10-19 22:18:06 +00:00
|
|
|
self.cacheService = cacheService
|
2019-10-12 19:03:50 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 20:24:27 +00:00
|
|
|
func getCategories(budgetId: String?, expense: Bool?, archived: Bool?, count: Int?, page: Int?) -> AnyPublisher<[Category], NetworkError> {
|
|
|
|
if let categories = cacheService?.getCategories(budgetId: budgetId, expense: expense, archived: archived, count: count, page: page) {
|
2019-10-19 22:18:06 +00:00
|
|
|
print("Returning categories from cache")
|
|
|
|
return categories
|
|
|
|
}
|
|
|
|
|
|
|
|
print("No cached categories, fetching from network")
|
2021-09-19 20:24:27 +00:00
|
|
|
return apiService.getCategories(budgetId: budgetId, expense: expense, archived: archived, count: count, page: page).map { (categories: [Category]) in
|
2019-10-19 22:18:06 +00:00
|
|
|
self.cacheService?.addCategories(categories)
|
|
|
|
return categories
|
|
|
|
}.eraseToAnyPublisher()
|
2019-10-12 19:03:50 +00:00
|
|
|
}
|
2019-10-13 01:22:09 +00:00
|
|
|
|
2021-09-10 04:47:57 +00:00
|
|
|
func getCategory(_ categoryId: String) -> AnyPublisher<Category, NetworkError> {
|
2019-10-19 22:18:06 +00:00
|
|
|
if let category = cacheService?.getCategory(categoryId) {
|
|
|
|
print("Returning category from cache")
|
|
|
|
return category
|
|
|
|
}
|
|
|
|
print("Category with ID \(categoryId) not cached, returning from network")
|
|
|
|
return apiService.getCategory(categoryId).map { category in
|
|
|
|
self.cacheService?.addCategory(category)
|
|
|
|
return category
|
|
|
|
}.eraseToAnyPublisher()
|
2019-10-13 01:22:09 +00:00
|
|
|
}
|
2019-10-12 19:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
|
|
|
class MockCategoryRepository: CategoryRepository {
|
2019-10-13 01:22:09 +00:00
|
|
|
static let category = Category(
|
2021-09-10 04:47:57 +00:00
|
|
|
budgetId: MockBudgetRepository.budget.id,
|
|
|
|
id: "3",
|
2019-10-13 01:22:09 +00:00
|
|
|
title: "Test Category",
|
|
|
|
description: "This is a test category to help with testing",
|
2021-09-10 04:47:57 +00:00
|
|
|
amount: 10000,
|
2021-09-15 03:22:14 +00:00
|
|
|
expense: true,
|
|
|
|
archived: false
|
2019-10-13 01:22:09 +00:00
|
|
|
)
|
|
|
|
|
2021-09-19 20:24:27 +00:00
|
|
|
func getCategories(budgetId: String?, expense: Bool?, archived: Bool?, count: Int?, page: Int?) -> AnyPublisher<[Category], NetworkError> {
|
2019-10-13 01:22:09 +00:00
|
|
|
return Result.Publisher([MockCategoryRepository.category]).eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2021-09-10 04:47:57 +00:00
|
|
|
func getCategory(_ categoryId: String) -> AnyPublisher<Category, NetworkError> {
|
2019-10-13 01:22:09 +00:00
|
|
|
return Result.Publisher(MockCategoryRepository.category).eraseToAnyPublisher()
|
2019-10-12 19:03:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|