2019-10-19 22:18:06 +00:00
|
|
|
//
|
|
|
|
// BudgetApiService.swift
|
|
|
|
// Budget
|
|
|
|
//
|
|
|
|
// Created by Billy Brawner on 9/25/19.
|
|
|
|
// Copyright © 2019 William Brawner. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2022-01-03 17:56:43 +00:00
|
|
|
import TwigsCore
|
2019-10-19 22:18:06 +00:00
|
|
|
|
2021-12-10 04:35:56 +00:00
|
|
|
class TwigsInMemoryCacheService: TwigsApiService {
|
2022-01-03 17:56:43 +00:00
|
|
|
private var budgets = Set<Budget>()
|
|
|
|
private var categories = Set<TwigsCore.Category>()
|
|
|
|
private var transactions = Set<Transaction>()
|
2019-10-19 22:18:06 +00:00
|
|
|
|
2022-01-03 17:56:43 +00:00
|
|
|
public init() {
|
|
|
|
super.init(RequestHelper())
|
|
|
|
}
|
|
|
|
|
2019-10-19 22:18:06 +00:00
|
|
|
// MARK: Budgets
|
2022-01-03 17:56:43 +00:00
|
|
|
override func getBudgets(count: Int? = nil, page: Int? = nil) async throws -> [Budget] {
|
2019-10-19 22:18:06 +00:00
|
|
|
let results = budgets.sorted { (first, second) -> Bool in
|
|
|
|
return first.name < second.name
|
|
|
|
}
|
|
|
|
if results.isEmpty {
|
2022-01-03 17:56:43 +00:00
|
|
|
let budgets = try await super.getBudgets(count: count, page: page)
|
|
|
|
self.addBudgets(budgets)
|
|
|
|
return budgets
|
2019-10-19 22:18:06 +00:00
|
|
|
}
|
2022-01-03 17:56:43 +00:00
|
|
|
return results.slice(count: count, page: page)
|
2019-10-19 22:18:06 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 17:56:43 +00:00
|
|
|
override func getBudget(_ id: String) async throws -> Budget {
|
2019-10-19 22:18:06 +00:00
|
|
|
guard let budget = budgets.first(where: { $0.id == id }) else {
|
2022-01-03 17:56:43 +00:00
|
|
|
let budget = try await super.getBudget(id)
|
|
|
|
self.addBudget(budget)
|
|
|
|
return budget
|
|
|
|
}
|
|
|
|
return budget
|
|
|
|
}
|
|
|
|
|
|
|
|
override func newBudget(_ budget: Budget) async throws -> Budget {
|
|
|
|
let newBudget = try await super.newBudget(budget)
|
|
|
|
self.addBudget(newBudget)
|
|
|
|
return newBudget
|
|
|
|
}
|
|
|
|
|
|
|
|
override func updateBudget(_ budget: Budget) async throws -> Budget {
|
|
|
|
let newBudget = try await super.updateBudget(budget)
|
|
|
|
if let index = self.budgets.firstIndex(where: {$0.id == budget.id}) {
|
|
|
|
self.budgets.remove(at: index)
|
|
|
|
}
|
|
|
|
self.addBudget(newBudget)
|
|
|
|
return newBudget
|
|
|
|
}
|
|
|
|
|
|
|
|
override func deleteBudget(_ id: String) async throws {
|
|
|
|
try await super.deleteBudget(id)
|
|
|
|
if let index = self.budgets.firstIndex(where: {$0.id == id}) {
|
|
|
|
self.budgets.remove(at: index)
|
2019-10-19 22:18:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 17:56:43 +00:00
|
|
|
private func addBudgets(_ budgets: [Budget]) {
|
2019-10-19 22:18:06 +00:00
|
|
|
budgets.forEach { addBudget($0) }
|
|
|
|
}
|
|
|
|
|
2022-01-03 17:56:43 +00:00
|
|
|
private func addBudget(_ budget: Budget) {
|
2019-10-19 22:18:06 +00:00
|
|
|
self.budgets.insert(budget)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Categories
|
2022-01-03 17:56:43 +00:00
|
|
|
override func getCategories(budgetId: String? = nil, expense: Bool? = nil, archived: Bool? = nil, count: Int? = nil, page: Int? = nil) async throws -> [TwigsCore.Category] {
|
2019-10-19 22:18:06 +00:00
|
|
|
var results = categories
|
|
|
|
if budgetId != nil {
|
|
|
|
results = categories.filter { $0.budgetId == budgetId }
|
|
|
|
}
|
2021-09-19 20:24:27 +00:00
|
|
|
if expense != nil {
|
|
|
|
results = results.filter { $0.expense == expense }
|
|
|
|
}
|
2021-09-15 03:22:14 +00:00
|
|
|
if archived != nil {
|
|
|
|
results = results.filter { $0.archived == archived }
|
|
|
|
}
|
2019-10-19 22:18:06 +00:00
|
|
|
if results.isEmpty {
|
2022-01-03 17:56:43 +00:00
|
|
|
let categories = try await super.getCategories(budgetId: budgetId, expense: expense, archived: archived, count: count, page: page)
|
|
|
|
self.addCategories(categories)
|
|
|
|
return categories
|
2019-10-19 22:18:06 +00:00
|
|
|
}
|
|
|
|
let sortedResults = results.sorted { $0.title < $1.title }
|
2022-01-03 17:56:43 +00:00
|
|
|
return sortedResults.slice(count: count, page: page)
|
2019-10-19 22:18:06 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 17:56:43 +00:00
|
|
|
override func getCategory(_ id: String) async throws -> TwigsCore.Category {
|
2019-10-19 22:18:06 +00:00
|
|
|
guard let category = categories.first(where: { $0.id == id }) else {
|
2022-01-03 17:56:43 +00:00
|
|
|
let category = try await super.getCategory(id)
|
|
|
|
self.addCategory(category)
|
|
|
|
return category
|
2019-10-19 22:18:06 +00:00
|
|
|
}
|
2022-01-03 17:56:43 +00:00
|
|
|
return category
|
2019-10-19 22:18:06 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 17:56:43 +00:00
|
|
|
private func addCategories(_ categories: [TwigsCore.Category]) {
|
2019-10-19 22:18:06 +00:00
|
|
|
categories.forEach { addCategory($0) }
|
|
|
|
}
|
|
|
|
|
2022-01-03 17:56:43 +00:00
|
|
|
private func addCategory(_ category: TwigsCore.Category) {
|
2019-10-19 22:18:06 +00:00
|
|
|
self.categories.insert(category)
|
|
|
|
}
|
2021-10-22 02:43:01 +00:00
|
|
|
|
2022-01-03 17:56:43 +00:00
|
|
|
override func createCategory(_ category: TwigsCore.Category) async throws -> TwigsCore.Category {
|
|
|
|
let newCategory = try await super.createCategory(category)
|
|
|
|
self.categories.insert(newCategory)
|
|
|
|
return newCategory
|
2021-12-10 04:35:56 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 17:56:43 +00:00
|
|
|
override func updateCategory(_ category: TwigsCore.Category) async throws -> TwigsCore.Category {
|
|
|
|
let newCategory = try await super.updateCategory(category)
|
|
|
|
self.removeCategory(newCategory.id)
|
|
|
|
self.categories.insert(newCategory)
|
|
|
|
return newCategory
|
2021-12-10 04:35:56 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 17:56:43 +00:00
|
|
|
override func deleteCategory(_ id: String) async throws {
|
|
|
|
try await super.deleteCategory(id)
|
|
|
|
self.removeCategory(id)
|
2021-10-22 02:43:01 +00:00
|
|
|
}
|
2022-01-03 17:56:43 +00:00
|
|
|
|
|
|
|
private func removeCategory(_ id: String) {
|
2021-10-22 02:43:01 +00:00
|
|
|
if let index = self.categories.firstIndex(where: { $0.id == id }) {
|
|
|
|
self.categories.remove(at: index)
|
|
|
|
}
|
|
|
|
}
|
2019-10-19 22:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines which slice of the array should be returned based on the count and page parameters
|
|
|
|
*/
|
|
|
|
private func calculateStartAndEndIndices(count: Int, page: Int?) -> (start: Int, end: Int?) {
|
|
|
|
let end = count * (page ?? 1)
|
|
|
|
let start = max(end - count, 0)
|
|
|
|
return (start, end)
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Array {
|
|
|
|
func slice(count: Int?, page: Int?) -> Array<Element> {
|
|
|
|
if count == nil {
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
let indices: (Int, Int?) = calculateStartAndEndIndices(count: count!, page: page)
|
|
|
|
return Array(self[indices.0..<Swift.min((indices.1 ?? self.count), self.count)])
|
|
|
|
}
|
|
|
|
}
|