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

73 lines
1.9 KiB
Go
Raw Normal View History

2024-11-26 21:36:40 +00:00
package gormstore
import (
"context"
2024-11-26 21:38:45 +00:00
"git.itzana.me/strafesnet/maps-service/internal/datastore"
"git.itzana.me/strafesnet/maps-service/internal/model"
2024-11-26 21:36:40 +00:00
"gorm.io/gorm"
)
2024-11-26 22:37:18 +00:00
type Submissions struct {
2024-11-26 21:36:40 +00:00
db *gorm.DB
}
2024-11-26 22:37:18 +00:00
func (m Submissions) Get(ctx context.Context, id int64) (model.Submission, error) {
var smap model.Submission
2024-11-26 21:36:40 +00:00
if err := m.db.WithContext(ctx).First(&smap, id).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return smap, datastore.ErrNotExist
}
return smap, err
}
return smap, nil
}
2024-11-26 22:37:18 +00:00
func (m Submissions) GetList(ctx context.Context, id []int64) ([]model.Submission, error) {
var mapList []model.Submission
2024-11-26 21:36:40 +00:00
if err := m.db.WithContext(ctx).Find(&mapList, "id IN ?", id).Error; err != nil {
return mapList, err
}
return mapList, nil
}
2024-11-26 22:37:18 +00:00
func (m Submissions) Create(ctx context.Context, smap model.Submission) (model.Submission, error) {
2024-11-26 21:36:40 +00:00
if err := m.db.WithContext(ctx).Create(&smap).Error; err != nil {
return smap, err
}
return smap, nil
}
2024-11-26 22:37:18 +00:00
func (m Submissions) Update(ctx context.Context, id int64, values datastore.OptionalMap) error {
if err := m.db.WithContext(ctx).Model(&model.Submission{}).Where("id = ?", id).Updates(values.Map()).Error; err != nil {
2024-11-26 21:36:40 +00:00
if err == gorm.ErrRecordNotFound {
return datastore.ErrNotExist
}
return err
}
return nil
}
2024-11-26 22:37:18 +00:00
func (m Submissions) Delete(ctx context.Context, id int64) error {
if err := m.db.WithContext(ctx).Delete(&model.Submission{}, id).Error; err != nil {
2024-11-26 21:36:40 +00:00
if err == gorm.ErrRecordNotFound {
return datastore.ErrNotExist
}
return err
}
return nil
}
2024-11-26 22:37:18 +00:00
func (m Submissions) List(ctx context.Context, filters datastore.OptionalMap, page model.Page) ([]model.Submission, error) {
var maps []model.Submission
2024-11-26 21:36:40 +00:00
if err := m.db.WithContext(ctx).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
}