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 @discardableResult in Repository #41

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
4 changes: 2 additions & 2 deletions MMEX/Repositories/InfotableRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ extension InfotableRepository {
) {
// Update existing setting
info.value = stringValue
_ = update(info)
update(info)
} else {
// Insert new setting
var info = InfotableData(id: 0, name: key, value: stringValue)
_ = insert(&info)
insert(&info)
}
}
}
40 changes: 22 additions & 18 deletions MMEX/Repositories/Repository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ extension RepositoryProtocol {
}
}

@discardableResult
func insert(_ data: inout RepositoryData) -> Bool {
guard let db else { return false }
do {
Expand All @@ -114,6 +115,7 @@ extension RepositoryProtocol {
}
}

@discardableResult
func update(_ data: RepositoryData) -> Bool {
guard let db else { return false }
guard data.id > 0 else { return false }
Expand All @@ -130,6 +132,7 @@ extension RepositoryProtocol {
}
}

@discardableResult
func delete(_ data: RepositoryData) -> Bool {
guard let db else { return false }
guard data.id > 0 else { return false }
Expand All @@ -146,6 +149,7 @@ extension RepositoryProtocol {
}
}

@discardableResult
func deleteAll() -> Bool {
guard let db else { return false }
do {
Expand All @@ -166,111 +170,111 @@ extension Repository {
var infotableMap: [Int64: Int64] = [:]
do {
let repo = InfotableRepository(db: db)
_ = repo.deleteAll()
repo.deleteAll()
for var data in InfotableData.sampleData {
let id = data.id
_ = repo.insert(&data)
repo.insert(&data)
infotableMap[id] = data.id
}
}

var currencyMap: [Int64: Int64] = [:]
do {
let repo = CurrencyRepository(db: db)
_ = repo.deleteAll()
repo.deleteAll()
for var data in CurrencyData.sampleData {
let id = data.id
_ = repo.insert(&data)
repo.insert(&data)
currencyMap[id] = data.id
}
}

var accountMap: [Int64: Int64] = [:]
do {
let repo = AccountRepository(db: db)
_ = repo.deleteAll()
repo.deleteAll()
for var data in AccountData.sampleData {
let id = data.id
data.currencyId = currencyMap[data.currencyId] ?? data.currencyId
_ = repo.insert(&data)
repo.insert(&data)
accountMap[id] = data.id
}
}

var assetMap: [Int64: Int64] = [:]
do {
let repo = AssetRepository(db: db)
_ = repo.deleteAll()
repo.deleteAll()
for var data in AssetData.sampleData {
let id = data.id
data.currencyId = currencyMap[data.currencyId] ?? data.currencyId
_ = repo.insert(&data)
repo.insert(&data)
assetMap[id] = data.id
}
}

var stockMap: [Int64: Int64] = [:]
do {
let repo = StockRepository(db: db)
_ = repo.deleteAll()
repo.deleteAll()
for var data in StockData.sampleData {
let id = data.id
data.accountId = accountMap[data.accountId] ?? data.accountId
_ = repo.insert(&data)
repo.insert(&data)
stockMap[id] = data.id
}
}

var categoryMap: [Int64: Int64] = [:]
do {
let repo = CategoryRepository(db: db)
_ = repo.deleteAll()
repo.deleteAll()
for var data in CategoryData.sampleData {
let id = data.id
data.parentId = categoryMap[data.parentId] ?? data.parentId
_ = repo.insert(&data)
repo.insert(&data)
categoryMap[id] = data.id
}
}

var payeeMap: [Int64: Int64] = [:]
do {
let repo = PayeeRepository(db: db)
_ = repo.deleteAll()
repo.deleteAll()
for var data in PayeeData.sampleData {
let id = data.id
data.categoryId = categoryMap[data.categoryId] ?? data.categoryId
_ = repo.insert(&data)
repo.insert(&data)
payeeMap[id] = data.id
}
}

var transactionMap: [Int64: Int64] = [:]
do {
let repo = TransactionRepository(db: db)
_ = repo.deleteAll()
repo.deleteAll()
for var data in TransactionData.sampleData {
let id = data.id
data.accountId = accountMap[data.accountId] ?? data.accountId
data.toAccountId = accountMap[data.toAccountId] ?? data.toAccountId
data.payeeId = payeeMap[data.payeeId] ?? data.payeeId
data.categId = categoryMap[data.categId] ?? data.categId
_ = repo.insert(&data)
repo.insert(&data)
transactionMap[id] = data.id
}
}

var scheduledMap: [Int64: Int64] = [:]
do {
let repo = ScheduledRepository(db: db)
_ = repo.deleteAll()
repo.deleteAll()
for var data in ScheduledData.sampleData {
let id = data.id
data.accountId = accountMap[data.accountId] ?? data.accountId
data.toAccountId = accountMap[data.toAccountId] ?? data.toAccountId
data.payeeId = payeeMap[data.payeeId] ?? data.payeeId
data.categId = categoryMap[data.categId] ?? data.categId
_ = repo.insert(&data)
repo.insert(&data)
scheduledMap[id] = data.id
}
}
Expand Down