Files
gochat/backend/internal/repository/notification_subscription_repo.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

85 lines
3.0 KiB
Go

package repository
import (
"context"
"encoding/json"
"github.com/gochat/gochat/internal/model"
"gorm.io/gorm"
)
// NotificationSubscriptionRepo provides data access for NotificationSubscription.
type NotificationSubscriptionRepo struct {
db *gorm.DB
}
func NewNotificationSubscriptionRepo(db *gorm.DB) *NotificationSubscriptionRepo {
return &NotificationSubscriptionRepo{db: db}
}
// Create inserts a new notification subscription.
func (r *NotificationSubscriptionRepo) Create(ctx context.Context, sub *model.NotificationSubscription) error {
return r.db.WithContext(ctx).Create(sub).Error
}
// Save inserts or updates a subscription.
func (r *NotificationSubscriptionRepo) Save(ctx context.Context, sub *model.NotificationSubscription) error {
return r.db.WithContext(ctx).Save(sub).Error
}
// FindByID retrieves a subscription by ID.
func (r *NotificationSubscriptionRepo) FindByID(ctx context.Context, id uint) (*model.NotificationSubscription, error) {
var sub model.NotificationSubscription
err := r.db.WithContext(ctx).First(&sub, id).Error
return &sub, err
}
// FindByUserID retrieves all subscriptions for a user.
func (r *NotificationSubscriptionRepo) FindByUserID(ctx context.Context, userID uint) ([]model.NotificationSubscription, error) {
var subs []model.NotificationSubscription
err := r.db.WithContext(ctx).Where("user_id = ?", userID).Find(&subs).Error
return subs, err
}
// FindByUserIDAndPushToken finds the Chatwoot destroy target by push_token.
func (r *NotificationSubscriptionRepo) FindByUserIDAndPushToken(ctx context.Context, userID uint, pushToken string) (*model.NotificationSubscription, error) {
subs, err := r.FindByUserID(ctx, userID)
if err != nil {
return nil, err
}
for i := range subs {
if subs[i].Identifier == pushToken {
return &subs[i], nil
}
var attrs map[string]any
if err := json.Unmarshal(subs[i].SubscriptionAttributes, &attrs); err == nil {
if value, _ := attrs["push_token"].(string); value == pushToken {
return &subs[i], nil
}
if value, _ := attrs["endpoint"].(string); value == pushToken {
return &subs[i], nil
}
}
}
return nil, gorm.ErrRecordNotFound
}
// FindByIdentifier retrieves a subscription by its unique identifier.
func (r *NotificationSubscriptionRepo) FindByIdentifier(ctx context.Context, identifier string) (*model.NotificationSubscription, error) {
var sub model.NotificationSubscription
err := r.db.WithContext(ctx).Where("identifier = ?", identifier).First(&sub).Error
return &sub, err
}
// Delete removes a subscription by ID.
func (r *NotificationSubscriptionRepo) Delete(ctx context.Context, id uint) error {
return r.db.WithContext(ctx).Delete(&model.NotificationSubscription{}, id).Error
}
// DeleteByUserIDAndIdentifier removes a specific subscription by user and identifier.
func (r *NotificationSubscriptionRepo) DeleteByUserIDAndIdentifier(ctx context.Context, userID uint, identifier string) error {
return r.db.WithContext(ctx).
Where("user_id = ? AND identifier = ?", userID, identifier).
Delete(&model.NotificationSubscription{}).Error
}