zdravko/internal/services/worker_group.go

45 lines
1.2 KiB
Go
Raw Normal View History

2024-02-24 21:07:49 +00:00
package services
import (
"context"
"code.tjo.space/mentos1386/zdravko/internal/models"
"code.tjo.space/mentos1386/zdravko/internal/models/query"
"gorm.io/gorm/clause"
)
func GetOrCreateWorkerGroup(ctx context.Context, q *query.Query, workerGroup models.WorkerGroup) (*models.WorkerGroup, error) {
tx := q.Begin()
if err := tx.WorkerGroup.WithContext(ctx).Clauses(clause.OnConflict{DoNothing: true}).Create(&workerGroup); err != nil {
_ = tx.Rollback()
return nil, err
}
wg, err := tx.WorkerGroup.WithContext(ctx).Where(
q.WorkerGroup.Slug.Eq(workerGroup.Slug),
).First()
if err != nil {
_ = tx.Rollback()
return nil, err
}
return wg, tx.Commit()
}
func GetWorkerGroups(ctx context.Context, q *query.Query) ([]*models.WorkerGroup, error) {
return q.WorkerGroup.WithContext(ctx).Preload(q.WorkerGroup.Monitors).Find()
}
func GetWorkerGroupsBySlug(ctx context.Context, q *query.Query, slugs []string) ([]*models.WorkerGroup, error) {
return q.WorkerGroup.WithContext(ctx).Where(
q.WorkerGroup.Slug.In(slugs...),
).Find()
}
func GetWorkerGroup(ctx context.Context, q *query.Query, slug string) (*models.WorkerGroup, error) {
return q.WorkerGroup.WithContext(ctx).Where(
q.WorkerGroup.Slug.Eq(slug),
).First()
}