120 lines
3.1 KiB
Go
120 lines
3.1 KiB
Go
package repository
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"accounting-app/internal/models"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// User preference repository errors
|
|
var (
|
|
ErrUserPreferenceNotFound = errors.New("user preference not found")
|
|
)
|
|
|
|
// UserPreferenceRepository handles database operations for user preferences
|
|
type UserPreferenceRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewUserPreferenceRepository creates a new UserPreferenceRepository instance
|
|
func NewUserPreferenceRepository(db *gorm.DB) *UserPreferenceRepository {
|
|
return &UserPreferenceRepository{db: db}
|
|
}
|
|
|
|
// Create creates a new user preference record
|
|
func (r *UserPreferenceRepository) Create(pref *models.UserPreference) error {
|
|
return r.db.Create(pref).Error
|
|
}
|
|
|
|
// GetByUserID retrieves user preference by user ID
|
|
func (r *UserPreferenceRepository) GetByUserID(userID uint) (*models.UserPreference, error) {
|
|
var pref models.UserPreference
|
|
err := r.db.Where("user_id = ?", userID).First(&pref).Error
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrUserPreferenceNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &pref, nil
|
|
}
|
|
|
|
// GetOrCreate retrieves user preference or creates a new one if not exists
|
|
func (r *UserPreferenceRepository) GetOrCreate(userID uint) (*models.UserPreference, error) {
|
|
pref, err := r.GetByUserID(userID)
|
|
if err == nil {
|
|
return pref, nil
|
|
}
|
|
if !errors.Is(err, ErrUserPreferenceNotFound) {
|
|
return nil, err
|
|
}
|
|
|
|
// Create new preference
|
|
newPref := &models.UserPreference{
|
|
UserID: &userID,
|
|
}
|
|
if err := r.Create(newPref); err != nil {
|
|
return nil, err
|
|
}
|
|
return newPref, nil
|
|
}
|
|
|
|
// Update updates an existing user preference
|
|
func (r *UserPreferenceRepository) Update(pref *models.UserPreference) error {
|
|
return r.db.Save(pref).Error
|
|
}
|
|
|
|
// UpdateLastAccount updates the last used account ID
|
|
func (r *UserPreferenceRepository) UpdateLastAccount(userID uint, accountID uint) error {
|
|
pref, err := r.GetOrCreate(userID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pref.LastAccountID = &accountID
|
|
return r.Update(pref)
|
|
}
|
|
|
|
// UpdateLastCategory updates the last used category ID
|
|
func (r *UserPreferenceRepository) UpdateLastCategory(userID uint, categoryID uint) error {
|
|
pref, err := r.GetOrCreate(userID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pref.LastCategoryID = &categoryID
|
|
return r.Update(pref)
|
|
}
|
|
|
|
// UpdateFrequentAccounts updates the frequent accounts list
|
|
func (r *UserPreferenceRepository) UpdateFrequentAccounts(userID uint, accountsJSON string) error {
|
|
pref, err := r.GetOrCreate(userID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pref.FrequentAccounts = accountsJSON
|
|
return r.Update(pref)
|
|
}
|
|
|
|
// UpdateFrequentCategories updates the frequent categories list
|
|
func (r *UserPreferenceRepository) UpdateFrequentCategories(userID uint, categoriesJSON string) error {
|
|
pref, err := r.GetOrCreate(userID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pref.FrequentCategories = categoriesJSON
|
|
return r.Update(pref)
|
|
}
|
|
|
|
// Delete deletes a user preference record
|
|
func (r *UserPreferenceRepository) Delete(userID uint) error {
|
|
result := r.db.Where("user_id = ?", userID).Delete(&models.UserPreference{})
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
if result.RowsAffected == 0 {
|
|
return ErrUserPreferenceNotFound
|
|
}
|
|
return nil
|
|
}
|