modify package and class name

This commit is contained in:
MaysWind
2021-04-18 00:00:50 +08:00
parent d5cb452c1f
commit bd9c8c7890
5 changed files with 48 additions and 48 deletions
+4 -4
View File
@@ -5,9 +5,9 @@ import (
"strings" "strings"
"time" "time"
"github.com/mayswind/ezbookkeeping/pkg/converters"
"github.com/mayswind/ezbookkeeping/pkg/core" "github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/errs" "github.com/mayswind/ezbookkeeping/pkg/errs"
"github.com/mayswind/ezbookkeeping/pkg/exporters"
"github.com/mayswind/ezbookkeeping/pkg/log" "github.com/mayswind/ezbookkeeping/pkg/log"
"github.com/mayswind/ezbookkeeping/pkg/models" "github.com/mayswind/ezbookkeeping/pkg/models"
"github.com/mayswind/ezbookkeeping/pkg/services" "github.com/mayswind/ezbookkeeping/pkg/services"
@@ -19,7 +19,7 @@ const pageCountForDataExport = 1000
// DataManagementsApi represents data management api // DataManagementsApi represents data management api
type DataManagementsApi struct { type DataManagementsApi struct {
exporter *exporters.CSVFileExporter exporter *converters.EzBookKeepingCSVFileExporter
tokens *services.TokenService tokens *services.TokenService
users *services.UserService users *services.UserService
accounts *services.AccountService accounts *services.AccountService
@@ -31,7 +31,7 @@ type DataManagementsApi struct {
// Initialize a data management api singleton instance // Initialize a data management api singleton instance
var ( var (
DataManagements = &DataManagementsApi{ DataManagements = &DataManagementsApi{
exporter: &exporters.CSVFileExporter{}, exporter: &converters.EzBookKeepingCSVFileExporter{},
tokens: services.Tokens, tokens: services.Tokens,
users: services.Users, users: services.Users,
accounts: services.Accounts, accounts: services.Accounts,
@@ -97,7 +97,7 @@ func (a *DataManagementsApi) ExportDataHandler(c *core.Context) ([]byte, string,
return nil, "", errs.ErrOperationFailed return nil, "", errs.ErrOperationFailed
} }
result, err := a.exporter.GetOutputContent(uid, timezone, allTransactions, accountMap, categoryMap, tagMap, tagIndexs) result, err := a.exporter.ToExportedContent(uid, timezone, allTransactions, accountMap, categoryMap, tagMap, tagIndexs)
if err != nil { if err != nil {
log.ErrorfWithRequestId(c, "[data_managements.ExportDataHandler] failed to get csv format exported data for \"uid:%d\", because %s", uid, err.Error()) log.ErrorfWithRequestId(c, "[data_managements.ExportDataHandler] failed to get csv format exported data for \"uid:%d\", because %s", uid, err.Error())
+18 -18
View File
@@ -5,8 +5,8 @@ import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"github.com/mayswind/ezbookkeeping/pkg/converters"
"github.com/mayswind/ezbookkeeping/pkg/errs" "github.com/mayswind/ezbookkeeping/pkg/errs"
"github.com/mayswind/ezbookkeeping/pkg/exporters"
"github.com/mayswind/ezbookkeeping/pkg/log" "github.com/mayswind/ezbookkeeping/pkg/log"
"github.com/mayswind/ezbookkeeping/pkg/models" "github.com/mayswind/ezbookkeeping/pkg/models"
"github.com/mayswind/ezbookkeeping/pkg/services" "github.com/mayswind/ezbookkeeping/pkg/services"
@@ -18,27 +18,27 @@ const pageCountForDataExport = 1000
// UserDataCli represents user data cli // UserDataCli represents user data cli
type UserDataCli struct { type UserDataCli struct {
csvExporter *exporters.CSVFileExporter ezBookKeepingCsvExporter *converters.EzBookKeepingCSVFileExporter
accounts *services.AccountService accounts *services.AccountService
transactions *services.TransactionService transactions *services.TransactionService
categories *services.TransactionCategoryService categories *services.TransactionCategoryService
tags *services.TransactionTagService tags *services.TransactionTagService
users *services.UserService users *services.UserService
twoFactorAuthorizations *services.TwoFactorAuthorizationService twoFactorAuthorizations *services.TwoFactorAuthorizationService
tokens *services.TokenService tokens *services.TokenService
} }
// Initialize an user data cli singleton instance // Initialize an user data cli singleton instance
var ( var (
UserData = &UserDataCli{ UserData = &UserDataCli{
csvExporter: &exporters.CSVFileExporter{}, ezBookKeepingCsvExporter: &converters.EzBookKeepingCSVFileExporter{},
accounts: services.Accounts, accounts: services.Accounts,
transactions: services.Transactions, transactions: services.Transactions,
categories: services.TransactionCategories, categories: services.TransactionCategories,
tags: services.TransactionTags, tags: services.TransactionTags,
users: services.Users, users: services.Users,
twoFactorAuthorizations: services.TwoFactorAuthorizations, twoFactorAuthorizations: services.TwoFactorAuthorizations,
tokens: services.Tokens, tokens: services.Tokens,
} }
) )
@@ -417,7 +417,7 @@ func (l *UserDataCli) ExportTransaction(c *cli.Context, username string) ([]byte
return nil, err return nil, err
} }
result, err := l.csvExporter.GetOutputContent(uid, time.Local, allTransactions, accountMap, categoryMap, tagMap, tagIndexs) result, err := l.ezBookKeepingCsvExporter.ToExportedContent(uid, time.Local, allTransactions, accountMap, categoryMap, tagMap, tagIndexs)
if err != nil { if err != nil {
log.BootErrorf("[user_data.ExportTransaction] failed to get csv format exported data for \"%s\", because %s", username, err.Error()) log.BootErrorf("[user_data.ExportTransaction] failed to get csv format exported data for \"%s\", because %s", username, err.Error())
+13
View File
@@ -0,0 +1,13 @@
package converters
import (
"time"
"github.com/mayswind/ezbookkeeping/pkg/models"
)
// DataConverter defines the structure of data exporter
type DataConverter interface {
// ToExportedContent returns the exported data
ToExportedContent(uid int64, timezone *time.Location, transactions []*models.Transaction, accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, allTagIndexs map[int64][]int64) ([]byte, error)
}
@@ -1,4 +1,4 @@
package exporters package converters
import ( import (
"fmt" "fmt"
@@ -9,16 +9,16 @@ import (
"github.com/mayswind/ezbookkeeping/pkg/utils" "github.com/mayswind/ezbookkeeping/pkg/utils"
) )
// CSVFileExporter defines the structure of csv file exporter // EzBookKeepingCSVFileExporter defines the structure of csv file exporter
type CSVFileExporter struct { type EzBookKeepingCSVFileExporter struct {
DataExporter DataConverter
} }
const csvHeaderLine = "Time,Type,Category,Sub Category,Account,Amount,Account2,Account2 Amount,Tags,Comment\n" const csvHeaderLine = "Time,Type,Category,Sub Category,Account,Amount,Account2,Account2 Amount,Tags,Comment\n"
const csvDataLineFormat = "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" const csvDataLineFormat = "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n"
// GetOutputContent returns the exported csv data // ToExportedContent returns the exported csv data
func (e *CSVFileExporter) GetOutputContent(uid int64, timezone *time.Location, transactions []*models.Transaction, accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, allTagIndexs map[int64][]int64) ([]byte, error) { func (e *EzBookKeepingCSVFileExporter) ToExportedContent(uid int64, timezone *time.Location, transactions []*models.Transaction, accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, allTagIndexs map[int64][]int64) ([]byte, error) {
var ret strings.Builder var ret strings.Builder
ret.Grow(len(transactions) * 100) ret.Grow(len(transactions) * 100)
@@ -55,7 +55,7 @@ func (e *CSVFileExporter) GetOutputContent(uid int64, timezone *time.Location, t
return []byte(ret.String()), nil return []byte(ret.String()), nil
} }
func (e *CSVFileExporter) getTransactionTypeName(transactionDbType models.TransactionDbType) string { func (e *EzBookKeepingCSVFileExporter) getTransactionTypeName(transactionDbType models.TransactionDbType) string {
if transactionDbType == models.TRANSACTION_DB_TYPE_MODIFY_BALANCE { if transactionDbType == models.TRANSACTION_DB_TYPE_MODIFY_BALANCE {
return "Balance Modification" return "Balance Modification"
} else if transactionDbType == models.TRANSACTION_DB_TYPE_INCOME { } else if transactionDbType == models.TRANSACTION_DB_TYPE_INCOME {
@@ -69,7 +69,7 @@ func (e *CSVFileExporter) getTransactionTypeName(transactionDbType models.Transa
} }
} }
func (e *CSVFileExporter) getTransactionCategoryName(categoryId int64, categoryMap map[int64]*models.TransactionCategory) string { func (e *EzBookKeepingCSVFileExporter) getTransactionCategoryName(categoryId int64, categoryMap map[int64]*models.TransactionCategory) string {
category, exists := categoryMap[categoryId] category, exists := categoryMap[categoryId]
if !exists { if !exists {
@@ -89,7 +89,7 @@ func (e *CSVFileExporter) getTransactionCategoryName(categoryId int64, categoryM
return parentCategory.Name return parentCategory.Name
} }
func (e *CSVFileExporter) getTransactionSubCategoryName(categoryId int64, categoryMap map[int64]*models.TransactionCategory) string { func (e *EzBookKeepingCSVFileExporter) getTransactionSubCategoryName(categoryId int64, categoryMap map[int64]*models.TransactionCategory) string {
category, exists := categoryMap[categoryId] category, exists := categoryMap[categoryId]
if exists { if exists {
@@ -99,7 +99,7 @@ func (e *CSVFileExporter) getTransactionSubCategoryName(categoryId int64, catego
} }
} }
func (e *CSVFileExporter) getAccountName(accountId int64, accountMap map[int64]*models.Account) string { func (e *EzBookKeepingCSVFileExporter) getAccountName(accountId int64, accountMap map[int64]*models.Account) string {
account, exists := accountMap[accountId] account, exists := accountMap[accountId]
if exists { if exists {
@@ -109,7 +109,7 @@ func (e *CSVFileExporter) getAccountName(accountId int64, accountMap map[int64]*
} }
} }
func (e *CSVFileExporter) getDisplayAmount(amount int64) string { func (e *EzBookKeepingCSVFileExporter) getDisplayAmount(amount int64) string {
displayAmount := utils.Int64ToString(amount) displayAmount := utils.Int64ToString(amount)
integer := utils.SubString(displayAmount, 0, len(displayAmount)-2) integer := utils.SubString(displayAmount, 0, len(displayAmount)-2)
decimals := utils.SubString(displayAmount, -2, 2) decimals := utils.SubString(displayAmount, -2, 2)
@@ -129,7 +129,7 @@ func (e *CSVFileExporter) getDisplayAmount(amount int64) string {
return integer + "." + decimals return integer + "." + decimals
} }
func (e *CSVFileExporter) getTags(transactionId int64, allTagIndexs map[int64][]int64, tagMap map[int64]*models.TransactionTag) string { func (e *EzBookKeepingCSVFileExporter) getTags(transactionId int64, allTagIndexs map[int64][]int64, tagMap map[int64]*models.TransactionTag) string {
tagIndexs, exists := allTagIndexs[transactionId] tagIndexs, exists := allTagIndexs[transactionId]
if !exists { if !exists {
@@ -156,7 +156,7 @@ func (e *CSVFileExporter) getTags(transactionId int64, allTagIndexs map[int64][]
return ret.String() return ret.String()
} }
func (e *CSVFileExporter) getComment(comment string) string { func (e *EzBookKeepingCSVFileExporter) getComment(comment string) string {
comment = strings.Replace(comment, ",", " ", -1) comment = strings.Replace(comment, ",", " ", -1)
comment = strings.Replace(comment, "\r\n", " ", -1) comment = strings.Replace(comment, "\r\n", " ", -1)
comment = strings.Replace(comment, "\n", " ", -1) comment = strings.Replace(comment, "\n", " ", -1)
-13
View File
@@ -1,13 +0,0 @@
package exporters
import (
"time"
"github.com/mayswind/ezbookkeeping/pkg/models"
)
// DataExporter defines the structure of data exporter
type DataExporter interface {
// GetOutputContent returns the exported data
GetOutputContent(uid int64, timezone *time.Location, transactions []*models.Transaction, accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, allTagIndexs map[int64][]int64) ([]byte, error)
}