97 lines
2.9 KiB
Go
97 lines
2.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type AssignmentUserRepository interface {
|
|
Create(ctx context.Context, tx *gorm.DB, user entities.TAssignmentUserEntity) (entities.TAssignmentUserEntity, error)
|
|
BulkCreate(ctx context.Context, tx *gorm.DB, users []entities.TAssignmentUserEntity) error
|
|
GetById(ctx context.Context, tx *gorm.DB, id string) (entities.TAssignmentUserEntity, error)
|
|
GetAllByAssignment(ctx context.Context, assignmentId string) ([]entities.TAssignmentUserEntity, error)
|
|
Update(ctx context.Context, tx *gorm.DB, user entities.TAssignmentUserEntity) (entities.TAssignmentUserEntity, error)
|
|
Delete(ctx context.Context, tx *gorm.DB, id string) error
|
|
}
|
|
|
|
type assignmentUserRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// BulkCreate implements AssignmentUserRepository.
|
|
func (r *assignmentUserRepository) BulkCreate(ctx context.Context, tx *gorm.DB, users []entities.TAssignmentUserEntity) error {
|
|
if tx == nil {
|
|
tx = r.db
|
|
}
|
|
if err := tx.WithContext(ctx).Create(&users).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func NewAssignmentUserRepository(db *gorm.DB) AssignmentUserRepository {
|
|
return &assignmentUserRepository{db: db}
|
|
}
|
|
|
|
func (r *assignmentUserRepository) Create(ctx context.Context, tx *gorm.DB, user entities.TAssignmentUserEntity) (entities.TAssignmentUserEntity, error) {
|
|
if tx == nil {
|
|
tx = r.db
|
|
}
|
|
if err := tx.WithContext(ctx).Create(&user).Error; err != nil {
|
|
return user, err
|
|
}
|
|
return user, nil
|
|
}
|
|
|
|
func (r *assignmentUserRepository) GetById(ctx context.Context, tx *gorm.DB, id string) (entities.TAssignmentUserEntity, error) {
|
|
if tx == nil {
|
|
tx = r.db
|
|
}
|
|
var user entities.TAssignmentUserEntity
|
|
if err := tx.WithContext(ctx).
|
|
Preload("Assignment").
|
|
Preload("User").
|
|
Preload("Role").
|
|
Preload("Client").
|
|
First(&user, "id = ?", id).Error; err != nil {
|
|
return user, err
|
|
}
|
|
return user, nil
|
|
}
|
|
|
|
func (r *assignmentUserRepository) GetAllByAssignment(ctx context.Context, assignmentId string) ([]entities.TAssignmentUserEntity, error) {
|
|
var users []entities.TAssignmentUserEntity
|
|
if err := r.db.WithContext(ctx).
|
|
Preload("Assignment").
|
|
Preload("User").
|
|
Preload("Role").
|
|
Preload("Client").
|
|
Where("assignment_id = ?", assignmentId).
|
|
Find(&users).Error; err != nil {
|
|
return users, err
|
|
}
|
|
return users, nil
|
|
}
|
|
|
|
func (r *assignmentUserRepository) Update(ctx context.Context, tx *gorm.DB, user entities.TAssignmentUserEntity) (entities.TAssignmentUserEntity, error) {
|
|
if tx == nil {
|
|
tx = r.db
|
|
}
|
|
if err := tx.WithContext(ctx).Model(&entities.TAssignmentUserEntity{}).Where("id = ?", user.ID).Select("*").Updates(&user).Error; err != nil {
|
|
return user, err
|
|
}
|
|
return user, nil
|
|
}
|
|
|
|
func (r *assignmentUserRepository) Delete(ctx context.Context, tx *gorm.DB, id string) error {
|
|
if tx == nil {
|
|
tx = r.db
|
|
}
|
|
if err := tx.WithContext(ctx).Delete(&entities.TAssignmentUserEntity{}, "id = ?", id).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|