2020-08-26 14:51:53 +02:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
errs "github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrMissingWorkgroup = errs.New("missing workgroup")
|
|
|
|
ErrInvalidWorkgroup = errs.New("invalid workgroup")
|
|
|
|
ErrDecisionSupportFileDoesNotExist = errs.New("decision support file does not exist")
|
|
|
|
)
|
|
|
|
|
|
|
|
type DSFRepository struct {
|
|
|
|
db *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DSFRepository) Create(ctx context.Context, changes *DecisionSupportFileChanges) (*DecisionSupportFile, error) {
|
|
|
|
dsf := &DecisionSupportFile{}
|
|
|
|
|
|
|
|
if err := r.updateFromChanges(dsf, changes); err != nil {
|
|
|
|
return nil, errs.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.db.Save(&dsf).Error; err != nil {
|
|
|
|
return nil, errs.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dsf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DSFRepository) Update(ctx context.Context, id string, changes *DecisionSupportFileChanges) (*DecisionSupportFile, error) {
|
|
|
|
dsf := &DecisionSupportFile{}
|
|
|
|
|
|
|
|
if err := r.db.Find(dsf, "id = ?", id).Error; err != nil {
|
|
|
|
return nil, errs.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.updateFromChanges(dsf, changes); err != nil {
|
|
|
|
return nil, errs.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.db.Save(dsf).Error; err != nil {
|
|
|
|
return nil, errs.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dsf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DSFRepository) updateFromChanges(dsf *DecisionSupportFile, changes *DecisionSupportFileChanges) error {
|
|
|
|
if changes.WorkgroupID == nil {
|
|
|
|
return errs.WithStack(ErrMissingWorkgroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg := &Workgroup{}
|
|
|
|
if err := r.db.Model(wg).First(wg, "id = ?", changes.WorkgroupID).Error; err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return errs.WithStack(ErrInvalidWorkgroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dsf.Workgroup = wg
|
|
|
|
|
|
|
|
if changes.Sections != nil {
|
2020-10-02 16:37:24 +02:00
|
|
|
dsf.Sections = changes.Sections
|
2020-08-26 14:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if changes.Title != nil {
|
|
|
|
dsf.Title = *changes.Title
|
|
|
|
}
|
|
|
|
|
|
|
|
if changes.Status != nil {
|
|
|
|
dsf.Status = *changes.Status
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
func (r *DSFRepository) Find(ctx context.Context, id string) (*DecisionSupportFile, error) {
|
|
|
|
dsf := &DecisionSupportFile{}
|
|
|
|
query := r.db.Model(dsf).Preload("Workgroup").Where("id = ?", id)
|
|
|
|
|
|
|
|
if err := query.First(&dsf).Error; err != nil {
|
|
|
|
return nil, errs.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dsf, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 14:51:53 +02:00
|
|
|
func (r *DSFRepository) Search(ctx context.Context, filter *DecisionSupportFileFilter) ([]*DecisionSupportFile, error) {
|
|
|
|
query := r.db.Model(&DecisionSupportFile{}).Preload("Workgroup")
|
|
|
|
|
|
|
|
if filter != nil {
|
|
|
|
if filter.Ids != nil {
|
|
|
|
query = query.Where("id in (?)", filter.Ids)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dsfs := make([]*DecisionSupportFile, 0)
|
|
|
|
|
|
|
|
if err := query.Find(&dsfs).Error; err != nil {
|
|
|
|
return nil, errs.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dsfs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDSFRepository(db *gorm.DB) *DSFRepository {
|
|
|
|
return &DSFRepository{db}
|
|
|
|
}
|