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 Attachment #46

Merged
merged 1 commit into from
Sep 25, 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
8 changes: 8 additions & 0 deletions MMEX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
920824102CA4C5CD00388AB2 /* BudgetYearRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9208240F2CA4C5CD00388AB2 /* BudgetYearRepository.swift */; };
920824122CA4C6A400388AB2 /* BudgetTableRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 920824112CA4C6A400388AB2 /* BudgetTableRepository.swift */; };
920824162CA4C95F00388AB2 /* ReportRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 920824152CA4C95F00388AB2 /* ReportRepository.swift */; };
920824182CA4CF4300388AB2 /* AttachmentData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 920824172CA4CF4300388AB2 /* AttachmentData.swift */; };
9208241A2CA4D35700388AB2 /* AttachmentRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 920824192CA4D35700388AB2 /* AttachmentRepository.swift */; };
929EF65F2C9FF2DE0051A3E6 /* AssetData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 929EF65E2C9FF2DE0051A3E6 /* AssetData.swift */; };
929EF6612C9FF2FD0051A3E6 /* StockData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 929EF6602C9FF2FD0051A3E6 /* StockData.swift */; };
929EF6632C9FF3ED0051A3E6 /* AssetRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 929EF6622C9FF3ED0051A3E6 /* AssetRepository.swift */; };
Expand Down Expand Up @@ -114,6 +116,8 @@
9208240F2CA4C5CD00388AB2 /* BudgetYearRepository.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BudgetYearRepository.swift; sourceTree = "<group>"; };
920824112CA4C6A400388AB2 /* BudgetTableRepository.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BudgetTableRepository.swift; sourceTree = "<group>"; };
920824152CA4C95F00388AB2 /* ReportRepository.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReportRepository.swift; sourceTree = "<group>"; };
920824172CA4CF4300388AB2 /* AttachmentData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AttachmentData.swift; sourceTree = "<group>"; };
920824192CA4D35700388AB2 /* AttachmentRepository.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AttachmentRepository.swift; sourceTree = "<group>"; };
929EF65E2C9FF2DE0051A3E6 /* AssetData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssetData.swift; sourceTree = "<group>"; };
929EF6602C9FF2FD0051A3E6 /* StockData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StockData.swift; sourceTree = "<group>"; };
929EF6622C9FF3ED0051A3E6 /* AssetRepository.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssetRepository.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -333,6 +337,7 @@
920824052CA4AE0A00388AB2 /* TransactionSplitRepository.swift */,
929EF66E2CA0BC2D0051A3E6 /* ScheduledRepository.swift */,
920824072CA4B05700388AB2 /* ScheduledSplitRepository.swift */,
920824192CA4D35700388AB2 /* AttachmentRepository.swift */,
9208240F2CA4C5CD00388AB2 /* BudgetYearRepository.swift */,
920824112CA4C6A400388AB2 /* BudgetTableRepository.swift */,
920824152CA4C95F00388AB2 /* ReportRepository.swift */,
Expand Down Expand Up @@ -403,6 +408,7 @@
920824012CA4AD0C00388AB2 /* TransactionSplitData.swift */,
929EF66C2CA0BA5E0051A3E6 /* ScheduledData.swift */,
920824032CA4ADC100388AB2 /* ScheduledSplitData.swift */,
920824172CA4CF4300388AB2 /* AttachmentData.swift */,
920824092CA4C2AD00388AB2 /* BudgetYearData.swift */,
9208240B2CA4C31B00388AB2 /* BudgetTableData.swift */,
9208240D2CA4C4FA00388AB2 /* ReportData.swift */,
Expand Down Expand Up @@ -560,6 +566,7 @@
9208240A2CA4C2AD00388AB2 /* BudgetYearData.swift in Sources */,
A3363EEB2C93BF62004696C7 /* CurrencyRepository.swift in Sources */,
A37E7D942C9B217500B4ECFC /* InfotableData.swift in Sources */,
9208241A2CA4D35700388AB2 /* AttachmentRepository.swift in Sources */,
A37E7D882C9AC2E600B4ECFC /* VersionInfoView.swift in Sources */,
920823F62CA498A200388AB2 /* CurrencyHistoryData.swift in Sources */,
A3462F662C94854800F79145 /* ExportableEntity.swift in Sources */,
Expand All @@ -585,6 +592,7 @@
A3C142A42C9033E300D3CEC0 /* SettingsView.swift in Sources */,
A3363EE52C9323D2004696C7 /* CategoryDetailView.swift in Sources */,
929EF66F2CA0BC2D0051A3E6 /* ScheduledRepository.swift in Sources */,
920824182CA4CF4300388AB2 /* AttachmentData.swift in Sources */,
A37E7D842C9AC14800B4ECFC /* ManagementView.swift in Sources */,
920824162CA4C95F00388AB2 /* ReportRepository.swift in Sources */,
A37E7D902C9AC4F300B4ECFC /* TermsOfServiceView.swift in Sources */,
Expand Down
42 changes: 42 additions & 0 deletions MMEX/Models/AttachmentData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Attachment.swift
// MMEX
//
// Created 2024-09-26 by George Ef ([email protected])
//

import Foundation
import SQLite

