107 lines
3.4 KiB
Go
107 lines
3.4 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type (
|
|
MaintGroupRepository interface {
|
|
Create(ctx context.Context, tx *gorm.DB, maintGroup entities.M_MaintenanceGroup) (entities.M_MaintenanceGroup, error)
|
|
GetById(ctx context.Context, tx *gorm.DB, maintGroupId string) (entities.M_MaintenanceGroup, error)
|
|
GetByName(ctx context.Context, tx *gorm.DB, name string) (entities.M_MaintenanceGroup, error)
|
|
GetByCode(ctx context.Context, tx *gorm.DB, code string) (entities.M_MaintenanceGroup, error)
|
|
Update(ctx context.Context, tx *gorm.DB, maintGroup entities.M_MaintenanceGroup) (entities.M_MaintenanceGroup, error)
|
|
Delete(ctx context.Context, tx *gorm.DB, maintGroupId string) error
|
|
}
|
|
|
|
maintGroupRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
)
|
|
|
|
// GetByCode implements MaintGroupRepository.
|
|
func (m *maintGroupRepository) GetByCode(ctx context.Context, tx *gorm.DB, code string) (entities.M_MaintenanceGroup, error) {
|
|
if tx == nil {
|
|
tx = m.db
|
|
}
|
|
var maintGroup entities.M_MaintenanceGroup
|
|
if err := tx.WithContext(ctx).Where("code = ?", code).First(&maintGroup).Error; err != nil {
|
|
return entities.M_MaintenanceGroup{}, err
|
|
}
|
|
return maintGroup, nil
|
|
}
|
|
|
|
// Create implements MaintGroupRepository.
|
|
func (m *maintGroupRepository) Create(ctx context.Context, tx *gorm.DB, maintGroup entities.M_MaintenanceGroup) (entities.M_MaintenanceGroup, error) {
|
|
if tx == nil {
|
|
tx = m.db
|
|
}
|
|
|
|
if err := tx.WithContext(ctx).Create(&maintGroup).Error; err != nil {
|
|
return entities.M_MaintenanceGroup{}, err
|
|
}
|
|
return maintGroup, nil
|
|
}
|
|
|
|
// Delete implements MaintGroupRepository.
|
|
func (m *maintGroupRepository) Delete(ctx context.Context, tx *gorm.DB, maintGroupId string) error {
|
|
if tx == nil {
|
|
tx = m.db
|
|
}
|
|
if err := tx.WithContext(ctx).Delete(&entities.M_MaintenanceGroup{}, "id = ?", maintGroupId).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetById implements MaintGroupRepository.
|
|
func (m *maintGroupRepository) GetById(ctx context.Context, tx *gorm.DB, maintGroupId string) (entities.M_MaintenanceGroup, error) {
|
|
if tx == nil {
|
|
tx = m.db
|
|
}
|
|
var maintGroup entities.M_MaintenanceGroup
|
|
if err := tx.WithContext(ctx).
|
|
Preload("Client").
|
|
Preload("MaintenanceGroupRoles").
|
|
Preload("MaintenanceGroupRoles.Role").
|
|
Preload("MaintenanceGroupRoles.MaintenanceGroupRoleUsers").
|
|
Preload("MaintenanceGroupRoles.MaintenanceGroupRoleUsers.User").
|
|
First(&maintGroup, "id = ?", maintGroupId).Error; err != nil {
|
|
return entities.M_MaintenanceGroup{}, err
|
|
}
|
|
return maintGroup, nil
|
|
}
|
|
|
|
// GetByName implements MaintGroupRepository.
|
|
func (m *maintGroupRepository) GetByName(ctx context.Context, tx *gorm.DB, name string) (entities.M_MaintenanceGroup, error) {
|
|
if tx == nil {
|
|
tx = m.db
|
|
}
|
|
var maintGroup entities.M_MaintenanceGroup
|
|
if err := tx.WithContext(ctx).Where("name = ?", name).First(&maintGroup).Error; err != nil {
|
|
return entities.M_MaintenanceGroup{}, err
|
|
}
|
|
return maintGroup, nil
|
|
}
|
|
|
|
// Update implements MaintGroupRepository.
|
|
func (m *maintGroupRepository) Update(ctx context.Context, tx *gorm.DB, maintGroup entities.M_MaintenanceGroup) (entities.M_MaintenanceGroup, error) {
|
|
if tx == nil {
|
|
tx = m.db
|
|
}
|
|
if err := tx.WithContext(ctx).Updates(&maintGroup).Error; err != nil {
|
|
return entities.M_MaintenanceGroup{}, err
|
|
}
|
|
|
|
return maintGroup, nil
|
|
}
|
|
|
|
func NewMaintGroupRepository(db *gorm.DB) MaintGroupRepository {
|
|
return &maintGroupRepository{
|
|
db: db,
|
|
}
|
|
}
|