This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
backend/internal/profession/repository/pg_repository.go

135 lines
3.4 KiB
Go
Raw Normal View History

package repository
import (
"context"
"github.com/Kichiyaki/gopgutil/v10"
"github.com/pkg/errors"
"strings"
2022-09-20 16:46:47 +00:00
"gitea.dwysokinski.me/zdam-egzamin-zawodowy/backend/util/errorutil"
"github.com/go-pg/pg/v10"
2021-07-14 04:58:10 +00:00
2022-09-20 16:46:47 +00:00
"gitea.dwysokinski.me/zdam-egzamin-zawodowy/backend/internal/model"
"gitea.dwysokinski.me/zdam-egzamin-zawodowy/backend/internal/profession"
)
type PGRepositoryConfig struct {
2021-02-27 11:12:16 +00:00
DB *pg.DB
}
2021-07-14 05:13:51 +00:00
type PGRepository struct {
*pg.DB
}
var _ profession.Repository = &PGRepository{}
func NewPGRepository(cfg *PGRepositoryConfig) (*PGRepository, error) {
2021-02-27 11:12:16 +00:00
if cfg == nil || cfg.DB == nil {
2021-05-14 13:34:26 +00:00
return nil, errors.New("cfg.DB is required")
}
2021-07-14 05:13:51 +00:00
return &PGRepository{
2021-02-27 11:12:16 +00:00
cfg.DB,
}, nil
}
2021-07-14 05:13:51 +00:00
func (repo *PGRepository) Store(ctx context.Context, input *model.ProfessionInput) (*model.Profession, error) {
item := input.ToProfession()
if _, err := repo.
Model(item).
Context(ctx).
Returning("*").
Insert(); err != nil {
return nil, handleInsertAndUpdateError(err)
}
return item, nil
}
2021-07-14 05:13:51 +00:00
func (repo *PGRepository) UpdateMany(ctx context.Context, f *model.ProfessionFilter, input *model.ProfessionInput) ([]*model.Profession, error) {
if _, err := repo.
2021-07-14 04:58:10 +00:00
Model(&model.Profession{}).
Context(ctx).
Apply(input.ApplyUpdate).
Apply(f.Where).
2021-02-27 11:12:16 +00:00
Update(); err != nil && err != pg.ErrNoRows {
return nil, handleInsertAndUpdateError(err)
}
2021-03-06 13:43:50 +00:00
items, _, err := repo.Fetch(ctx, &profession.FetchConfig{
Count: false,
Filter: f,
})
if err != nil {
return nil, err
}
return items, nil
}
2021-07-14 05:13:51 +00:00
func (repo *PGRepository) Delete(ctx context.Context, f *model.ProfessionFilter) ([]*model.Profession, error) {
2021-07-14 04:58:10 +00:00
items := make([]*model.Profession, 0)
if _, err := repo.
Model(&items).
Context(ctx).
Returning("*").
Apply(f.Where).
2021-02-27 11:12:16 +00:00
Delete(); err != nil && err != pg.ErrNoRows {
2021-05-02 06:36:10 +00:00
return nil, errorutil.Wrap(err, messageFailedToDeleteModel)
}
return items, nil
}
2021-07-14 05:13:51 +00:00
func (repo *PGRepository) Fetch(ctx context.Context, cfg *profession.FetchConfig) ([]*model.Profession, int, error) {
var err error
2021-07-14 04:58:10 +00:00
items := make([]*model.Profession, 0)
total := 0
query := repo.
Model(&items).
Context(ctx).
Limit(cfg.Limit).
Offset(cfg.Offset).
Apply(cfg.Filter.Where).
2021-05-04 17:19:14 +00:00
Apply(gopgutil.OrderAppender{
Orders: cfg.Sort,
}.Apply)
if cfg.Count {
total, err = query.SelectAndCount()
} else {
err = query.Select()
}
if err != nil && err != pg.ErrNoRows {
2021-05-02 06:36:10 +00:00
return nil, 0, errorutil.Wrap(err, messageFailedToFetchModel)
}
return items, total, nil
}
2021-07-14 05:13:51 +00:00
func (repo *PGRepository) GetAssociatedQualifications(
ctx context.Context,
ids ...int,
2021-07-14 04:58:10 +00:00
) (map[int][]*model.Qualification, error) {
m := make(map[int][]*model.Qualification)
for _, id := range ids {
2021-07-14 04:58:10 +00:00
m[id] = make([]*model.Qualification, 0)
}
2021-07-14 04:58:10 +00:00
var qualificationToProfession []*model.QualificationToProfession
if err := repo.
Model(&qualificationToProfession).
Context(ctx).
Where(gopgutil.BuildConditionArray("profession_id"), pg.Array(ids)).
Relation("Qualification").
Order("qualification.formula ASC", "qualification.code ASC").
Select(); err != nil {
2021-05-02 06:36:10 +00:00
return nil, errorutil.Wrap(err, messageFailedToFetchAssociatedQualifications)
}
for _, record := range qualificationToProfession {
m[record.ProfessionID] = append(m[record.ProfessionID], record.Qualification)
}
return m, nil
}
func handleInsertAndUpdateError(err error) error {
if strings.Contains(err.Error(), "name") || strings.Contains(err.Error(), "slug") {
2021-05-02 06:36:10 +00:00
return errorutil.Wrap(err, messageNameIsAlreadyTaken)
}
2021-05-02 06:36:10 +00:00
return errorutil.Wrap(err, messageFailedToSaveModel)
}