code refactor

This commit is contained in:
MaysWind
2021-03-10 23:33:18 +08:00
parent cd7756013b
commit ac6d449227
4 changed files with 54 additions and 54 deletions
+6 -6
View File
@@ -21,16 +21,16 @@ type LatestExchangeRate struct {
type LatestExchangeRateSlice []*LatestExchangeRate type LatestExchangeRateSlice []*LatestExchangeRate
// Len returns the count of items // Len returns the count of items
func (c LatestExchangeRateSlice) Len() int { func (s LatestExchangeRateSlice) Len() int {
return len(c) return len(s)
} }
// Swap swaps two items // Swap swaps two items
func (c LatestExchangeRateSlice) Swap(i, j int) { func (s LatestExchangeRateSlice) Swap(i, j int) {
c[i], c[j] = c[j], c[i] s[i], s[j] = s[j], s[i]
} }
// Less reports whether the first item is less than the second one // Less reports whether the first item is less than the second one
func (c LatestExchangeRateSlice) Less(i, j int) bool { func (s LatestExchangeRateSlice) Less(i, j int) bool {
return strings.Compare(c[i].Currency, c[j].Currency) < 0 return strings.Compare(s[i].Currency, s[j].Currency) < 0
} }
+31 -31
View File
@@ -133,12 +133,12 @@ type TransactionInfoPageWrapperResponse struct {
} }
// IsEditable returns whether this transaction can be edited // IsEditable returns whether this transaction can be edited
func (c *Transaction) IsEditable(account *Account, relatedAccount *Account) bool { func (t *Transaction) IsEditable(account *Account, relatedAccount *Account) bool {
if account == nil || account.Hidden { if account == nil || account.Hidden {
return false return false
} }
if c.Type == TRANSACTION_DB_TYPE_TRANSFER_OUT { if t.Type == TRANSACTION_DB_TYPE_TRANSFER_OUT {
if relatedAccount == nil || relatedAccount.Hidden { if relatedAccount == nil || relatedAccount.Hidden {
return false return false
} }
@@ -148,52 +148,52 @@ func (c *Transaction) IsEditable(account *Account, relatedAccount *Account) bool
} }
// ToTransactionInfoResponse returns a view-object according to database model // ToTransactionInfoResponse returns a view-object according to database model
func (c *Transaction) ToTransactionInfoResponse(tagIds []int64, editable bool) *TransactionInfoResponse { func (t *Transaction) ToTransactionInfoResponse(tagIds []int64, editable bool) *TransactionInfoResponse {
var transactionType TransactionType var transactionType TransactionType
if c.Type == TRANSACTION_DB_TYPE_MODIFY_BALANCE { if t.Type == TRANSACTION_DB_TYPE_MODIFY_BALANCE {
transactionType = TRANSACTION_TYPE_MODIFY_BALANCE transactionType = TRANSACTION_TYPE_MODIFY_BALANCE
} else if c.Type == TRANSACTION_DB_TYPE_EXPENSE { } else if t.Type == TRANSACTION_DB_TYPE_EXPENSE {
transactionType = TRANSACTION_TYPE_EXPENSE transactionType = TRANSACTION_TYPE_EXPENSE
} else if c.Type == TRANSACTION_DB_TYPE_INCOME { } else if t.Type == TRANSACTION_DB_TYPE_INCOME {
transactionType = TRANSACTION_TYPE_INCOME transactionType = TRANSACTION_TYPE_INCOME
} else if c.Type == TRANSACTION_DB_TYPE_TRANSFER_OUT { } else if t.Type == TRANSACTION_DB_TYPE_TRANSFER_OUT {
transactionType = TRANSACTION_TYPE_TRANSFER transactionType = TRANSACTION_TYPE_TRANSFER
} else if c.Type == TRANSACTION_DB_TYPE_TRANSFER_IN { } else if t.Type == TRANSACTION_DB_TYPE_TRANSFER_IN {
transactionType = TRANSACTION_TYPE_TRANSFER transactionType = TRANSACTION_TYPE_TRANSFER
} else { } else {
return nil return nil
} }
sourceAccountId := c.AccountId sourceAccountId := t.AccountId
sourceAmount := c.Amount sourceAmount := t.Amount
destinationAccountId := int64(0) destinationAccountId := int64(0)
destinationAmount := int64(0) destinationAmount := int64(0)
if c.Type == TRANSACTION_DB_TYPE_TRANSFER_OUT { if t.Type == TRANSACTION_DB_TYPE_TRANSFER_OUT {
destinationAccountId = c.RelatedAccountId destinationAccountId = t.RelatedAccountId
destinationAmount = c.RelatedAccountAmount destinationAmount = t.RelatedAccountAmount
} else if c.Type == TRANSACTION_DB_TYPE_TRANSFER_IN { } else if t.Type == TRANSACTION_DB_TYPE_TRANSFER_IN {
sourceAccountId = c.RelatedAccountId sourceAccountId = t.RelatedAccountId
sourceAmount = c.RelatedAccountAmount sourceAmount = t.RelatedAccountAmount
destinationAccountId = c.AccountId destinationAccountId = t.AccountId
destinationAmount = c.Amount destinationAmount = t.Amount
} }
return &TransactionInfoResponse{ return &TransactionInfoResponse{
Id: c.TransactionId, Id: t.TransactionId,
TimeSequenceId: c.TransactionTime, TimeSequenceId: t.TransactionTime,
Type: transactionType, Type: transactionType,
CategoryId: c.CategoryId, CategoryId: t.CategoryId,
Time: utils.GetUnixTimeFromTransactionTime(c.TransactionTime), Time: utils.GetUnixTimeFromTransactionTime(t.TransactionTime),
SourceAccountId: sourceAccountId, SourceAccountId: sourceAccountId,
DestinationAccountId: destinationAccountId, DestinationAccountId: destinationAccountId,
SourceAmount: sourceAmount, SourceAmount: sourceAmount,
DestinationAmount: destinationAmount, DestinationAmount: destinationAmount,
TagIds: utils.Int64ArrayToStringArray(tagIds), TagIds: utils.Int64ArrayToStringArray(tagIds),
Comment: c.Comment, Comment: t.Comment,
Editable: editable, Editable: editable,
} }
} }
@@ -202,20 +202,20 @@ func (c *Transaction) ToTransactionInfoResponse(tagIds []int64, editable bool) *
type TransactionInfoResponseSlice []*TransactionInfoResponse type TransactionInfoResponseSlice []*TransactionInfoResponse
// Len returns the count of items // Len returns the count of items
func (c TransactionInfoResponseSlice) Len() int { func (s TransactionInfoResponseSlice) Len() int {
return len(c) return len(s)
} }
// Swap swaps two items // Swap swaps two items
func (c TransactionInfoResponseSlice) Swap(i, j int) { func (s TransactionInfoResponseSlice) Swap(i, j int) {
c[i], c[j] = c[j], c[i] s[i], s[j] = s[j], s[i]
} }
// Less reports whether the first item is less than the second one // Less reports whether the first item is less than the second one
func (c TransactionInfoResponseSlice) Less(i, j int) bool { func (s TransactionInfoResponseSlice) Less(i, j int) bool {
if c[i].Time != c[j].Time { if s[i].Time != s[j].Time {
return c[i].Time > c[j].Time return s[i].Time > s[j].Time
} }
return c[i].Id > c[j].Id return s[i].Id > s[j].Id
} }
+6 -6
View File
@@ -132,16 +132,16 @@ func (c *TransactionCategory) ToTransactionCategoryInfoResponse() *TransactionCa
type TransactionCategoryInfoResponseSlice []*TransactionCategoryInfoResponse type TransactionCategoryInfoResponseSlice []*TransactionCategoryInfoResponse
// Len returns the count of items // Len returns the count of items
func (c TransactionCategoryInfoResponseSlice) Len() int { func (s TransactionCategoryInfoResponseSlice) Len() int {
return len(c) return len(s)
} }
// Swap swaps two items // Swap swaps two items
func (c TransactionCategoryInfoResponseSlice) Swap(i, j int) { func (s TransactionCategoryInfoResponseSlice) Swap(i, j int) {
c[i], c[j] = c[j], c[i] s[i], s[j] = s[j], s[i]
} }
// Less reports whether the first item is less than the second one // Less reports whether the first item is less than the second one
func (c TransactionCategoryInfoResponseSlice) Less(i, j int) bool { func (s TransactionCategoryInfoResponseSlice) Less(i, j int) bool {
return c[i].DisplayOrder < c[j].DisplayOrder return s[i].DisplayOrder < s[j].DisplayOrder
} }
+11 -11
View File
@@ -60,12 +60,12 @@ type TransactionTagInfoResponse struct {
} }
// ToTransactionTagInfoResponse returns a view-object according to database model // ToTransactionTagInfoResponse returns a view-object according to database model
func (c *TransactionTag) ToTransactionTagInfoResponse() *TransactionTagInfoResponse { func (t *TransactionTag) ToTransactionTagInfoResponse() *TransactionTagInfoResponse {
return &TransactionTagInfoResponse{ return &TransactionTagInfoResponse{
Id: c.TagId, Id: t.TagId,
Name: c.Name, Name: t.Name,
DisplayOrder: c.DisplayOrder, DisplayOrder: t.DisplayOrder,
Hidden: c.Hidden, Hidden: t.Hidden,
} }
} }
@@ -73,16 +73,16 @@ func (c *TransactionTag) ToTransactionTagInfoResponse() *TransactionTagInfoRespo
type TransactionTagInfoResponseSlice []*TransactionTagInfoResponse type TransactionTagInfoResponseSlice []*TransactionTagInfoResponse
// Len returns the count of items // Len returns the count of items
func (c TransactionTagInfoResponseSlice) Len() int { func (s TransactionTagInfoResponseSlice) Len() int {
return len(c) return len(s)
} }
// Swap swaps two items // Swap swaps two items
func (c TransactionTagInfoResponseSlice) Swap(i, j int) { func (s TransactionTagInfoResponseSlice) Swap(i, j int) {
c[i], c[j] = c[j], c[i] s[i], s[j] = s[j], s[i]
} }
// Less reports whether the first item is less than the second one // Less reports whether the first item is less than the second one
func (c TransactionTagInfoResponseSlice) Less(i, j int) bool { func (s TransactionTagInfoResponseSlice) Less(i, j int) bool {
return c[i].DisplayOrder < c[j].DisplayOrder return s[i].DisplayOrder < s[j].DisplayOrder
} }