twigs-swift-sdk/Sources/TwigsCore/Transaction.swift

71 lines
2.1 KiB
Swift
Raw Normal View History

2021-12-23 03:44:47 +00:00
//
// File.swift
//
//
// Created by William Brawner on 12/22/21.
//
import Foundation
2021-12-23 13:10:33 +00:00
public struct Transaction: Identifiable, Hashable, Codable {
public let id: String
public let title: String
public let description: String?
public let date: Date
public let amount: Int
public let categoryId: String?
public let expense: Bool
public let createdBy: String
public let budgetId: String
public init(id: String = "", title: String = "", description: String? = "", date: Date = Date(), amount: Int = 0, categoryId: String? = "", expense: Bool = true, createdBy: String, budgetId: String) {
self.id = id
self.title = title
self.description = description
self.date = date
self.amount = amount
self.categoryId = categoryId
self.expense = expense
self.createdBy = createdBy
self.budgetId = budgetId
}
2021-12-23 03:44:47 +00:00
}
2021-12-23 13:10:33 +00:00
public struct BalanceResponse: Codable {
public let balance: Int
public init(balance: Int) {
self.balance = balance
}
2021-12-23 03:44:47 +00:00
}
2021-12-23 13:10:33 +00:00
public enum TransactionType: Int, CaseIterable, Identifiable, Hashable {
2021-12-23 03:44:47 +00:00
case expense
case income
2021-12-23 13:10:33 +00:00
public var id: TransactionType { self }
2021-12-23 03:44:47 +00:00
}
extension Transaction {
2021-12-23 13:10:33 +00:00
public var type: TransactionType {
2021-12-23 03:44:47 +00:00
if (self.expense) {
return .expense
} else {
return .income
}
}
2021-12-23 13:10:33 +00:00
public var amountString: String {
return self.amount > 0 ? String(format: "%.02f", Double(self.amount) / 100.0) : ""
2021-12-23 03:44:47 +00:00
}
}
2021-12-23 13:10:33 +00:00
public protocol TransactionRepository {
2021-12-23 03:44:47 +00:00
func getTransactions(budgetIds: [String], categoryIds: [String]?, from: Date?, to: Date?, count: Int?, page: Int?) async throws -> [Transaction]
func getTransaction(_ transactionId: String) async throws -> Transaction
func createTransaction(_ transaction: Transaction) async throws -> Transaction
func updateTransaction(_ transaction: Transaction) async throws -> Transaction
func deleteTransaction(_ transactionId: String) async throws
func sumTransactions(budgetId: String?, categoryId: String?, from: Date?, to: Date?) async throws -> BalanceResponse
}