backend: add bookmark CRUD with public list endpoint

New bookmarks feature: domain model, repository, service and handler
supporting list/create/update/delete. Public endpoint exposes the
admin user's bookmarks for the homepage navigation grid; authenticated
endpoints scope by user. Dev Dockerfile drops air for plain `go run`
and uses goproxy.cn to avoid build failures on the deploy host.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-03 01:51:43 +08:00
parent efd644dc67
commit ffecc9451d
9 changed files with 343 additions and 3 deletions

View File

@@ -0,0 +1,51 @@
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) 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
}