Files
gochat/backend/internal/channel/line/repository.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

70 lines
2.4 KiB
Go

package line
// Repository provides GORM-based persistence for LINE channel data.
// Reference: gochat's repository pattern (TikTok, WhatsApp repositories)
//
// Design: Repository pattern with GORM for LINE channel model persistence.
// - Create, FindByID, Update, Delete with soft-delete support
// - UpdateFields for partial updates using map[string]interface{}
// - List with account-level filtering for multi-tenant isolation
import (
"context"
"gorm.io/gorm"
channelmodel "github.com/gochat/gochat/internal/model/channel"
)
// Repository handles LINE channel database operations.
type Repository struct {
db *gorm.DB
}
// NewRepository creates a LINE channel repository instance.
func NewRepository(db *gorm.DB) *Repository {
return &Repository{db: db}
}
// Create persists a new LINE channel record.
func (r *Repository) Create(ctx context.Context, channel *channelmodel.ChannelLINE) error {
return r.db.WithContext(ctx).Create(channel).Error
}
// FindByID retrieves a LINE channel by primary key.
func (r *Repository) FindByID(ctx context.Context, id uint) (*channelmodel.ChannelLINE, error) {
var ch channelmodel.ChannelLINE
if err := r.db.WithContext(ctx).First(&ch, id).Error; err != nil {
return nil, err
}
return &ch, nil
}
// FindByChannelID retrieves a LINE channel by its LINE Channel ID.
func (r *Repository) FindByChannelID(ctx context.Context, channelID string) (*channelmodel.ChannelLINE, error) {
var ch channelmodel.ChannelLINE
if err := r.db.WithContext(ctx).Where("channel_id = ?", channelID).First(&ch).Error; err != nil {
return nil, err
}
return &ch, nil
}
// UpdateFields performs a partial update on a LINE channel record.
func (r *Repository) UpdateFields(ctx context.Context, id uint, fields map[string]interface{}) error {
return r.db.WithContext(ctx).Model(&channelmodel.ChannelLINE{}).Where("id = ?", id).Updates(fields).Error
}
// Delete soft-deletes a LINE channel record (GORM soft-delete).
func (r *Repository) Delete(ctx context.Context, id uint) error {
return r.db.WithContext(ctx).Delete(&channelmodel.ChannelLINE{}, id).Error
}
// List retrieves all LINE channels for a given account.
func (r *Repository) List(ctx context.Context, accountID uint) ([]channelmodel.ChannelLINE, error) {
var channels []channelmodel.ChannelLINE
if err := r.db.WithContext(ctx).Where("account_id = ?", accountID).Find(&channels).Error; err != nil {
return nil, err
}
return channels, nil
}