111 lines
3.9 KiB
Go
111 lines
3.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// AssignmentPolicyV2Repo implements GORM repository for AssignmentPolicyV2.
|
|
type AssignmentPolicyV2Repo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewAssignmentPolicyV2Repo creates a new AssignmentPolicyV2 repository.
|
|
func NewAssignmentPolicyV2Repo(db *gorm.DB) *AssignmentPolicyV2Repo {
|
|
return &AssignmentPolicyV2Repo{db: db}
|
|
}
|
|
|
|
// Create creates a new assignment policy V2.
|
|
func (r *AssignmentPolicyV2Repo) Create(ctx context.Context, policy *model.AssignmentPolicyV2) error {
|
|
return r.db.WithContext(ctx).Create(policy).Error
|
|
}
|
|
|
|
// FindByID retrieves an assignment policy V2 by primary key.
|
|
func (r *AssignmentPolicyV2Repo) FindByID(ctx context.Context, id uint) (*model.AssignmentPolicyV2, error) {
|
|
var policy model.AssignmentPolicyV2
|
|
if err := r.db.WithContext(ctx).First(&policy, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &policy, nil
|
|
}
|
|
|
|
// FindByAccount retrieves all assignment policies V2 for an account.
|
|
func (r *AssignmentPolicyV2Repo) FindByAccount(ctx context.Context, accountID uint) ([]model.AssignmentPolicyV2, error) {
|
|
var policies []model.AssignmentPolicyV2
|
|
if err := r.db.WithContext(ctx).
|
|
Where("account_id = ?", accountID).
|
|
Find(&policies).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return policies, nil
|
|
}
|
|
|
|
// Update updates an assignment policy V2.
|
|
func (r *AssignmentPolicyV2Repo) Update(ctx context.Context, policy *model.AssignmentPolicyV2) error {
|
|
return r.db.WithContext(ctx).Save(policy).Error
|
|
}
|
|
|
|
// Delete soft-deletes an assignment policy V2.
|
|
func (r *AssignmentPolicyV2Repo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.AssignmentPolicyV2{}, id).Error
|
|
}
|
|
|
|
// AssignmentPolicyInboxRepo implements GORM repository for AssignmentPolicyInbox.
|
|
type AssignmentPolicyInboxRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewAssignmentPolicyInboxRepo creates a new AssignmentPolicyInbox repository.
|
|
func NewAssignmentPolicyInboxRepo(db *gorm.DB) *AssignmentPolicyInboxRepo {
|
|
return &AssignmentPolicyInboxRepo{db: db}
|
|
}
|
|
|
|
// Create creates a new assignment policy inbox association.
|
|
func (r *AssignmentPolicyInboxRepo) Create(ctx context.Context, apInbox *model.AssignmentPolicyInbox) error {
|
|
return r.db.WithContext(ctx).Create(apInbox).Error
|
|
}
|
|
|
|
// FindByAssignmentPolicy retrieves all inbox associations for a policy.
|
|
func (r *AssignmentPolicyInboxRepo) FindByAssignmentPolicy(ctx context.Context, policyID uint) ([]model.AssignmentPolicyInbox, error) {
|
|
var apInboxes []model.AssignmentPolicyInbox
|
|
if err := r.db.WithContext(ctx).
|
|
Where("assignment_policy_id = ?", policyID).
|
|
Find(&apInboxes).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return apInboxes, nil
|
|
}
|
|
|
|
// FindByInbox retrieves the assignment policy inbox association for a specific inbox.
|
|
func (r *AssignmentPolicyInboxRepo) FindByInbox(ctx context.Context, inboxID uint) (*model.AssignmentPolicyInbox, error) {
|
|
var apInbox model.AssignmentPolicyInbox
|
|
if err := r.db.WithContext(ctx).
|
|
Where("inbox_id = ?", inboxID).
|
|
First(&apInbox).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &apInbox, nil
|
|
}
|
|
|
|
// Delete soft-deletes an assignment policy inbox association.
|
|
func (r *AssignmentPolicyInboxRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.AssignmentPolicyInbox{}, id).Error
|
|
}
|
|
|
|
// DeleteByInbox removes the assignment policy association for a specific inbox.
|
|
func (r *AssignmentPolicyInboxRepo) DeleteByInbox(ctx context.Context, inboxID uint) error {
|
|
return r.db.WithContext(ctx).
|
|
Where("inbox_id = ?", inboxID).
|
|
Delete(&model.AssignmentPolicyInbox{}).Error
|
|
}
|
|
|
|
// DeleteByAssignmentPolicy removes all inbox associations for a policy.
|
|
func (r *AssignmentPolicyInboxRepo) DeleteByAssignmentPolicy(ctx context.Context, policyID uint) error {
|
|
return r.db.WithContext(ctx).
|
|
Where("assignment_policy_id = ?", policyID).
|
|
Delete(&model.AssignmentPolicyInbox{}).Error
|
|
}
|