2021-02-27 11:12:16 +00:00
|
|
|
package usecase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/zdam-egzamin-zawodowy/backend/internal/models"
|
|
|
|
"github.com/zdam-egzamin-zawodowy/backend/internal/profession"
|
2021-02-27 11:17:29 +00:00
|
|
|
sqlutils "github.com/zdam-egzamin-zawodowy/backend/pkg/utils/sql"
|
2021-02-27 11:12:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type usecase struct {
|
|
|
|
professionRepository profession.Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
ProfessionRepository profession.Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(cfg *Config) (profession.Usecase, error) {
|
|
|
|
if cfg == nil || cfg.ProfessionRepository == nil {
|
|
|
|
return nil, fmt.Errorf("profession/usecase: ProfessionRepository is required")
|
|
|
|
}
|
|
|
|
return &usecase{
|
|
|
|
cfg.ProfessionRepository,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ucase *usecase) Store(ctx context.Context, input *models.ProfessionInput) (*models.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-03-05 18:52:15 +00:00
|
|
|
func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *models.ProfessionInput) (*models.Profession, error) {
|
2021-02-27 11:12:16 +00:00
|
|
|
if id <= 0 {
|
|
|
|
return nil, fmt.Errorf(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,
|
|
|
|
&models.ProfessionFilter{
|
|
|
|
ID: []int{id},
|
|
|
|
},
|
|
|
|
input)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(items) == 0 {
|
|
|
|
return nil, fmt.Errorf(messageItemNotFound)
|
|
|
|
}
|
|
|
|
return items[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ucase *usecase) Delete(ctx context.Context, f *models.ProfessionFilter) ([]*models.Profession, error) {
|
|
|
|
return ucase.professionRepository.Delete(ctx, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ucase *usecase) Fetch(ctx context.Context, cfg *profession.FetchConfig) ([]*models.Profession, int, error) {
|
|
|
|
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-03-06 07:44:49 +00:00
|
|
|
cfg.Sort = sqlutils.SanitizeSorts(cfg.Sort)
|
2021-02-27 11:12:16 +00:00
|
|
|
return ucase.professionRepository.Fetch(ctx, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ucase *usecase) GetByID(ctx context.Context, id int) (*models.Profession, error) {
|
|
|
|
items, _, err := ucase.Fetch(ctx, &profession.FetchConfig{
|
|
|
|
Limit: 1,
|
|
|
|
Count: false,
|
|
|
|
Filter: &models.ProfessionFilter{
|
|
|
|
ID: []int{id},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(items) == 0 {
|
|
|
|
return nil, fmt.Errorf(messageItemNotFound)
|
|
|
|
}
|
|
|
|
return items[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ucase *usecase) GetBySlug(ctx context.Context, slug string) (*models.Profession, error) {
|
|
|
|
items, _, err := ucase.Fetch(ctx, &profession.FetchConfig{
|
|
|
|
Limit: 1,
|
|
|
|
Count: false,
|
|
|
|
Filter: &models.ProfessionFilter{
|
|
|
|
Slug: []string{slug},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(items) == 0 {
|
|
|
|
return nil, fmt.Errorf(messageItemNotFound)
|
|
|
|
}
|
|
|
|
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-03-13 15:05:20 +00:00
|
|
|
func validateInput(input *models.ProfessionInput, opts validateOptions) error {
|
2021-02-27 11:40:06 +00:00
|
|
|
if input.IsEmpty() {
|
|
|
|
return fmt.Errorf(messageEmptyPayload)
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.Name != nil {
|
2021-03-05 18:52:15 +00:00
|
|
|
if *input.Name == "" {
|
2021-02-27 11:40:06 +00:00
|
|
|
return fmt.Errorf(messageNameIsRequired)
|
2021-03-05 18:52:15 +00:00
|
|
|
} else if len(*input.Name) > profession.MaxNameLength {
|
2021-02-27 11:40:06 +00:00
|
|
|
return fmt.Errorf(messageNameIsTooLong, profession.MaxNameLength)
|
|
|
|
}
|
2021-03-13 15:05:20 +00:00
|
|
|
} else if !opts.allowNilValues {
|
2021-02-27 11:40:06 +00:00
|
|
|
return fmt.Errorf(messageNameIsRequired)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|