108 lines
3.8 KiB
Go
108 lines
3.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// DashboardAppRepo provides data access for DashboardApp.
|
|
type DashboardAppRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewDashboardAppRepo(db *gorm.DB) *DashboardAppRepo {
|
|
return &DashboardAppRepo{db: db}
|
|
}
|
|
|
|
func (r *DashboardAppRepo) Create(ctx context.Context, app *model.DashboardApp) error {
|
|
return r.db.WithContext(ctx).Create(app).Error
|
|
}
|
|
|
|
func (r *DashboardAppRepo) GetByID(ctx context.Context, id uint) (*model.DashboardApp, error) {
|
|
var app model.DashboardApp
|
|
if err := r.db.WithContext(ctx).First(&app, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &app, nil
|
|
}
|
|
|
|
func (r *DashboardAppRepo) Update(ctx context.Context, app *model.DashboardApp) error {
|
|
return r.db.WithContext(ctx).Save(app).Error
|
|
}
|
|
|
|
func (r *DashboardAppRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.DashboardApp{}, id).Error
|
|
}
|
|
|
|
// FindAllByAccountID returns all dashboard apps for an account (no pagination).
|
|
// Chatwoot: Current.account.dashboard_apps — returns all without limit/offset
|
|
func (r *DashboardAppRepo) FindAllByAccountID(ctx context.Context, accountID uint) ([]model.DashboardApp, error) {
|
|
var apps []model.DashboardApp
|
|
if err := r.db.WithContext(ctx).Where("account_id = ?", accountID).Find(&apps).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return apps, nil
|
|
}
|
|
|
|
func (r *DashboardAppRepo) FindByAccountID(ctx context.Context, accountID uint, offset, limit int) ([]model.DashboardApp, int64, error) {
|
|
var apps []model.DashboardApp
|
|
var count int64
|
|
db := r.db.WithContext(ctx).Model(&model.DashboardApp{}).Where("account_id = ?", accountID)
|
|
db.Count(&count)
|
|
if err := db.Offset(offset).Limit(limit).Find(&apps).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return apps, count, nil
|
|
}
|
|
|
|
func (r *DashboardAppRepo) FindByUserID(ctx context.Context, userID uint, offset, limit int) ([]model.DashboardApp, int64, error) {
|
|
var apps []model.DashboardApp
|
|
var count int64
|
|
db := r.db.WithContext(ctx).Model(&model.DashboardApp{}).Where("user_id = ?", userID)
|
|
db.Count(&count)
|
|
if err := db.Offset(offset).Limit(limit).Find(&apps).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return apps, count, nil
|
|
}
|
|
|
|
// Search searches dashboard apps by title (keyword match) within an account.
|
|
// Returns matching apps and total count, with pagination support.
|
|
// SearchAll returns all matching dashboard apps (no pagination).
|
|
func (r *DashboardAppRepo) SearchAll(ctx context.Context, accountID uint, query string) ([]model.DashboardApp, error) {
|
|
var apps []model.DashboardApp
|
|
searchPattern := "%" + query + "%"
|
|
if err := r.db.WithContext(ctx).Where("account_id = ? AND title LIKE ?", accountID, searchPattern).
|
|
Order("created_at DESC").Find(&apps).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return apps, nil
|
|
}
|
|
|
|
func (r *DashboardAppRepo) Search(ctx context.Context, accountID uint, query string, offset, limit int) ([]model.DashboardApp, int64, error) {
|
|
var apps []model.DashboardApp
|
|
var count int64
|
|
searchPattern := "%" + query + "%"
|
|
db := r.db.WithContext(ctx).Model(&model.DashboardApp{}).
|
|
Where("account_id = ? AND title LIKE ?", accountID, searchPattern)
|
|
db.Count(&count)
|
|
if err := db.Offset(offset).Limit(limit).Order("created_at DESC").Find(&apps).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return apps, count, nil
|
|
}
|
|
|
|
// FindActiveByAccountID returns only active dashboard apps for an account.
|
|
// FindAllActiveByAccountID returns all active dashboard apps for an account (no pagination).
|
|
func (r *DashboardAppRepo) FindAllActiveByAccountID(ctx context.Context, accountID uint) ([]model.DashboardApp, error) {
|
|
var apps []model.DashboardApp
|
|
if err := r.db.WithContext(ctx).
|
|
Where("account_id = ? AND active = ? AND deleted_at IS NULL", accountID, true).
|
|
Order("created_at DESC").
|
|
Find(&apps).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return apps, nil
|
|
} |