From f87c4dab8f8271abfafb52116177c1620432577f Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 27 Nov 2024 15:14:50 -0800 Subject: [PATCH] hooray functions copy pasted --- pkg/datastore/datastore.go | 5 +++ pkg/datastore/gormstore/submissions.go | 48 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/pkg/datastore/datastore.go b/pkg/datastore/datastore.go index 608b56f..bb7020e 100644 --- a/pkg/datastore/datastore.go +++ b/pkg/datastore/datastore.go @@ -16,4 +16,9 @@ type Datastore interface { type Submissions interface { Get(ctx context.Context, id int64) (model.Submission, error) + GetList(ctx context.Context, id []int64) ([]model.Submission, error) + Create(ctx context.Context, smap model.Submission) (model.Submission, error) + Update(ctx context.Context, id int64, values OptionalMap) error + Delete(ctx context.Context, id int64) error + List(ctx context.Context, filters OptionalMap, page model.Page) ([]model.Submission, error) } diff --git a/pkg/datastore/gormstore/submissions.go b/pkg/datastore/gormstore/submissions.go index c7c1d5d..b39a242 100644 --- a/pkg/datastore/gormstore/submissions.go +++ b/pkg/datastore/gormstore/submissions.go @@ -21,3 +21,51 @@ func (env *Submissions) Get(ctx context.Context, id int64) (model.Submission, er } return submission, nil } + +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 +} + +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 +}