return types instead of interfaces
This commit is contained in:
parent
5a94e9403b
commit
31a713ec38
|
@ -25,22 +25,17 @@ func (opts Metadata) ToMapClaims() jwt.MapClaims {
|
|||
return mClaims
|
||||
}
|
||||
|
||||
type TokenGenerator interface {
|
||||
Generate(metadata Metadata) (string, error)
|
||||
ExtractAccessTokenMetadata(token string) (*Metadata, error)
|
||||
}
|
||||
|
||||
type tokenGenerator struct {
|
||||
type TokenGenerator struct {
|
||||
accessSecret string
|
||||
}
|
||||
|
||||
func NewTokenGenerator(accessSecret string) TokenGenerator {
|
||||
return &tokenGenerator{
|
||||
func NewTokenGenerator(accessSecret string) *TokenGenerator {
|
||||
return &TokenGenerator{
|
||||
accessSecret: accessSecret,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *tokenGenerator) Generate(metadata Metadata) (string, error) {
|
||||
func (g *TokenGenerator) Generate(metadata Metadata) (string, error) {
|
||||
atClaims := metadata.ToMapClaims()
|
||||
if !metadata.StaySignedIn {
|
||||
atClaims["exp"] = time.Now().Add(time.Hour * 24).Unix()
|
||||
|
@ -54,7 +49,7 @@ func (g *tokenGenerator) Generate(metadata Metadata) (string, error) {
|
|||
return accessToken, nil
|
||||
}
|
||||
|
||||
func (g *tokenGenerator) ExtractAccessTokenMetadata(token string) (*Metadata, error) {
|
||||
func (g *TokenGenerator) ExtractAccessTokenMetadata(token string) (*Metadata, error) {
|
||||
return extractTokenMetadata(g.accessSecret, token)
|
||||
}
|
||||
|
||||
|
|
|
@ -11,27 +11,29 @@ import (
|
|||
"github.com/zdam-egzamin-zawodowy/backend/pkg/util/errorutil"
|
||||
)
|
||||
|
||||
type usecase struct {
|
||||
userRepository user.Repository
|
||||
tokenGenerator jwt.TokenGenerator
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
UserRepository user.Repository
|
||||
TokenGenerator jwt.TokenGenerator
|
||||
TokenGenerator *jwt.TokenGenerator
|
||||
}
|
||||
|
||||
func New(cfg *Config) (auth.Usecase, error) {
|
||||
type Usecase struct {
|
||||
userRepository user.Repository
|
||||
tokenGenerator *jwt.TokenGenerator
|
||||
}
|
||||
|
||||
var _ auth.Usecase = &Usecase{}
|
||||
|
||||
func New(cfg *Config) (*Usecase, error) {
|
||||
if cfg == nil || cfg.UserRepository == nil {
|
||||
return nil, errors.New("cfg.UserRepository is required")
|
||||
}
|
||||
return &usecase{
|
||||
return &Usecase{
|
||||
cfg.UserRepository,
|
||||
cfg.TokenGenerator,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (ucase *usecase) SignIn(ctx context.Context, email, password string, staySignedIn bool) (*model.User, string, error) {
|
||||
func (ucase *Usecase) SignIn(ctx context.Context, email, password string, staySignedIn bool) (*model.User, string, error) {
|
||||
u, err := ucase.GetUserByCredentials(ctx, email, password)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
|
@ -51,7 +53,7 @@ func (ucase *usecase) SignIn(ctx context.Context, email, password string, staySi
|
|||
return u, token, nil
|
||||
}
|
||||
|
||||
func (ucase *usecase) ExtractAccessTokenMetadata(ctx context.Context, accessToken string) (*model.User, error) {
|
||||
func (ucase *Usecase) ExtractAccessTokenMetadata(ctx context.Context, accessToken string) (*model.User, error) {
|
||||
metadata, err := ucase.tokenGenerator.ExtractAccessTokenMetadata(accessToken)
|
||||
if err != nil {
|
||||
return nil, errorutil.Wrap(err, messageInvalidAccessToken)
|
||||
|
@ -60,7 +62,7 @@ func (ucase *usecase) ExtractAccessTokenMetadata(ctx context.Context, accessToke
|
|||
return ucase.GetUserByCredentials(ctx, metadata.Credentials.Email, metadata.Credentials.Password)
|
||||
}
|
||||
|
||||
func (ucase *usecase) GetUserByCredentials(ctx context.Context, email, password string) (*model.User, error) {
|
||||
func (ucase *Usecase) GetUserByCredentials(ctx context.Context, email, password string) (*model.User, error) {
|
||||
users, _, err := ucase.userRepository.Fetch(ctx, &user.FetchConfig{
|
||||
Limit: 1,
|
||||
Count: false,
|
||||
|
|
|
@ -14,24 +14,26 @@ import (
|
|||
"github.com/zdam-egzamin-zawodowy/backend/internal/profession"
|
||||
)
|
||||
|
||||
type pgRepository struct {
|
||||
*pg.DB
|
||||
}
|
||||
|
||||
type PGRepositoryConfig struct {
|
||||
DB *pg.DB
|
||||
}
|
||||
|
||||
func NewPGRepository(cfg *PGRepositoryConfig) (profession.Repository, error) {
|
||||
type PGRepository struct {
|
||||
*pg.DB
|
||||
}
|
||||
|
||||
var _ profession.Repository = &PGRepository{}
|
||||
|
||||
func NewPGRepository(cfg *PGRepositoryConfig) (*PGRepository, error) {
|
||||
if cfg == nil || cfg.DB == nil {
|
||||
return nil, errors.New("cfg.DB is required")
|
||||
}
|
||||
return &pgRepository{
|
||||
return &PGRepository{
|
||||
cfg.DB,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (repo *pgRepository) Store(ctx context.Context, input *model.ProfessionInput) (*model.Profession, error) {
|
||||
func (repo *PGRepository) Store(ctx context.Context, input *model.ProfessionInput) (*model.Profession, error) {
|
||||
item := input.ToProfession()
|
||||
if _, err := repo.
|
||||
Model(item).
|
||||
|
@ -43,7 +45,7 @@ func (repo *pgRepository) Store(ctx context.Context, input *model.ProfessionInpu
|
|||
return item, nil
|
||||
}
|
||||
|
||||
func (repo *pgRepository) UpdateMany(ctx context.Context, f *model.ProfessionFilter, input *model.ProfessionInput) ([]*model.Profession, error) {
|
||||
func (repo *PGRepository) UpdateMany(ctx context.Context, f *model.ProfessionFilter, input *model.ProfessionInput) ([]*model.Profession, error) {
|
||||
if _, err := repo.
|
||||
Model(&model.Profession{}).
|
||||
Context(ctx).
|
||||
|
@ -62,7 +64,7 @@ func (repo *pgRepository) UpdateMany(ctx context.Context, f *model.ProfessionFil
|
|||
return items, nil
|
||||
}
|
||||
|
||||
func (repo *pgRepository) Delete(ctx context.Context, f *model.ProfessionFilter) ([]*model.Profession, error) {
|
||||
func (repo *PGRepository) Delete(ctx context.Context, f *model.ProfessionFilter) ([]*model.Profession, error) {
|
||||
items := make([]*model.Profession, 0)
|
||||
if _, err := repo.
|
||||
Model(&items).
|
||||
|
@ -75,7 +77,7 @@ func (repo *pgRepository) Delete(ctx context.Context, f *model.ProfessionFilter)
|
|||
return items, nil
|
||||
}
|
||||
|
||||
func (repo *pgRepository) Fetch(ctx context.Context, cfg *profession.FetchConfig) ([]*model.Profession, int, error) {
|
||||
func (repo *PGRepository) Fetch(ctx context.Context, cfg *profession.FetchConfig) ([]*model.Profession, int, error) {
|
||||
var err error
|
||||
items := make([]*model.Profession, 0)
|
||||
total := 0
|
||||
|
@ -100,7 +102,7 @@ func (repo *pgRepository) Fetch(ctx context.Context, cfg *profession.FetchConfig
|
|||
return items, total, nil
|
||||
}
|
||||
|
||||
func (repo *pgRepository) GetAssociatedQualifications(
|
||||
func (repo *PGRepository) GetAssociatedQualifications(
|
||||
ctx context.Context,
|
||||
ids ...int,
|
||||
) (map[int][]*model.Qualification, error) {
|
||||
|
|
|
@ -8,31 +8,33 @@ import (
|
|||
"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) {
|
||||
type Usecase struct {
|
||||
professionRepository profession.Repository
|
||||
}
|
||||
|
||||
var _ profession.Usecase = &Usecase{}
|
||||
|
||||
func New(cfg *Config) (*Usecase, error) {
|
||||
if cfg == nil || cfg.ProfessionRepository == nil {
|
||||
return nil, errors.New("cfg.ProfessionRepository is required")
|
||||
}
|
||||
return &usecase{
|
||||
return &Usecase{
|
||||
cfg.ProfessionRepository,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (ucase *usecase) Store(ctx context.Context, input *model.ProfessionInput) (*model.Profession, error) {
|
||||
func (ucase *Usecase) Store(ctx context.Context, input *model.ProfessionInput) (*model.Profession, error) {
|
||||
if err := validateInput(input.Sanitize(), validateOptions{false}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ucase.professionRepository.Store(ctx, input)
|
||||
}
|
||||
|
||||
func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *model.ProfessionInput) (*model.Profession, error) {
|
||||
func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *model.ProfessionInput) (*model.Profession, error) {
|
||||
if id <= 0 {
|
||||
return nil, errors.New(messageInvalidID)
|
||||
}
|
||||
|
@ -53,11 +55,11 @@ func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *model.Pr
|
|||
return items[0], nil
|
||||
}
|
||||
|
||||
func (ucase *usecase) Delete(ctx context.Context, f *model.ProfessionFilter) ([]*model.Profession, error) {
|
||||
func (ucase *Usecase) Delete(ctx context.Context, f *model.ProfessionFilter) ([]*model.Profession, error) {
|
||||
return ucase.professionRepository.Delete(ctx, f)
|
||||
}
|
||||
|
||||
func (ucase *usecase) Fetch(ctx context.Context, cfg *profession.FetchConfig) ([]*model.Profession, int, error) {
|
||||
func (ucase *Usecase) Fetch(ctx context.Context, cfg *profession.FetchConfig) ([]*model.Profession, int, error) {
|
||||
if cfg == nil {
|
||||
cfg = &profession.FetchConfig{
|
||||
Limit: profession.FetchDefaultLimit,
|
||||
|
@ -71,7 +73,7 @@ func (ucase *usecase) Fetch(ctx context.Context, cfg *profession.FetchConfig) ([
|
|||
return ucase.professionRepository.Fetch(ctx, cfg)
|
||||
}
|
||||
|
||||
func (ucase *usecase) GetByID(ctx context.Context, id int) (*model.Profession, error) {
|
||||
func (ucase *Usecase) GetByID(ctx context.Context, id int) (*model.Profession, error) {
|
||||
items, _, err := ucase.Fetch(ctx, &profession.FetchConfig{
|
||||
Limit: 1,
|
||||
Count: false,
|
||||
|
@ -88,7 +90,7 @@ func (ucase *usecase) GetByID(ctx context.Context, id int) (*model.Profession, e
|
|||
return items[0], nil
|
||||
}
|
||||
|
||||
func (ucase *usecase) GetBySlug(ctx context.Context, slug string) (*model.Profession, error) {
|
||||
func (ucase *Usecase) GetBySlug(ctx context.Context, slug string) (*model.Profession, error) {
|
||||
items, _, err := ucase.Fetch(ctx, &profession.FetchConfig{
|
||||
Limit: 1,
|
||||
Count: false,
|
||||
|
|
|
@ -14,24 +14,26 @@ import (
|
|||
"github.com/zdam-egzamin-zawodowy/backend/internal/qualification"
|
||||
)
|
||||
|
||||
type pgRepository struct {
|
||||
*pg.DB
|
||||
}
|
||||
|
||||
type PGRepositoryConfig struct {
|
||||
DB *pg.DB
|
||||
}
|
||||
|
||||
func NewPGRepository(cfg *PGRepositoryConfig) (qualification.Repository, error) {
|
||||
type PGRepository struct {
|
||||
*pg.DB
|
||||
}
|
||||
|
||||
var _ qualification.Repository = &PGRepository{}
|
||||
|
||||
func NewPGRepository(cfg *PGRepositoryConfig) (*PGRepository, error) {
|
||||
if cfg == nil || cfg.DB == nil {
|
||||
return nil, errors.New("cfg.DB is required")
|
||||
}
|
||||
return &pgRepository{
|
||||
return &PGRepository{
|
||||
cfg.DB,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (repo *pgRepository) Store(ctx context.Context, input *model.QualificationInput) (*model.Qualification, error) {
|
||||
func (repo *PGRepository) Store(ctx context.Context, input *model.QualificationInput) (*model.Qualification, error) {
|
||||
item := input.ToQualification()
|
||||
err := repo.RunInTransaction(ctx, func(tx *pg.Tx) error {
|
||||
if _, err := tx.
|
||||
|
@ -53,7 +55,7 @@ func (repo *pgRepository) Store(ctx context.Context, input *model.QualificationI
|
|||
return item, err
|
||||
}
|
||||
|
||||
func (repo *pgRepository) UpdateMany(
|
||||
func (repo *PGRepository) UpdateMany(
|
||||
ctx context.Context,
|
||||
f *model.QualificationFilter,
|
||||
input *model.QualificationInput,
|
||||
|
@ -108,7 +110,7 @@ func (repo *pgRepository) UpdateMany(
|
|||
return items, err
|
||||
}
|
||||
|
||||
func (repo *pgRepository) Delete(ctx context.Context, f *model.QualificationFilter) ([]*model.Qualification, error) {
|
||||
func (repo *PGRepository) Delete(ctx context.Context, f *model.QualificationFilter) ([]*model.Qualification, error) {
|
||||
items := make([]*model.Qualification, 0)
|
||||
if _, err := repo.
|
||||
Model(&items).
|
||||
|
@ -121,7 +123,7 @@ func (repo *pgRepository) Delete(ctx context.Context, f *model.QualificationFilt
|
|||
return items, nil
|
||||
}
|
||||
|
||||
func (repo *pgRepository) Fetch(ctx context.Context, cfg *qualification.FetchConfig) ([]*model.Qualification, int, error) {
|
||||
func (repo *PGRepository) Fetch(ctx context.Context, cfg *qualification.FetchConfig) ([]*model.Qualification, int, error) {
|
||||
var err error
|
||||
items := make([]*model.Qualification, 0)
|
||||
total := 0
|
||||
|
@ -146,7 +148,7 @@ func (repo *pgRepository) Fetch(ctx context.Context, cfg *qualification.FetchCon
|
|||
return items, total, nil
|
||||
}
|
||||
|
||||
func (repo *pgRepository) GetSimilar(ctx context.Context, cfg *qualification.GetSimilarConfig) ([]*model.Qualification, int, error) {
|
||||
func (repo *PGRepository) GetSimilar(ctx context.Context, cfg *qualification.GetSimilarConfig) ([]*model.Qualification, int, error) {
|
||||
var err error
|
||||
subquery := repo.
|
||||
Model(&model.QualificationToProfession{}).
|
||||
|
@ -181,7 +183,7 @@ func (repo *pgRepository) GetSimilar(ctx context.Context, cfg *qualification.Get
|
|||
})
|
||||
}
|
||||
|
||||
func (repo *pgRepository) associateQualificationWithProfession(tx *pg.Tx, qualificationIDs, professionIDs []int) error {
|
||||
func (repo *PGRepository) associateQualificationWithProfession(tx *pg.Tx, qualificationIDs, professionIDs []int) error {
|
||||
var toInsert []*model.QualificationToProfession
|
||||
for _, professionID := range professionIDs {
|
||||
for _, qualificationID := range qualificationIDs {
|
||||
|
|
|
@ -8,31 +8,33 @@ import (
|
|||
"github.com/zdam-egzamin-zawodowy/backend/internal/qualification"
|
||||
)
|
||||
|
||||
type usecase struct {
|
||||
qualificationRepository qualification.Repository
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
QualificationRepository qualification.Repository
|
||||
}
|
||||
|
||||
func New(cfg *Config) (qualification.Usecase, error) {
|
||||
type Usecase struct {
|
||||
qualificationRepository qualification.Repository
|
||||
}
|
||||
|
||||
var _ qualification.Usecase = &Usecase{}
|
||||
|
||||
func New(cfg *Config) (*Usecase, error) {
|
||||
if cfg == nil || cfg.QualificationRepository == nil {
|
||||
return nil, errors.New("cfg.QualificationRepository is required")
|
||||
}
|
||||
return &usecase{
|
||||
return &Usecase{
|
||||
cfg.QualificationRepository,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (ucase *usecase) Store(ctx context.Context, input *model.QualificationInput) (*model.Qualification, error) {
|
||||
func (ucase *Usecase) Store(ctx context.Context, input *model.QualificationInput) (*model.Qualification, error) {
|
||||
if err := validateInput(input.Sanitize(), validateOptions{false}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ucase.qualificationRepository.Store(ctx, input)
|
||||
}
|
||||
|
||||
func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *model.QualificationInput) (*model.Qualification, error) {
|
||||
func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *model.QualificationInput) (*model.Qualification, error) {
|
||||
if id <= 0 {
|
||||
return nil, errors.New(messageInvalidID)
|
||||
}
|
||||
|
@ -53,11 +55,11 @@ func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *model.Qu
|
|||
return items[0], nil
|
||||
}
|
||||
|
||||
func (ucase *usecase) Delete(ctx context.Context, f *model.QualificationFilter) ([]*model.Qualification, error) {
|
||||
func (ucase *Usecase) Delete(ctx context.Context, f *model.QualificationFilter) ([]*model.Qualification, error) {
|
||||
return ucase.qualificationRepository.Delete(ctx, f)
|
||||
}
|
||||
|
||||
func (ucase *usecase) Fetch(ctx context.Context, cfg *qualification.FetchConfig) ([]*model.Qualification, int, error) {
|
||||
func (ucase *Usecase) Fetch(ctx context.Context, cfg *qualification.FetchConfig) ([]*model.Qualification, int, error) {
|
||||
if cfg == nil {
|
||||
cfg = &qualification.FetchConfig{
|
||||
Limit: qualification.FetchDefaultLimit,
|
||||
|
@ -70,7 +72,7 @@ func (ucase *usecase) Fetch(ctx context.Context, cfg *qualification.FetchConfig)
|
|||
return ucase.qualificationRepository.Fetch(ctx, cfg)
|
||||
}
|
||||
|
||||
func (ucase *usecase) GetByID(ctx context.Context, id int) (*model.Qualification, error) {
|
||||
func (ucase *Usecase) GetByID(ctx context.Context, id int) (*model.Qualification, error) {
|
||||
items, _, err := ucase.Fetch(ctx, &qualification.FetchConfig{
|
||||
Limit: 1,
|
||||
Count: false,
|
||||
|
@ -87,7 +89,7 @@ func (ucase *usecase) GetByID(ctx context.Context, id int) (*model.Qualification
|
|||
return items[0], nil
|
||||
}
|
||||
|
||||
func (ucase *usecase) GetBySlug(ctx context.Context, slug string) (*model.Qualification, error) {
|
||||
func (ucase *Usecase) GetBySlug(ctx context.Context, slug string) (*model.Qualification, error) {
|
||||
items, _, err := ucase.Fetch(ctx, &qualification.FetchConfig{
|
||||
Limit: 1,
|
||||
Count: false,
|
||||
|
@ -104,7 +106,7 @@ func (ucase *usecase) GetBySlug(ctx context.Context, slug string) (*model.Qualif
|
|||
return items[0], nil
|
||||
}
|
||||
|
||||
func (ucase *usecase) GetSimilar(ctx context.Context, cfg *qualification.GetSimilarConfig) ([]*model.Qualification, int, error) {
|
||||
func (ucase *Usecase) GetSimilar(ctx context.Context, cfg *qualification.GetSimilarConfig) ([]*model.Qualification, int, error) {
|
||||
if cfg == nil || cfg.QualificationID <= 0 {
|
||||
return nil, 0, errors.New(messageQualificationIDIsRequired)
|
||||
}
|
||||
|
|
|
@ -15,24 +15,26 @@ import (
|
|||
"github.com/zdam-egzamin-zawodowy/backend/pkg/util/errorutil"
|
||||
)
|
||||
|
||||
type pgRepository struct {
|
||||
*pg.DB
|
||||
*repository
|
||||
}
|
||||
|
||||
type PGRepositoryConfig struct {
|
||||
DB *pg.DB
|
||||
FileStorage fstorage.FileStorage
|
||||
}
|
||||
|
||||
func NewPGRepository(cfg *PGRepositoryConfig) (question.Repository, error) {
|
||||
type PGRepository struct {
|
||||
*pg.DB
|
||||
*repository
|
||||
}
|
||||
|
||||
var _ question.Repository = &PGRepository{}
|
||||
|
||||
func NewPGRepository(cfg *PGRepositoryConfig) (*PGRepository, error) {
|
||||
if cfg == nil || cfg.DB == nil {
|
||||
return nil, errors.New("cfg.DB is required")
|
||||
}
|
||||
if cfg.FileStorage == nil {
|
||||
return nil, errors.New("cfg.FileStorage is required")
|
||||
}
|
||||
return &pgRepository{
|
||||
return &PGRepository{
|
||||
cfg.DB,
|
||||
&repository{
|
||||
fileStorage: cfg.FileStorage,
|
||||
|
@ -40,7 +42,7 @@ func NewPGRepository(cfg *PGRepositoryConfig) (question.Repository, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (repo *pgRepository) Store(ctx context.Context, input *model.QuestionInput) (*model.Question, error) {
|
||||
func (repo *PGRepository) Store(ctx context.Context, input *model.QuestionInput) (*model.Question, error) {
|
||||
item := input.ToQuestion()
|
||||
baseQuery := repo.
|
||||
Model(item).
|
||||
|
@ -68,7 +70,7 @@ func (repo *pgRepository) Store(ctx context.Context, input *model.QuestionInput)
|
|||
return item, nil
|
||||
}
|
||||
|
||||
func (repo *pgRepository) UpdateOneByID(ctx context.Context, id int, input *model.QuestionInput) (*model.Question, error) {
|
||||
func (repo *PGRepository) UpdateOneByID(ctx context.Context, id int, input *model.QuestionInput) (*model.Question, error) {
|
||||
item := &model.Question{}
|
||||
baseQuery := repo.
|
||||
Model(item).
|
||||
|
@ -101,7 +103,7 @@ func (repo *pgRepository) UpdateOneByID(ctx context.Context, id int, input *mode
|
|||
return item, nil
|
||||
}
|
||||
|
||||
func (repo *pgRepository) Delete(ctx context.Context, f *model.QuestionFilter) ([]*model.Question, error) {
|
||||
func (repo *PGRepository) Delete(ctx context.Context, f *model.QuestionFilter) ([]*model.Question, error) {
|
||||
items := make([]*model.Question, 0)
|
||||
if _, err := repo.
|
||||
Model(&items).
|
||||
|
@ -117,7 +119,7 @@ func (repo *pgRepository) Delete(ctx context.Context, f *model.QuestionFilter) (
|
|||
return items, nil
|
||||
}
|
||||
|
||||
func (repo *pgRepository) Fetch(ctx context.Context, cfg *question.FetchConfig) ([]*model.Question, int, error) {
|
||||
func (repo *PGRepository) Fetch(ctx context.Context, cfg *question.FetchConfig) ([]*model.Question, int, error) {
|
||||
var err error
|
||||
items := make([]*model.Question, 0)
|
||||
total := 0
|
||||
|
@ -142,7 +144,7 @@ func (repo *pgRepository) Fetch(ctx context.Context, cfg *question.FetchConfig)
|
|||
return items, total, nil
|
||||
}
|
||||
|
||||
func (repo *pgRepository) GenerateTest(ctx context.Context, cfg *question.GenerateTestConfig) ([]*model.Question, error) {
|
||||
func (repo *PGRepository) GenerateTest(ctx context.Context, cfg *question.GenerateTestConfig) ([]*model.Question, error) {
|
||||
subquery := repo.
|
||||
Model(&model.Question{}).
|
||||
Column("id").
|
||||
|
|
|
@ -16,31 +16,33 @@ var (
|
|||
}
|
||||
)
|
||||
|
||||
type usecase struct {
|
||||
questionRepository question.Repository
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
QuestionRepository question.Repository
|
||||
}
|
||||
|
||||
func New(cfg *Config) (question.Usecase, error) {
|
||||
type Usecase struct {
|
||||
questionRepository question.Repository
|
||||
}
|
||||
|
||||
var _ question.Usecase = &Usecase{}
|
||||
|
||||
func New(cfg *Config) (*Usecase, error) {
|
||||
if cfg == nil || cfg.QuestionRepository == nil {
|
||||
return nil, errors.New("cfg.QuestionRepository is required")
|
||||
}
|
||||
return &usecase{
|
||||
return &Usecase{
|
||||
cfg.QuestionRepository,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (ucase *usecase) Store(ctx context.Context, input *model.QuestionInput) (*model.Question, error) {
|
||||
func (ucase *Usecase) Store(ctx context.Context, input *model.QuestionInput) (*model.Question, error) {
|
||||
if err := validateInput(input.Sanitize(), validateOptions{false}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ucase.questionRepository.Store(ctx, input)
|
||||
}
|
||||
|
||||
func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *model.QuestionInput) (*model.Question, error) {
|
||||
func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *model.QuestionInput) (*model.Question, error) {
|
||||
if id <= 0 {
|
||||
return nil, errors.New(messageInvalidID)
|
||||
}
|
||||
|
@ -59,11 +61,11 @@ func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *model.Qu
|
|||
return item, nil
|
||||
}
|
||||
|
||||
func (ucase *usecase) Delete(ctx context.Context, f *model.QuestionFilter) ([]*model.Question, error) {
|
||||
func (ucase *Usecase) Delete(ctx context.Context, f *model.QuestionFilter) ([]*model.Question, error) {
|
||||
return ucase.questionRepository.Delete(ctx, f)
|
||||
}
|
||||
|
||||
func (ucase *usecase) Fetch(ctx context.Context, cfg *question.FetchConfig) ([]*model.Question, int, error) {
|
||||
func (ucase *Usecase) Fetch(ctx context.Context, cfg *question.FetchConfig) ([]*model.Question, int, error) {
|
||||
if cfg == nil {
|
||||
cfg = &question.FetchConfig{
|
||||
Limit: question.FetchMaxLimit,
|
||||
|
@ -79,7 +81,7 @@ func (ucase *usecase) Fetch(ctx context.Context, cfg *question.FetchConfig) ([]*
|
|||
return ucase.questionRepository.Fetch(ctx, cfg)
|
||||
}
|
||||
|
||||
func (ucase *usecase) GetByID(ctx context.Context, id int) (*model.Question, error) {
|
||||
func (ucase *Usecase) GetByID(ctx context.Context, id int) (*model.Question, error) {
|
||||
items, _, err := ucase.Fetch(ctx, &question.FetchConfig{
|
||||
Limit: 1,
|
||||
Count: false,
|
||||
|
@ -96,7 +98,7 @@ func (ucase *usecase) GetByID(ctx context.Context, id int) (*model.Question, err
|
|||
return items[0], nil
|
||||
}
|
||||
|
||||
func (ucase *usecase) GenerateTest(ctx context.Context, cfg *question.GenerateTestConfig) ([]*model.Question, error) {
|
||||
func (ucase *Usecase) GenerateTest(ctx context.Context, cfg *question.GenerateTestConfig) ([]*model.Question, error) {
|
||||
if cfg == nil {
|
||||
cfg = &question.GenerateTestConfig{
|
||||
Limit: question.TestMaxLimit,
|
||||
|
|
|
@ -14,24 +14,26 @@ import (
|
|||
"github.com/zdam-egzamin-zawodowy/backend/internal/user"
|
||||
)
|
||||
|
||||
type pgRepository struct {
|
||||
*pg.DB
|
||||
}
|
||||
|
||||
type PGRepositoryConfig struct {
|
||||
DB *pg.DB
|
||||
}
|
||||
|
||||
func NewPGRepository(cfg *PGRepositoryConfig) (user.Repository, error) {
|
||||
type PGRepository struct {
|
||||
*pg.DB
|
||||
}
|
||||
|
||||
var _ user.Repository = &PGRepository{}
|
||||
|
||||
func NewPGRepository(cfg *PGRepositoryConfig) (*PGRepository, error) {
|
||||
if cfg == nil || cfg.DB == nil {
|
||||
return nil, errors.New("cfg.DB is required")
|
||||
}
|
||||
return &pgRepository{
|
||||
return &PGRepository{
|
||||
cfg.DB,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (repo *pgRepository) Store(ctx context.Context, input *model.UserInput) (*model.User, error) {
|
||||
func (repo *PGRepository) Store(ctx context.Context, input *model.UserInput) (*model.User, error) {
|
||||
item := input.ToUser()
|
||||
if _, err := repo.
|
||||
Model(item).
|
||||
|
@ -43,7 +45,7 @@ func (repo *pgRepository) Store(ctx context.Context, input *model.UserInput) (*m
|
|||
return item, nil
|
||||
}
|
||||
|
||||
func (repo *pgRepository) UpdateMany(ctx context.Context, f *model.UserFilter, input *model.UserInput) ([]*model.User, error) {
|
||||
func (repo *PGRepository) UpdateMany(ctx context.Context, f *model.UserFilter, input *model.UserInput) ([]*model.User, error) {
|
||||
if _, err := repo.
|
||||
Model(&model.User{}).
|
||||
Context(ctx).
|
||||
|
@ -62,7 +64,7 @@ func (repo *pgRepository) UpdateMany(ctx context.Context, f *model.UserFilter, i
|
|||
return items, nil
|
||||
}
|
||||
|
||||
func (repo *pgRepository) Delete(ctx context.Context, f *model.UserFilter) ([]*model.User, error) {
|
||||
func (repo *PGRepository) Delete(ctx context.Context, f *model.UserFilter) ([]*model.User, error) {
|
||||
items := make([]*model.User, 0)
|
||||
if _, err := repo.
|
||||
Model(&items).
|
||||
|
@ -75,7 +77,7 @@ func (repo *pgRepository) Delete(ctx context.Context, f *model.UserFilter) ([]*m
|
|||
return items, nil
|
||||
}
|
||||
|
||||
func (repo *pgRepository) Fetch(ctx context.Context, cfg *user.FetchConfig) ([]*model.User, int, error) {
|
||||
func (repo *PGRepository) Fetch(ctx context.Context, cfg *user.FetchConfig) ([]*model.User, int, error) {
|
||||
var err error
|
||||
items := make([]*model.User, 0)
|
||||
total := 0
|
||||
|
|
|
@ -9,31 +9,33 @@ import (
|
|||
"github.com/zdam-egzamin-zawodowy/backend/internal/user"
|
||||
)
|
||||
|
||||
type usecase struct {
|
||||
userRepository user.Repository
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
UserRepository user.Repository
|
||||
}
|
||||
|
||||
func New(cfg *Config) (user.Usecase, error) {
|
||||
type Usecase struct {
|
||||
userRepository user.Repository
|
||||
}
|
||||
|
||||
var _ user.Usecase = &Usecase{}
|
||||
|
||||
func New(cfg *Config) (*Usecase, error) {
|
||||
if cfg == nil || cfg.UserRepository == nil {
|
||||
return nil, errors.New("cfg.UserRepository is required")
|
||||
}
|
||||
return &usecase{
|
||||
return &Usecase{
|
||||
cfg.UserRepository,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (ucase *usecase) Store(ctx context.Context, input *model.UserInput) (*model.User, error) {
|
||||
func (ucase *Usecase) Store(ctx context.Context, input *model.UserInput) (*model.User, error) {
|
||||
if err := validateInput(input.Sanitize(), validateOptions{false}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ucase.userRepository.Store(ctx, input)
|
||||
}
|
||||
|
||||
func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *model.UserInput) (*model.User, error) {
|
||||
func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *model.UserInput) (*model.User, error) {
|
||||
if id <= 0 {
|
||||
return nil, errors.New(messageInvalidID)
|
||||
}
|
||||
|
@ -53,7 +55,7 @@ func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *model.Us
|
|||
return items[0], nil
|
||||
}
|
||||
|
||||
func (ucase *usecase) UpdateMany(ctx context.Context, f *model.UserFilter, input *model.UserInput) ([]*model.User, error) {
|
||||
func (ucase *Usecase) UpdateMany(ctx context.Context, f *model.UserFilter, input *model.UserInput) ([]*model.User, error) {
|
||||
if f == nil {
|
||||
return []*model.User{}, nil
|
||||
}
|
||||
|
@ -67,11 +69,11 @@ func (ucase *usecase) UpdateMany(ctx context.Context, f *model.UserFilter, input
|
|||
return items, nil
|
||||
}
|
||||
|
||||
func (ucase *usecase) Delete(ctx context.Context, f *model.UserFilter) ([]*model.User, error) {
|
||||
func (ucase *Usecase) Delete(ctx context.Context, f *model.UserFilter) ([]*model.User, error) {
|
||||
return ucase.userRepository.Delete(ctx, f)
|
||||
}
|
||||
|
||||
func (ucase *usecase) Fetch(ctx context.Context, cfg *user.FetchConfig) ([]*model.User, int, error) {
|
||||
func (ucase *Usecase) Fetch(ctx context.Context, cfg *user.FetchConfig) ([]*model.User, int, error) {
|
||||
if cfg == nil {
|
||||
cfg = &user.FetchConfig{
|
||||
Limit: user.FetchMaxLimit,
|
||||
|
@ -87,7 +89,7 @@ func (ucase *usecase) Fetch(ctx context.Context, cfg *user.FetchConfig) ([]*mode
|
|||
return ucase.userRepository.Fetch(ctx, cfg)
|
||||
}
|
||||
|
||||
func (ucase *usecase) GetByID(ctx context.Context, id int) (*model.User, error) {
|
||||
func (ucase *Usecase) GetByID(ctx context.Context, id int) (*model.User, error) {
|
||||
items, _, err := ucase.Fetch(ctx, &user.FetchConfig{
|
||||
Limit: 1,
|
||||
Count: false,
|
||||
|
@ -104,7 +106,7 @@ func (ucase *usecase) GetByID(ctx context.Context, id int) (*model.User, error)
|
|||
return items[0], nil
|
||||
}
|
||||
|
||||
func (ucase *usecase) GetByCredentials(ctx context.Context, email, password string) (*model.User, error) {
|
||||
func (ucase *Usecase) GetByCredentials(ctx context.Context, email, password string) (*model.User, error) {
|
||||
items, _, err := ucase.Fetch(ctx, &user.FetchConfig{
|
||||
Limit: 1,
|
||||
Count: false,
|
||||
|
|
Reference in New Issue