small refactor
This commit is contained in:
parent
e0fb674f17
commit
01ab1aa509
|
@ -27,7 +27,7 @@ func New(cfg *Config) (profession.Usecase, error) {
|
|||
}
|
||||
|
||||
func (ucase *usecase) Store(ctx context.Context, input *models.ProfessionInput) (*models.Profession, error) {
|
||||
if err := ucase.validateInput(input.Sanitize(), validateOptions{false}); err != nil {
|
||||
if err := validateInput(input.Sanitize(), validateOptions{false}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ucase.professionRepository.Store(ctx, input)
|
||||
|
@ -37,7 +37,7 @@ func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *models.P
|
|||
if id <= 0 {
|
||||
return nil, fmt.Errorf(messageInvalidID)
|
||||
}
|
||||
if err := ucase.validateInput(input.Sanitize(), validateOptions{true}); err != nil {
|
||||
if err := validateInput(input.Sanitize(), validateOptions{true}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items, err := ucase.professionRepository.UpdateMany(ctx,
|
||||
|
@ -104,10 +104,10 @@ func (ucase *usecase) GetBySlug(ctx context.Context, slug string) (*models.Profe
|
|||
}
|
||||
|
||||
type validateOptions struct {
|
||||
nameCanBeNil bool
|
||||
allowNilValues bool
|
||||
}
|
||||
|
||||
func (ucase *usecase) validateInput(input *models.ProfessionInput, opts validateOptions) error {
|
||||
func validateInput(input *models.ProfessionInput, opts validateOptions) error {
|
||||
if input.IsEmpty() {
|
||||
return fmt.Errorf(messageEmptyPayload)
|
||||
}
|
||||
|
@ -118,7 +118,7 @@ func (ucase *usecase) validateInput(input *models.ProfessionInput, opts validate
|
|||
} else if len(*input.Name) > profession.MaxNameLength {
|
||||
return fmt.Errorf(messageNameIsTooLong, profession.MaxNameLength)
|
||||
}
|
||||
} else if !opts.nameCanBeNil {
|
||||
} else if !opts.allowNilValues {
|
||||
return fmt.Errorf(messageNameIsRequired)
|
||||
}
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ func New(cfg *Config) (qualification.Usecase, error) {
|
|||
}
|
||||
|
||||
func (ucase *usecase) Store(ctx context.Context, input *models.QualificationInput) (*models.Qualification, error) {
|
||||
if err := ucase.validateInput(input.Sanitize(), validateOptions{false}); err != nil {
|
||||
if err := validateInput(input.Sanitize(), validateOptions{false}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ucase.qualificationRepository.Store(ctx, input)
|
||||
|
@ -37,7 +37,7 @@ func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *models.Q
|
|||
if id <= 0 {
|
||||
return nil, fmt.Errorf(messageInvalidID)
|
||||
}
|
||||
if err := ucase.validateInput(input.Sanitize(), validateOptions{true}); err != nil {
|
||||
if err := validateInput(input.Sanitize(), validateOptions{true}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items, err := ucase.qualificationRepository.UpdateMany(ctx,
|
||||
|
@ -107,7 +107,7 @@ type validateOptions struct {
|
|||
allowNilValues bool
|
||||
}
|
||||
|
||||
func (ucase *usecase) validateInput(input *models.QualificationInput, opts validateOptions) error {
|
||||
func validateInput(input *models.QualificationInput, opts validateOptions) error {
|
||||
if input.IsEmpty() {
|
||||
return fmt.Errorf(messageEmptyPayload)
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
imageAcceptedMIMETypes = map[string]bool{
|
||||
imageValidMIMETypes = map[string]bool{
|
||||
"image/jpeg": true,
|
||||
"image/jpg": true,
|
||||
"image/png": true,
|
||||
|
@ -35,7 +35,7 @@ func New(cfg *Config) (question.Usecase, error) {
|
|||
}
|
||||
|
||||
func (ucase *usecase) Store(ctx context.Context, input *models.QuestionInput) (*models.Question, error) {
|
||||
if err := ucase.validateInput(input.Sanitize(), validateOptions{false}); err != nil {
|
||||
if err := validateInput(input.Sanitize(), validateOptions{false}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ucase.questionRepository.Store(ctx, input)
|
||||
|
@ -45,7 +45,7 @@ func (ucase *usecase) UpdateOneByID(ctx context.Context, id int, input *models.Q
|
|||
if id <= 0 {
|
||||
return nil, fmt.Errorf(messageInvalidID)
|
||||
}
|
||||
if err := ucase.validateInput(input.Sanitize(), validateOptions{true}); err != nil {
|
||||
if err := validateInput(input.Sanitize(), validateOptions{true}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
item, err := ucase.questionRepository.UpdateOneByID(ctx,
|
||||
|
@ -111,7 +111,7 @@ type validateOptions struct {
|
|||
allowNilValues bool
|
||||
}
|
||||
|
||||
func (ucase *usecase) validateInput(input *models.QuestionInput, opts validateOptions) error {
|
||||
func validateInput(input *models.QuestionInput, opts validateOptions) error {
|
||||
if input.IsEmpty() {
|
||||
return fmt.Errorf(messageEmptyPayload)
|
||||
}
|
||||
|
@ -165,31 +165,31 @@ func (ucase *usecase) validateInput(input *models.QuestionInput, opts validateOp
|
|||
}
|
||||
|
||||
if input.Image != nil {
|
||||
if !imageAcceptedMIMETypes[input.Image.ContentType] {
|
||||
if validateMimeType(input.Image.ContentType) {
|
||||
return fmt.Errorf(messageImageNotAcceptableMIMEType, "Obrazek pytanie")
|
||||
}
|
||||
}
|
||||
|
||||
if input.AnswerAImage != nil {
|
||||
if !imageAcceptedMIMETypes[input.AnswerAImage.ContentType] {
|
||||
if !validateMimeType(input.AnswerAImage.ContentType) {
|
||||
return fmt.Errorf(messageAnswerIsInvalid, "Obrazek odpowiedź A")
|
||||
}
|
||||
}
|
||||
|
||||
if input.AnswerBImage != nil {
|
||||
if !imageAcceptedMIMETypes[input.AnswerBImage.ContentType] {
|
||||
if !validateMimeType(input.AnswerBImage.ContentType) {
|
||||
return fmt.Errorf(messageAnswerIsInvalid, "Obrazek odpowiedź B")
|
||||
}
|
||||
}
|
||||
|
||||
if input.AnswerCImage != nil {
|
||||
if !imageAcceptedMIMETypes[input.AnswerCImage.ContentType] {
|
||||
if !validateMimeType(input.AnswerCImage.ContentType) {
|
||||
return fmt.Errorf(messageAnswerIsInvalid, "Obrazek odpowiedź C")
|
||||
}
|
||||
}
|
||||
|
||||
if input.AnswerDImage != nil {
|
||||
if !imageAcceptedMIMETypes[input.AnswerDImage.ContentType] {
|
||||
if !validateMimeType(input.AnswerDImage.ContentType) {
|
||||
return fmt.Errorf(messageAnswerIsInvalid, "Obrazek odpowiedź D")
|
||||
}
|
||||
}
|
||||
|
@ -231,3 +231,7 @@ func (ucase *usecase) validateInput(input *models.QuestionInput, opts validateOp
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateMimeType(contentType string) bool {
|
||||
return imageValidMIMETypes[contentType]
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ func New(cfg *Config) (user.Usecase, error) {
|
|||
}
|
||||
|
||||
func (ucase *usecase) Store(ctx context.Context, input *models.UserInput) (*models.User, error) {
|
||||
if err := ucase.validateInput(input.Sanitize(), validateOptions{false}); err != nil {
|
||||
if err := validateInput(input.Sanitize(), validateOptions{false}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ucase.userRepository.Store(ctx, input)
|
||||
|
@ -58,7 +58,7 @@ func (ucase *usecase) UpdateMany(ctx context.Context, f *models.UserFilter, inpu
|
|||
if f == nil {
|
||||
return []*models.User{}, nil
|
||||
}
|
||||
if err := ucase.validateInput(input.Sanitize(), validateOptions{true}); err != nil {
|
||||
if err := validateInput(input.Sanitize(), validateOptions{true}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items, err := ucase.userRepository.UpdateMany(ctx, f, input)
|
||||
|
@ -124,7 +124,7 @@ type validateOptions struct {
|
|||
acceptNilValues bool
|
||||
}
|
||||
|
||||
func (ucase *usecase) validateInput(input *models.UserInput, opts validateOptions) error {
|
||||
func validateInput(input *models.UserInput, opts validateOptions) error {
|
||||
if input.IsEmpty() {
|
||||
return fmt.Errorf(messageEmptyPayload)
|
||||
}
|
||||
|
|
Reference in New Issue