maps-service/pkg/datastore/gormstore/submissions.go

85 lines
2.3 KiB
Go
Raw Normal View History

2024-11-26 21:36:40 +00:00
package gormstore
import (
"context"
2024-11-26 23:30:58 +00:00
"errors"
2024-11-28 02:57:42 +00:00
2024-11-26 23:30:58 +00:00
"git.itzana.me/strafesnet/maps-service/pkg/datastore"
"git.itzana.me/strafesnet/maps-service/pkg/model"
2024-11-26 21:36:40 +00:00
"gorm.io/gorm"
)
2024-11-26 23:30:58 +00:00
type Submissions struct {
2024-11-26 21:36:40 +00:00
db *gorm.DB
}
2024-11-26 23:30:58 +00:00
func (env *Submissions) Get(ctx context.Context, id int64) (model.Submission, error) {
var submission model.Submission
if err := env.db.First(&submission, id).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return submission, datastore.ErrNotExist
2024-11-26 21:36:40 +00:00
}
}
2024-11-26 23:30:58 +00:00
return submission, nil
2024-11-26 21:36:40 +00:00
}
2024-11-27 23:14:50 +00:00
func (env *Submissions) GetList(ctx context.Context, id []int64) ([]model.Submission, error) {
var mapList []model.Submission
if err := env.db.Find(&mapList, "id IN ?", id).Error; err != nil {
return mapList, err
}
return mapList, nil
}
func (env *Submissions) Create(ctx context.Context, smap model.Submission) (model.Submission, error) {
if err := env.db.Create(&smap).Error; err != nil {
return smap, err
}
return smap, nil
}
func (env *Submissions) Update(ctx context.Context, id int64, values datastore.OptionalMap) error {
if err := env.db.Model(&model.Submission{}).Where("id = ?", id).Updates(values.Map()).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return datastore.ErrNotExist
}
return err
}
return nil
}
2024-11-28 02:57:42 +00:00
// the update can only occur if the status matches one of the provided values.
func (env *Submissions) IfStatusThenUpdate(ctx context.Context, id int64, statuses []model.Status, values datastore.OptionalMap) error {
if err := env.db.Model(&model.Submission{}).Where("id = ?", id).Where("status IN ?",statuses).Updates(values.Map()).Error; err != nil {
2024-11-28 02:57:42 +00:00
if err == gorm.ErrRecordNotFound {
return datastore.ErrNotExist
}
return err
}
return nil
}
2024-11-27 23:14:50 +00:00
func (env *Submissions) Delete(ctx context.Context, id int64) error {
if err := env.db.Delete(&model.Submission{}, id).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return datastore.ErrNotExist
}
return err
}
return nil
}
func (env *Submissions) List(ctx context.Context, filters datastore.OptionalMap, page model.Page) ([]model.Submission, error) {
var maps []model.Submission
if err := env.db.Where(filters.Map()).Offset(int((page.Number - 1) * page.Size)).Limit(int(page.Size)).Find(&maps).Error; err != nil {
return nil, err
}
return maps, nil
}