init
This commit is contained in:
84
internal/repository/template_repository.go
Normal file
84
internal/repository/template_repository.go
Normal file
@@ -0,0 +1,84 @@
|
||||
// Package repository provides data access layer for the application
|
||||
package repository
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"accounting-app/internal/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Template repository errors
|
||||
var (
|
||||
ErrTemplateNotFound = errors.New("template not found")
|
||||
)
|
||||
|
||||
// TemplateRepository handles database operations for transaction templates
|
||||
// Feature: api-interface-optimization
|
||||
// Validates: Requirements 15.1, 15.2
|
||||
type TemplateRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewTemplateRepository creates a new TemplateRepository instance
|
||||
func NewTemplateRepository(db *gorm.DB) *TemplateRepository {
|
||||
return &TemplateRepository{db: db}
|
||||
}
|
||||
|
||||
// Create creates a new transaction template
|
||||
func (r *TemplateRepository) Create(template *models.TransactionTemplate) error {
|
||||
return r.db.Create(template).Error
|
||||
}
|
||||
|
||||
// GetByID retrieves a template by ID
|
||||
func (r *TemplateRepository) GetByID(userID uint, id uint) (*models.TransactionTemplate, error) {
|
||||
var template models.TransactionTemplate
|
||||
if err := r.db.Where("user_id = ?", userID).Preload("Category").Preload("Account").First(&template, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrTemplateNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &template, nil
|
||||
}
|
||||
|
||||
// GetAll retrieves all templates for a user
|
||||
func (r *TemplateRepository) GetAll(userID uint) ([]models.TransactionTemplate, error) {
|
||||
var templates []models.TransactionTemplate
|
||||
query := r.db.Where("user_id = ?", userID).Preload("Category").Preload("Account").Order("sort_order ASC, created_at DESC")
|
||||
|
||||
if err := query.Find(&templates).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return templates, nil
|
||||
}
|
||||
|
||||
// Update updates a template
|
||||
func (r *TemplateRepository) Update(template *models.TransactionTemplate) error {
|
||||
return r.db.Save(template).Error
|
||||
}
|
||||
|
||||
// Delete deletes a template
|
||||
func (r *TemplateRepository) Delete(userID uint, id uint) error {
|
||||
result := r.db.Where("user_id = ?", userID).Delete(&models.TransactionTemplate{}, id)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return ErrTemplateNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateSortOrder updates the sort order of templates
|
||||
func (r *TemplateRepository) UpdateSortOrder(userID uint, ids []uint) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
for i, id := range ids {
|
||||
if err := tx.Model(&models.TransactionTemplate{}).Where("id = ? AND user_id = ?", id, userID).Update("sort_order", i).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user