package repository import ( "evanpage-backend/internal/domain" "gorm.io/gorm" ) type BookmarkRepository struct { db *gorm.DB } func NewBookmarkRepository(db *gorm.DB) *BookmarkRepository { return &BookmarkRepository{db: db} } func (r *BookmarkRepository) Create(bm *domain.Bookmark) error { return r.db.Create(bm).Error } func (r *BookmarkRepository) BulkCreate(bms []*domain.Bookmark) error { if len(bms) == 0 { return nil } return r.db.Create(&bms).Error } func (r *BookmarkRepository) FindByID(id uint) (*domain.Bookmark, error) { var bm domain.Bookmark err := r.db.First(&bm, id).Error return &bm, err } func (r *BookmarkRepository) ListByUser(userID uint) ([]domain.Bookmark, error) { var bms []domain.Bookmark err := r.db.Where("user_id = ?", userID).Order("sort_order asc, created_at desc").Find(&bms).Error return bms, err } func (r *BookmarkRepository) ListByUserAndCategory(userID uint, category string) ([]domain.Bookmark, error) { var bms []domain.Bookmark err := r.db.Where("user_id = ? AND category = ?", userID, category).Order("sort_order asc, created_at desc").Find(&bms).Error return bms, err } func (r *BookmarkRepository) ListCategoriesByUser(userID uint) ([]string, error) { var categories []string err := r.db.Model(&domain.Bookmark{}).Where("user_id = ?", userID).Distinct().Pluck("category", &categories).Error return categories, err } func (r *BookmarkRepository) Update(bm *domain.Bookmark) error { return r.db.Save(bm).Error } func (r *BookmarkRepository) Delete(id uint) error { return r.db.Delete(&domain.Bookmark{}, id).Error } func (r *BookmarkRepository) BulkDeleteByUser(userID uint, ids []uint) (int64, error) { if len(ids) == 0 { return 0, nil } res := r.db.Where("user_id = ? AND id IN ?", userID, ids).Delete(&domain.Bookmark{}) return res.RowsAffected, res.Error } func (r *BookmarkRepository) BulkUpdateCategoryByUser(userID uint, ids []uint, category string) (int64, error) { if len(ids) == 0 { return 0, nil } res := r.db.Model(&domain.Bookmark{}).Where("user_id = ? AND id IN ?", userID, ids).Update("category", category) return res.RowsAffected, res.Error } func (r *BookmarkRepository) RenameCategoryByUser(userID uint, from, to string) (int64, error) { res := r.db.Model(&domain.Bookmark{}).Where("user_id = ? AND category = ?", userID, from).Update("category", to) return res.RowsAffected, res.Error } func (r *BookmarkRepository) ReorderByUser(userID uint, orderedIDs []uint) error { if len(orderedIDs) == 0 { return nil } return r.db.Transaction(func(tx *gorm.DB) error { for i, id := range orderedIDs { if err := tx.Model(&domain.Bookmark{}). Where("id = ? AND user_id = ?", id, userID). Update("sort_order", i).Error; err != nil { return err } } return nil }) }