profession - add usecase

This commit is contained in:
Dawid Wysokiński 2021-02-27 12:12:16 +01:00
parent f49dfc5c43
commit b1a50a3e1e
6 changed files with 124 additions and 19 deletions

View File

@ -16,7 +16,7 @@ type FetchConfig struct {
type Repository interface {
Store(ctx context.Context, input *models.ProfessionInput) (*models.Profession, error)
Update(ctx context.Context, f *models.ProfessionFilter, input *models.ProfessionInput) ([]*models.Profession, error)
UpdateMany(ctx context.Context, f *models.ProfessionFilter, input *models.ProfessionInput) ([]*models.Profession, error)
Delete(ctx context.Context, f *models.ProfessionFilter) ([]*models.Profession, error)
Fetch(ctx context.Context, cfg *FetchConfig) ([]*models.Profession, int, error)
}

View File

@ -1,8 +1,8 @@
package repository
const (
nameIsAlreadyTaken = "Istnieje już zawód o podanej nazwie."
failedToSaveModel = "Wystąpił błąd podczas zapisywania zawodu, prosimy spróbować później."
failedToDeleteModel = "Wystąpił błąd podczas usuwania zawodu, prosimy spróbować później."
failedToFetchModel = "Wystąpił błąd podczas pobierania zawodów, prosimy spróbować później."
messageNameIsAlreadyTaken = "Istnieje już zawód o podanej nazwie."
messageFailedToSaveModel = "Wystąpił błąd podczas zapisywania zawodu, prosimy spróbować później."
messageFailedToDeleteModel = "Wystąpił błąd podczas usuwania zawodu, prosimy spróbować później."
messageFailedToFetchModel = "Wystąpił błąd podczas pobierania zawodów, prosimy spróbować później."
)

View File

@ -2,6 +2,7 @@ package repository
import (
"context"
"fmt"
"strings"
errorutils "github.com/zdam-egzamin-zawodowy/backend/pkg/utils/error"
@ -16,13 +17,16 @@ type pgRepository struct {
}
type PGRepositoryConfig struct {
db *pg.DB
DB *pg.DB
}
func NewPGRepository(cfg PGRepositoryConfig) profession.Repository {
return &pgRepository{
cfg.db,
func NewPGRepository(cfg *PGRepositoryConfig) (profession.Repository, error) {
if cfg == nil || cfg.DB == nil {
return nil, fmt.Errorf("profession/pg_repository: *pg.DB is required")
}
return &pgRepository{
cfg.DB,
}, nil
}
func (repo *pgRepository) Store(ctx context.Context, input *models.ProfessionInput) (*models.Profession, error) {
@ -33,14 +37,14 @@ func (repo *pgRepository) Store(ctx context.Context, input *models.ProfessionInp
Returning("*").
Insert(); err != nil {
if strings.Contains(err.Error(), "name") {
return nil, errorutils.Wrap(err, nameIsAlreadyTaken)
return nil, errorutils.Wrap(err, messageNameIsAlreadyTaken)
}
return nil, errorutils.Wrap(err, failedToSaveModel)
return nil, errorutils.Wrap(err, messageFailedToSaveModel)
}
return item, nil
}
func (repo *pgRepository) Update(ctx context.Context, f *models.ProfessionFilter, input *models.ProfessionInput) ([]*models.Profession, error) {
func (repo *pgRepository) UpdateMany(ctx context.Context, f *models.ProfessionFilter, input *models.ProfessionInput) ([]*models.Profession, error) {
items := []*models.Profession{}
if _, err := repo.
Model(&items).
@ -48,11 +52,11 @@ func (repo *pgRepository) Update(ctx context.Context, f *models.ProfessionFilter
Returning("*").
Apply(input.ApplyUpdate).
Apply(f.Where).
Update(); err != nil {
Update(); err != nil && err != pg.ErrNoRows {
if strings.Contains(err.Error(), "name") {
return nil, errorutils.Wrap(err, nameIsAlreadyTaken)
return nil, errorutils.Wrap(err, messageNameIsAlreadyTaken)
}
return nil, errorutils.Wrap(err, failedToSaveModel)
return nil, errorutils.Wrap(err, messageFailedToSaveModel)
}
return items, nil
}
@ -64,8 +68,8 @@ func (repo *pgRepository) Delete(ctx context.Context, f *models.ProfessionFilter
Context(ctx).
Returning("*").
Apply(f.Where).
Delete(); err != nil {
return nil, errorutils.Wrap(err, failedToDeleteModel)
Delete(); err != nil && err != pg.ErrNoRows {
return nil, errorutils.Wrap(err, messageFailedToDeleteModel)
}
return items, nil
}
@ -87,7 +91,7 @@ func (repo *pgRepository) Fetch(ctx context.Context, cfg *profession.FetchConfig
err = query.Select()
}
if err != nil && err != pg.ErrNoRows {
return nil, 0, errorutils.Wrap(err, failedToFetchModel)
return nil, 0, errorutils.Wrap(err, messageFailedToFetchModel)
}
return items, total, nil
}

View File

@ -9,7 +9,6 @@ import (
type Usecase interface {
Store(ctx context.Context, input *models.ProfessionInput) (*models.Profession, error)
UpdateOne(ctx context.Context, id int, input *models.ProfessionInput) (*models.Profession, error)
UpdateMany(ctx context.Context, f *models.ProfessionFilter, input *models.ProfessionInput) ([]*models.Profession, error)
Delete(ctx context.Context, f *models.ProfessionFilter) ([]*models.Profession, error)
Fetch(ctx context.Context, cfg *FetchConfig) ([]*models.Profession, int, error)
GetByID(ctx context.Context, id int) (*models.Profession, error)

View File

@ -0,0 +1,6 @@
package usecase
const (
messageInvalidID = "Niepoprawne ID."
messageItemNotFound = "Nie znaleziono zawodu."
)

View File

@ -0,0 +1,96 @@
package usecase
import (
"context"
"fmt"
"github.com/zdam-egzamin-zawodowy/backend/internal/models"
"github.com/zdam-egzamin-zawodowy/backend/internal/profession"
)
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) {
return ucase.professionRepository.Store(ctx, input)
}
func (ucase *usecase) UpdateOne(ctx context.Context, id int, input *models.ProfessionInput) (*models.Profession, error) {
if id <= 0 {
return nil, fmt.Errorf(messageInvalidID)
}
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{
Limit: 100,
Count: true,
}
}
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
}