2021-02-27 13:01:59 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-04 17:19:14 +00:00
|
|
|
"github.com/Kichiyaki/gopgutil/v10"
|
2021-03-27 15:20:55 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-02-27 13:01:59 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-07-16 15:44:41 +00:00
|
|
|
"github.com/zdam-egzamin-zawodowy/backend/util/errorutil"
|
2021-02-27 13:01:59 +00:00
|
|
|
|
|
|
|
"github.com/go-pg/pg/v10"
|
2021-07-14 04:58:10 +00:00
|
|
|
|
|
|
|
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
2021-02-27 13:01:59 +00:00
|
|
|
"github.com/zdam-egzamin-zawodowy/backend/internal/user"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PGRepositoryConfig struct {
|
|
|
|
DB *pg.DB
|
|
|
|
}
|
|
|
|
|
2021-07-14 05:13:51 +00:00
|
|
|
type PGRepository struct {
|
|
|
|
*pg.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ user.Repository = &PGRepository{}
|
|
|
|
|
|
|
|
func NewPGRepository(cfg *PGRepositoryConfig) (*PGRepository, error) {
|
2021-02-27 13:01:59 +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-02-27 13:01:59 +00:00
|
|
|
}
|
2021-07-14 05:13:51 +00:00
|
|
|
return &PGRepository{
|
2021-02-27 13:01:59 +00:00
|
|
|
cfg.DB,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-07-14 05:13:51 +00:00
|
|
|
func (repo *PGRepository) Store(ctx context.Context, input *model.UserInput) (*model.User, error) {
|
2021-02-27 13:01:59 +00:00
|
|
|
item := input.ToUser()
|
|
|
|
if _, err := repo.
|
|
|
|
Model(item).
|
|
|
|
Context(ctx).
|
|
|
|
Returning("*").
|
|
|
|
Insert(); err != nil {
|
2021-03-27 15:20:55 +00:00
|
|
|
return nil, handleInsertAndUpdateError(err)
|
2021-02-27 13:01:59 +00:00
|
|
|
}
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
|
2021-07-14 05:13:51 +00:00
|
|
|
func (repo *PGRepository) UpdateMany(ctx context.Context, f *model.UserFilter, input *model.UserInput) ([]*model.User, error) {
|
2021-02-27 13:01:59 +00:00
|
|
|
if _, err := repo.
|
2021-07-14 04:58:10 +00:00
|
|
|
Model(&model.User{}).
|
2021-02-27 13:01:59 +00:00
|
|
|
Context(ctx).
|
|
|
|
Apply(input.ApplyUpdate).
|
|
|
|
Apply(f.Where).
|
|
|
|
Update(); err != nil && err != pg.ErrNoRows {
|
2021-03-27 15:20:55 +00:00
|
|
|
return nil, handleInsertAndUpdateError(err)
|
2021-02-27 13:01:59 +00:00
|
|
|
}
|
2021-03-06 13:43:50 +00:00
|
|
|
items, _, err := repo.Fetch(ctx, &user.FetchConfig{
|
|
|
|
Count: false,
|
|
|
|
Filter: f,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-27 13:01:59 +00:00
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
2021-07-14 05:13:51 +00:00
|
|
|
func (repo *PGRepository) Delete(ctx context.Context, f *model.UserFilter) ([]*model.User, error) {
|
2021-07-14 04:58:10 +00:00
|
|
|
items := make([]*model.User, 0)
|
2021-02-27 13:01:59 +00:00
|
|
|
if _, err := repo.
|
|
|
|
Model(&items).
|
|
|
|
Context(ctx).
|
|
|
|
Returning("*").
|
|
|
|
Apply(f.Where).
|
|
|
|
Delete(); err != nil && err != pg.ErrNoRows {
|
2021-05-02 06:36:10 +00:00
|
|
|
return nil, errorutil.Wrap(err, messageFailedToDeleteModel)
|
2021-02-27 13:01:59 +00:00
|
|
|
}
|
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
2021-07-14 05:13:51 +00:00
|
|
|
func (repo *PGRepository) Fetch(ctx context.Context, cfg *user.FetchConfig) ([]*model.User, int, error) {
|
2021-02-27 13:01:59 +00:00
|
|
|
var err error
|
2021-07-14 04:58:10 +00:00
|
|
|
items := make([]*model.User, 0)
|
2021-02-27 13:01:59 +00:00
|
|
|
total := 0
|
|
|
|
query := repo.
|
|
|
|
Model(&items).
|
|
|
|
Context(ctx).
|
|
|
|
Limit(cfg.Limit).
|
|
|
|
Offset(cfg.Offset).
|
2021-03-11 19:23:53 +00:00
|
|
|
Apply(cfg.Filter.Where).
|
2021-05-04 17:19:14 +00:00
|
|
|
Apply(gopgutil.OrderAppender{
|
|
|
|
Orders: cfg.Sort,
|
|
|
|
}.Apply)
|
2021-02-27 13:01:59 +00:00
|
|
|
|
|
|
|
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)
|
2021-02-27 13:01:59 +00:00
|
|
|
}
|
|
|
|
return items, total, nil
|
|
|
|
}
|
2021-03-27 15:20:55 +00:00
|
|
|
|
|
|
|
func handleInsertAndUpdateError(err error) error {
|
|
|
|
if strings.Contains(err.Error(), "email") {
|
2021-05-02 06:36:10 +00:00
|
|
|
return errorutil.Wrap(err, messageEmailIsAlreadyTaken)
|
2021-03-27 15:20:55 +00:00
|
|
|
}
|
2021-05-02 06:36:10 +00:00
|
|
|
return errorutil.Wrap(err, messageFailedToSaveModel)
|
2021-03-27 15:20:55 +00:00
|
|
|
}
|