feat: 完成后端API路由、交易服务和数据库模式的初始搭建

This commit is contained in:
2026-01-30 15:40:50 +08:00
parent c4d7825328
commit 8518b35d7f
3 changed files with 40 additions and 2 deletions

View File

@@ -70,7 +70,7 @@ func Setup(db *gorm.DB, yunAPIClient *service.YunAPIClient, cfg *config.Config)
categoryService := service.NewCategoryService(categoryRepo)
tagService := service.NewTagService(tagRepo)
classificationService := service.NewClassificationService(classificationRepo, categoryRepo)
transactionService := service.NewTransactionService(transactionRepo, accountRepo, categoryRepo, tagRepo, db)
transactionService := service.NewTransactionService(transactionRepo, accountRepo, categoryRepo, tagRepo, ledgerRepo, db)
imageService := service.NewImageService(transactionImageRepo, transactionRepo, db, cfg.ImageUploadDir)
recurringService := service.NewRecurringTransactionService(recurringRepo, transactionRepo, accountRepo, categoryRepo, allocationRuleRepo, allocationRecordRepo, piggyBankRepo, db)
exchangeRateService := service.NewExchangeRateService(exchangeRateRepo)
@@ -347,7 +347,7 @@ func SetupWithRedis(db *gorm.DB, yunAPIClient *service.YunAPIClient, redisClient
categoryService := service.NewCategoryService(categoryRepo)
tagService := service.NewTagService(tagRepo)
classificationService := service.NewClassificationService(classificationRepo, categoryRepo)
transactionService := service.NewTransactionService(transactionRepo, accountRepo, categoryRepo, tagRepo, db)
transactionService := service.NewTransactionService(transactionRepo, accountRepo, categoryRepo, tagRepo, ledgerRepo, db)
imageService := service.NewImageService(transactionImageRepo, transactionRepo, db, cfg.ImageUploadDir)
recurringService := service.NewRecurringTransactionService(recurringRepo, transactionRepo, accountRepo, categoryRepo, allocationRuleRepo, allocationRecordRepo, piggyBankRepo, db)
reportService := service.NewReportService(reportRepo, exchangeRateRepo)

View File

@@ -39,6 +39,7 @@ type TransactionInput struct {
ImagePath string `json:"image_path,omitempty"`
ToAccountID *uint `json:"to_account_id,omitempty"`
TagIDs []uint `json:"tag_ids,omitempty"`
LedgerID *uint `json:"ledger_id,omitempty"`
}
// TransactionListInput represents the input for listing transactions
@@ -65,6 +66,7 @@ type TransactionService struct {
accountRepo *repository.AccountRepository
categoryRepo *repository.CategoryRepository
tagRepo *repository.TagRepository
ledgerRepo *repository.LedgerRepository
db *gorm.DB
}
@@ -74,6 +76,7 @@ func NewTransactionService(
accountRepo *repository.AccountRepository,
categoryRepo *repository.CategoryRepository,
tagRepo *repository.TagRepository,
ledgerRepo *repository.LedgerRepository,
db *gorm.DB,
) *TransactionService {
return &TransactionService{
@@ -81,6 +84,7 @@ func NewTransactionService(
accountRepo: accountRepo,
categoryRepo: categoryRepo,
tagRepo: tagRepo,
ledgerRepo: ledgerRepo,
db: db,
}
}
@@ -160,6 +164,23 @@ func (s *TransactionService) CreateTransaction(userID uint, input TransactionInp
return nil, err
}
// If LedgerID is not provided, use the default ledger
var ledgerID uint
if input.LedgerID == nil || *input.LedgerID == 0 {
defaultLedger, err := s.ledgerRepo.GetDefault(userID)
if err != nil {
return nil, fmt.Errorf("failed to get default ledger: %w", err)
}
ledgerID = defaultLedger.ID
} else {
ledgerID = *input.LedgerID
// Verify ledger exists
_, err := s.ledgerRepo.GetByID(userID, ledgerID)
if err != nil {
return nil, fmt.Errorf("failed to verify ledger: %w", err)
}
}
// Execute within a database transaction
var transaction *models.Transaction
err := s.db.Transaction(func(tx *gorm.DB) error {
@@ -216,6 +237,7 @@ func (s *TransactionService) CreateTransaction(userID uint, input TransactionInp
Note: input.Note,
ImagePath: input.ImagePath,
ToAccountID: input.ToAccountID,
LedgerID: &ledgerID,
}
// Save transaction with tags
@@ -414,6 +436,14 @@ func (s *TransactionService) UpdateTransaction(userID, id uint, input Transactio
transaction.Note = input.Note
transaction.ImagePath = input.ImagePath
transaction.ToAccountID = input.ToAccountID
if input.LedgerID != nil && *input.LedgerID > 0 {
// Verify ledger exists
_, err := s.ledgerRepo.GetByID(userID, *input.LedgerID)
if err != nil {
return fmt.Errorf("failed to verify ledger: %w", err)
}
transaction.LedgerID = input.LedgerID
}
if err := txTransactionRepo.UpdateWithTags(transaction, input.TagIDs); err != nil {
return fmt.Errorf("failed to update transaction: %w", err)