Reviewed-on: #99 Co-authored-by: Quaternions <krakow20@gmail.com> Co-committed-by: Quaternions <krakow20@gmail.com>
66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package gormstore
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"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
|
|
}
|
|
|
|
func (env *Operations) CountSince(ctx context.Context, owner int64, since time.Time) (int64, error) {
|
|
var count int64
|
|
if err := env.db.Model(&model.Operation{}).Where("owner = ? AND created_at > ?",owner,since).Count(&count).Error; err != nil {
|
|
return count, err
|
|
}
|
|
|
|
return count, nil
|
|
}
|