2021-02-27 11:12:16 +00:00
|
|
|
package usecase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-27 15:20:55 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-02-27 11:12:16 +00:00
|
|
|
|
2021-07-14 04:58:10 +00:00
|
|
|
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
2021-02-27 11:12:16 +00:00
|
|
|
"github.com/zdam-egzamin-zawodowy/backend/internal/profession"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
ProfessionRepository profession.Repository
|
|
|
|
}
|
|
|
|
|
2021-07-14 05:13:51 +00:00
|
|
|
type Usecase struct {
|
|
|
|
professionRepository profession.Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ profession.Usecase = &Usecase{}
|
|
|
|
|
|
|
|
func New(cfg *Config) (*Usecase, error) {
|
2021-02-27 11:12:16 +00:00
|
|
|
if cfg == nil || cfg.ProfessionRepository == nil {
|
2021-05-14 13:34:26 +00:00
|
|
|
return nil, errors.New("cfg.ProfessionRepository is required")
|
2021-02-27 11:12:16 +00:00
|
|
|
}
|
2021-07-14 05:13:51 +00:00
|
|
|
return &Usecase{
|
2021-02-27 11:12:16 +00:00
|
|
|
cfg.ProfessionRepository,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-07-14 05:13:51 +00:00
|
|
|
func (ucase *Usecase) Store(ctx context.Context, input *model.ProfessionInput) (*model.Profession, error) {
|
2021-03-13 15:05:20 +00:00
|
|
|
if err := validateInput(input.Sanitize(), validateOptions{false}); err != nil {
|
2021-02-27 11:40:06 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-27 11:12:16 +00:00
|
|
|
return ucase.professionRepository.Store(ctx, input)
|
|
|
|
}
|
|
|
|
|
2021-07-14 05:13:51 +00:00
|
|
|
func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *model.ProfessionInput) (*model.Profession, error) {
|
2021-02-27 11:12:16 +00:00
|
|
|
if id <= 0 {
|
2021-03-27 15:20:55 +00:00
|
|
|
return nil, errors.New(messageInvalidID)
|
2021-02-27 11:40:06 +00:00
|
|
|
}
|
2021-03-13 15:05:20 +00:00
|
|
|
if err := validateInput(input.Sanitize(), validateOptions{true}); err != nil {
|
2021-02-27 13:01:59 +00:00
|
|
|
return nil, err
|
2021-02-27 11:12:16 +00:00
|
|
|
}
|
|
|
|
items, err := ucase.professionRepository.UpdateMany(ctx,
|
2021-07-14 04:58:10 +00:00
|
|
|
&model.ProfessionFilter{
|
2021-02-27 11:12:16 +00:00
|
|
|
ID: []int{id},
|
|
|
|
},
|
|
|
|
input)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(items) == 0 {
|
2021-03-27 15:20:55 +00:00
|
|
|
return nil, errors.New(messageItemNotFound)
|
2021-02-27 11:12:16 +00:00
|
|
|
}
|
|
|
|
return items[0], nil
|
|
|
|
}
|
|
|
|
|
2021-07-14 05:13:51 +00:00
|
|
|
func (ucase *Usecase) Delete(ctx context.Context, f *model.ProfessionFilter) ([]*model.Profession, error) {
|
2021-02-27 11:12:16 +00:00
|
|
|
return ucase.professionRepository.Delete(ctx, f)
|
|
|
|
}
|
|
|
|
|
2021-07-14 05:13:51 +00:00
|
|
|
func (ucase *Usecase) Fetch(ctx context.Context, cfg *profession.FetchConfig) ([]*model.Profession, int, error) {
|
2021-02-27 11:12:16 +00:00
|
|
|
if cfg == nil {
|
|
|
|
cfg = &profession.FetchConfig{
|
2021-03-24 05:15:48 +00:00
|
|
|
Limit: profession.FetchDefaultLimit,
|
2021-02-27 11:12:16 +00:00
|
|
|
Count: true,
|
|
|
|
}
|
|
|
|
}
|
2021-05-09 09:59:37 +00:00
|
|
|
if len(cfg.Sort) > profession.MaxOrders {
|
|
|
|
cfg.Sort = cfg.Sort[0:profession.MaxOrders]
|
|
|
|
}
|
|
|
|
|
2021-02-27 11:12:16 +00:00
|
|
|
return ucase.professionRepository.Fetch(ctx, cfg)
|
|
|
|
}
|
|
|
|
|
2021-07-14 05:13:51 +00:00
|
|
|
func (ucase *Usecase) GetByID(ctx context.Context, id int) (*model.Profession, error) {
|
2021-02-27 11:12:16 +00:00
|
|
|
items, _, err := ucase.Fetch(ctx, &profession.FetchConfig{
|
|
|
|
Limit: 1,
|
|
|
|
Count: false,
|
2021-07-14 04:58:10 +00:00
|
|
|
Filter: &model.ProfessionFilter{
|
2021-02-27 11:12:16 +00:00
|
|
|
ID: []int{id},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(items) == 0 {
|
2021-03-27 15:20:55 +00:00
|
|
|
return nil, errors.New(messageItemNotFound)
|
2021-02-27 11:12:16 +00:00
|
|
|
}
|
|
|
|
return items[0], nil
|
|
|
|
}
|
|
|
|
|
2021-07-14 05:13:51 +00:00
|
|
|
func (ucase *Usecase) GetBySlug(ctx context.Context, slug string) (*model.Profession, error) {
|
2021-02-27 11:12:16 +00:00
|
|
|
items, _, err := ucase.Fetch(ctx, &profession.FetchConfig{
|
|
|
|
Limit: 1,
|
|
|
|
Count: false,
|
2021-07-14 04:58:10 +00:00
|
|
|
Filter: &model.ProfessionFilter{
|
2021-02-27 11:12:16 +00:00
|
|
|
Slug: []string{slug},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(items) == 0 {
|
2021-03-27 15:20:55 +00:00
|
|
|
return nil, errors.New(messageItemNotFound)
|
2021-02-27 11:12:16 +00:00
|
|
|
}
|
|
|
|
return items[0], nil
|
|
|
|
}
|
2021-02-27 11:40:06 +00:00
|
|
|
|
|
|
|
type validateOptions struct {
|
2021-03-13 15:05:20 +00:00
|
|
|
allowNilValues bool
|
2021-02-27 11:40:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 04:58:10 +00:00
|
|
|
func validateInput(input *model.ProfessionInput, opts validateOptions) error {
|
2021-02-27 11:40:06 +00:00
|
|
|
if input.IsEmpty() {
|
2021-03-27 15:20:55 +00:00
|
|
|
return errors.New(messageEmptyPayload)
|
2021-02-27 11:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if input.Name != nil {
|
2021-03-05 18:52:15 +00:00
|
|
|
if *input.Name == "" {
|
2021-03-27 15:20:55 +00:00
|
|
|
return errors.New(messageNameIsRequired)
|
2021-03-05 18:52:15 +00:00
|
|
|
} else if len(*input.Name) > profession.MaxNameLength {
|
2021-03-27 15:20:55 +00:00
|
|
|
return errors.Errorf(messageNameIsTooLong, profession.MaxNameLength)
|
2021-02-27 11:40:06 +00:00
|
|
|
}
|
2021-03-13 15:05:20 +00:00
|
|
|
} else if !opts.allowNilValues {
|
2021-03-27 15:20:55 +00:00
|
|
|
return errors.New(messageNameIsRequired)
|
2021-02-27 11:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|