feat(marketing): add user logo management module
Users can now save uploaded logos to the backend (marketing_user_logos table), avoiding repeated uploads. Includes CRUD endpoints: list, save, delete with a per-user limit of 10 logos. Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"wx_service/internal/marketing/model"
|
||||
)
|
||||
|
||||
var ErrUserLogoNotFound = errors.New("user logo not found")
|
||||
|
||||
type UserLogoRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewUserLogoRepository(db *gorm.DB) *UserLogoRepository {
|
||||
return &UserLogoRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *UserLogoRepository) Create(logo *model.UserLogo) error {
|
||||
if err := r.db.Create(logo).Error; err != nil {
|
||||
return fmt.Errorf("create user logo: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *UserLogoRepository) FindByUser(userID uint) ([]model.UserLogo, error) {
|
||||
var logos []model.UserLogo
|
||||
err := r.db.Where("user_id = ?", userID).Order("id DESC").Find(&logos).Error
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list user logos: %w", err)
|
||||
}
|
||||
return logos, nil
|
||||
}
|
||||
|
||||
func (r *UserLogoRepository) FindByID(id, userID uint) (*model.UserLogo, error) {
|
||||
var logo model.UserLogo
|
||||
err := r.db.Where("id = ? AND user_id = ?", id, userID).First(&logo).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrUserLogoNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("find user logo: %w", err)
|
||||
}
|
||||
return &logo, nil
|
||||
}
|
||||
|
||||
func (r *UserLogoRepository) Delete(id, userID uint) error {
|
||||
tx := r.db.Where("id = ? AND user_id = ?", id, userID).Delete(&model.UserLogo{})
|
||||
if tx.Error != nil {
|
||||
return fmt.Errorf("delete user logo: %w", tx.Error)
|
||||
}
|
||||
if tx.RowsAffected == 0 {
|
||||
return ErrUserLogoNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *UserLogoRepository) CountByUser(userID uint) (int64, error) {
|
||||
var count int64
|
||||
err := r.db.Model(&model.UserLogo{}).Where("user_id = ?", userID).Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
Reference in New Issue
Block a user