56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package gormstore
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"git.itzana.me/strafesnet/maps-service/pkg/datastore"
|
|
"git.itzana.me/strafesnet/maps-service/pkg/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Operations struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func (env *Operations) Get(ctx context.Context, id int32) (model.Operation, error) {
|
|
var operation model.Operation
|
|
if err := env.db.First(&operation, id).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return operation, datastore.ErrNotExist
|
|
}
|
|
return operation, err
|
|
}
|
|
return operation, nil
|
|
}
|
|
|
|
func (env *Operations) Create(ctx context.Context, smap model.Operation) (model.Operation, error) {
|
|
if err := env.db.Create(&smap).Error; err != nil {
|
|
return smap, err
|
|
}
|
|
|
|
return smap, nil
|
|
}
|
|
|
|
func (env *Operations) Update(ctx context.Context, id int32, values datastore.OptionalMap) error {
|
|
if err := env.db.Model(&model.Operation{}).Where("id = ?", id).Updates(values.Map()).Error; err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return datastore.ErrNotExist
|
|
}
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (env *Operations) Delete(ctx context.Context, id int32) error {
|
|
if err := env.db.Delete(&model.Operation{}, id).Error; err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return datastore.ErrNotExist
|
|
}
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|