enum RefType: String, EnumCollateNoCase {
case transaction = "Transaction"
case stock = "Stock"
case asset = "Asset"
case account = "BankAccount"
case scheduled = "RecurringTransaction"
case payee = "Payee"
case transactionSplit = "TransactionSplit"
case scheduledSplit = "RecurringTransactionSplit"
static let defaultValue = Self.transaction
}

struct AttachmentData: ExportableEntity {
var id : Int64 = 0
var refType : RefType = RefType.defaultValue
var refId : Int64 = 0
var description : String = ""
var filename : String = ""
}

extension AttachmentData: DataProtocol {
static let dataName = "Attachment"

func shortDesc() -> String {
"\(self.id)"
}
}

extension AttachmentData {
static let sampleData: [AttachmentData] = [
]
}
16 changes: 8 additions & 8 deletions MMEX/Models/BudgetTableData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation
import SQLite

enum Period: String, EnumCollateNoCase {
enum BudgetPeriod: String, EnumCollateNoCase {
case none = "None"
case weekly = "Weekly"
case biweekly = "Fortnightly"
Expand All @@ -22,13 +22,13 @@ enum Period: String, EnumCollateNoCase {
}

struct BudgetTableData: ExportableEntity {
var id : Int64 = 0
var yearId : Int64 = 0
var categId : Int64 = 0
var period : Period = Period.defaultValue
var amount : Double = 0.0
var notes : String = ""
var active : Bool = false
var id : Int64 = 0
var yearId : Int64 = 0
var categId : Int64 = 0
var period : BudgetPeriod = BudgetPeriod.defaultValue
var amount : Double = 0.0
var notes : String = ""
var active : Bool = false
}

extension BudgetTableData: DataProtocol {
Expand Down
74 changes: 74 additions & 0 deletions MMEX/Repositories/AttachmentRepository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// AttachmentRepository.swift
// MMEX
//
// Created 2024-09-26 by George Ef ([email protected])
//

import Foundation
import SQLite

class AttachmentRepository: RepositoryProtocol {
typealias RepositoryData = AttachmentData

let db: Connection?
init(db: Connection?) {
self.db = db
}

static let repositoryName = "ATTACHMENT_V1"
static let table = SQLite.Table(repositoryName)

// column | type | other
// -------------+---------+------
// ATTACHMENTID | INTEGER | PRIMARY KEY
// REFTYPE | TEXT | NOT NULL (BankAccount, Asset, Stock, ...)
// REFID | INTEGER | NOT NULL
// DESCRIPTION | TEXT | COLLATE NOCASE
// FILENAME | TEXT | NOT NULL COLLATE NOCASE

// column expressions
static let col_id = SQLite.Expression<Int64>("ATTACHMENTID")
static let col_refType = SQLite.Expression<String>("REFTYPE")
static let col_refId = SQLite.Expression<Int64>("REFID")
static let col_description = SQLite.Expression<String?>("DESCRIPTION")
static let col_filename = SQLite.Expression<String>("FILENAME")

static func selectQuery(from table: SQLite.Table) -> SQLite.Table {
return table.select(
col_id,
col_refType,
col_refId,
col_description,
col_filename
)
}

static func selectData(_ row: SQLite.Row) -> AttachmentData {
return AttachmentData(
id : row[col_id],
refType : RefType(collateNoCase: row[col_refType]),
refId : row[col_refId],
description : row[col_description] ?? "",
filename : row[col_filename]
)
}

static func itemSetters(_ attachment: AttachmentData) -> [SQLite.Setter] {
return [
col_refType <- attachment.refType.rawValue,
col_refId <- attachment.refId,
col_description <- attachment.description,
col_filename <- attachment.filename
]
}
}

extension AttachmentRepository {
// load all attachments
func load() -> [AttachmentData] {
return select(from: Self.table
.order(Self.col_id)
)
}
}
2 changes: 1 addition & 1 deletion MMEX/Repositories/BudgetTableRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class BudgetTableRepository: RepositoryProtocol {
id : row[col_id],
yearId : row[col_yearId] ?? -1,
categId : row[col_categId] ?? -1,
period : Period(collateNoCase: row[col_period]),
period : BudgetPeriod(collateNoCase: row[col_period]),
amount : row[cast_amount],
notes : row[col_notes] ?? "",
active : row[col_active] ?? 0 != 0
Expand Down
21 changes: 21 additions & 0 deletions MMEX/Repositories/Repository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,27 @@ extension Repository {
}
}

var attachmentMap: [Int64: Int64] = [:]
do {
let repo = AttachmentRepository(db: db)
repo.deleteAll()
for var data in AttachmentData.sampleData {
let id = data.id
data.refId = switch data.refType {
case .transaction : transactionMap[data.refId] ?? data.refId
case .stock : stockMap[data.refId] ?? data.refId
case .asset : assetMap[data.refId] ?? data.refId
case .account : accountMap[data.refId] ?? data.refId
case .scheduled : scheduledMap[data.refId] ?? data.refId
case .payee : payeeMap[data.refId] ?? data.refId
case .transactionSplit : transactionSplitMap[data.refId] ?? data.refId
case .scheduledSplit : scheduledSplitMap[data.refId] ?? data.refId
}
repo.insert(&data)
attachmentMap[id] = data.id
}
}

var budgetTableMap: [Int64: Int64] = [:]
do {
let repo = BudgetTableRepository(db: db)
Expand Down