diff --git a/internal/graphql/generated/generated.go b/internal/graphql/generated/generated.go index c57492b..cf73fb2 100644 --- a/internal/graphql/generated/generated.go +++ b/internal/graphql/generated/generated.go @@ -907,13 +907,13 @@ extend type Query { extend type Mutation { createProfession(input: ProfessionInput!): Profession @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) updateProfession(id: ID!, input: ProfessionInput!): Profession @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) deleteProfessions(ids: [ID!]!): [Profession!] @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) } `, BuiltIn: false}, {Name: "schema/qualification.graphql", Input: `type Qualification { @@ -995,13 +995,13 @@ extend type Query { extend type Mutation { createQualification(input: QualificationInput!): Qualification @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) updateQualification(id: ID!, input: QualificationInput!): Qualification @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) deleteQualifications(ids: [ID!]!): [Qualification!] @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) } `, BuiltIn: false}, {Name: "schema/question.graphql", Input: `enum Answer { @@ -1084,28 +1084,28 @@ extend type Query { limit: Int offset: Int sort: [String!] - ): QuestionList! @authenticated(yes: true) @hasRole(role: ADMIN) + ): QuestionList! @authenticated(yes: true) @hasRole(role: Admin) generateTest(qualificationIDs: [ID!]!, limit: Int): [Question!] } extend type Mutation { createQuestion(input: QuestionInput!): Question @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) updateQuestion(id: ID!, input: QuestionInput!): Question @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) deleteQuestions(ids: [ID!]!): [Question!] @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) } `, BuiltIn: false}, {Name: "schema/scalars.graphql", Input: `scalar Time scalar Upload `, BuiltIn: false}, {Name: "schema/user.graphql", Input: `enum Role { - ADMIN - USER + Admin + User } type User { @@ -1172,24 +1172,24 @@ extend type Query { limit: Int offset: Int sort: [String!] - ): UserList! @authenticated(yes: true) @hasRole(role: ADMIN) - user(id: Int!): User @authenticated(yes: true) @hasRole(role: ADMIN) + ): UserList! @authenticated(yes: true) @hasRole(role: Admin) + user(id: Int!): User @authenticated(yes: true) @hasRole(role: Admin) me: User } extend type Mutation { createUser(input: UserInput!): User @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) updateUser(id: ID!, input: UserInput!): User @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) updateManyUsers(ids: [ID!]!, input: UpdateManyUsersInput!): [User!] @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) deleteUsers(ids: [ID!]!): [User!] @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) signIn( email: String! password: String! @@ -1854,7 +1854,7 @@ func (ec *executionContext) _Mutation_createProfession(ctx context.Context, fiel return ec.directives.Authenticated(ctx, nil, directive0, yes) } directive2 := func(ctx context.Context) (interface{}, error) { - role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "ADMIN") + role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "Admin") if err != nil { return nil, err } @@ -1927,7 +1927,7 @@ func (ec *executionContext) _Mutation_updateProfession(ctx context.Context, fiel return ec.directives.Authenticated(ctx, nil, directive0, yes) } directive2 := func(ctx context.Context) (interface{}, error) { - role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "ADMIN") + role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "Admin") if err != nil { return nil, err } @@ -2000,7 +2000,7 @@ func (ec *executionContext) _Mutation_deleteProfessions(ctx context.Context, fie return ec.directives.Authenticated(ctx, nil, directive0, yes) } directive2 := func(ctx context.Context) (interface{}, error) { - role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "ADMIN") + role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "Admin") if err != nil { return nil, err } @@ -2073,7 +2073,7 @@ func (ec *executionContext) _Mutation_createQualification(ctx context.Context, f return ec.directives.Authenticated(ctx, nil, directive0, yes) } directive2 := func(ctx context.Context) (interface{}, error) { - role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "ADMIN") + role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "Admin") if err != nil { return nil, err } @@ -2146,7 +2146,7 @@ func (ec *executionContext) _Mutation_updateQualification(ctx context.Context, f return ec.directives.Authenticated(ctx, nil, directive0, yes) } directive2 := func(ctx context.Context) (interface{}, error) { - role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "ADMIN") + role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "Admin") if err != nil { return nil, err } @@ -2219,7 +2219,7 @@ func (ec *executionContext) _Mutation_deleteQualifications(ctx context.Context, return ec.directives.Authenticated(ctx, nil, directive0, yes) } directive2 := func(ctx context.Context) (interface{}, error) { - role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "ADMIN") + role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "Admin") if err != nil { return nil, err } @@ -2292,7 +2292,7 @@ func (ec *executionContext) _Mutation_createQuestion(ctx context.Context, field return ec.directives.Authenticated(ctx, nil, directive0, yes) } directive2 := func(ctx context.Context) (interface{}, error) { - role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "ADMIN") + role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "Admin") if err != nil { return nil, err } @@ -2365,7 +2365,7 @@ func (ec *executionContext) _Mutation_updateQuestion(ctx context.Context, field return ec.directives.Authenticated(ctx, nil, directive0, yes) } directive2 := func(ctx context.Context) (interface{}, error) { - role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "ADMIN") + role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "Admin") if err != nil { return nil, err } @@ -2438,7 +2438,7 @@ func (ec *executionContext) _Mutation_deleteQuestions(ctx context.Context, field return ec.directives.Authenticated(ctx, nil, directive0, yes) } directive2 := func(ctx context.Context) (interface{}, error) { - role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "ADMIN") + role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "Admin") if err != nil { return nil, err } @@ -2511,7 +2511,7 @@ func (ec *executionContext) _Mutation_createUser(ctx context.Context, field grap return ec.directives.Authenticated(ctx, nil, directive0, yes) } directive2 := func(ctx context.Context) (interface{}, error) { - role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "ADMIN") + role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "Admin") if err != nil { return nil, err } @@ -2584,7 +2584,7 @@ func (ec *executionContext) _Mutation_updateUser(ctx context.Context, field grap return ec.directives.Authenticated(ctx, nil, directive0, yes) } directive2 := func(ctx context.Context) (interface{}, error) { - role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "ADMIN") + role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "Admin") if err != nil { return nil, err } @@ -2657,7 +2657,7 @@ func (ec *executionContext) _Mutation_updateManyUsers(ctx context.Context, field return ec.directives.Authenticated(ctx, nil, directive0, yes) } directive2 := func(ctx context.Context) (interface{}, error) { - role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "ADMIN") + role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "Admin") if err != nil { return nil, err } @@ -2730,7 +2730,7 @@ func (ec *executionContext) _Mutation_deleteUsers(ctx context.Context, field gra return ec.directives.Authenticated(ctx, nil, directive0, yes) } directive2 := func(ctx context.Context) (interface{}, error) { - role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "ADMIN") + role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "Admin") if err != nil { return nil, err } @@ -3573,7 +3573,7 @@ func (ec *executionContext) _Query_questions(ctx context.Context, field graphql. return ec.directives.Authenticated(ctx, nil, directive0, yes) } directive2 := func(ctx context.Context) (interface{}, error) { - role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "ADMIN") + role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "Admin") if err != nil { return nil, err } @@ -3688,7 +3688,7 @@ func (ec *executionContext) _Query_users(ctx context.Context, field graphql.Coll return ec.directives.Authenticated(ctx, nil, directive0, yes) } directive2 := func(ctx context.Context) (interface{}, error) { - role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "ADMIN") + role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "Admin") if err != nil { return nil, err } @@ -3764,7 +3764,7 @@ func (ec *executionContext) _Query_user(ctx context.Context, field graphql.Colle return ec.directives.Authenticated(ctx, nil, directive0, yes) } directive2 := func(ctx context.Context) (interface{}, error) { - role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "ADMIN") + role, err := ec.unmarshalNRole2githubᚗcomᚋzdamᚑegzaminᚑzawodowyᚋbackendᚋinternalᚋmodelsᚐRole(ctx, "Admin") if err != nil { return nil, err } diff --git a/internal/graphql/schema/profession.graphql b/internal/graphql/schema/profession.graphql index 50ac1bf..b7ea76e 100644 --- a/internal/graphql/schema/profession.graphql +++ b/internal/graphql/schema/profession.graphql @@ -51,11 +51,11 @@ extend type Query { extend type Mutation { createProfession(input: ProfessionInput!): Profession @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) updateProfession(id: ID!, input: ProfessionInput!): Profession @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) deleteProfessions(ids: [ID!]!): [Profession!] @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) } diff --git a/internal/graphql/schema/qualification.graphql b/internal/graphql/schema/qualification.graphql index 9610d22..d07f111 100644 --- a/internal/graphql/schema/qualification.graphql +++ b/internal/graphql/schema/qualification.graphql @@ -77,11 +77,11 @@ extend type Query { extend type Mutation { createQualification(input: QualificationInput!): Qualification @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) updateQualification(id: ID!, input: QualificationInput!): Qualification @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) deleteQualifications(ids: [ID!]!): [Qualification!] @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) } diff --git a/internal/graphql/schema/question.graphql b/internal/graphql/schema/question.graphql index be09f8e..024dd6d 100644 --- a/internal/graphql/schema/question.graphql +++ b/internal/graphql/schema/question.graphql @@ -78,18 +78,18 @@ extend type Query { limit: Int offset: Int sort: [String!] - ): QuestionList! @authenticated(yes: true) @hasRole(role: ADMIN) + ): QuestionList! @authenticated(yes: true) @hasRole(role: Admin) generateTest(qualificationIDs: [ID!]!, limit: Int): [Question!] } extend type Mutation { createQuestion(input: QuestionInput!): Question @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) updateQuestion(id: ID!, input: QuestionInput!): Question @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) deleteQuestions(ids: [ID!]!): [Question!] @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) } diff --git a/internal/graphql/schema/user.graphql b/internal/graphql/schema/user.graphql index 9cf23ee..34b00f1 100644 --- a/internal/graphql/schema/user.graphql +++ b/internal/graphql/schema/user.graphql @@ -1,6 +1,6 @@ enum Role { - ADMIN - USER + Admin + User } type User { @@ -67,24 +67,24 @@ extend type Query { limit: Int offset: Int sort: [String!] - ): UserList! @authenticated(yes: true) @hasRole(role: ADMIN) - user(id: Int!): User @authenticated(yes: true) @hasRole(role: ADMIN) + ): UserList! @authenticated(yes: true) @hasRole(role: Admin) + user(id: Int!): User @authenticated(yes: true) @hasRole(role: Admin) me: User } extend type Mutation { createUser(input: UserInput!): User @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) updateUser(id: ID!, input: UserInput!): User @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) updateManyUsers(ids: [ID!]!, input: UpdateManyUsersInput!): [User!] @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) deleteUsers(ids: [ID!]!): [User!] @authenticated(yes: true) - @hasRole(role: ADMIN) + @hasRole(role: Admin) signIn( email: String! password: String! diff --git a/internal/models/profession.go b/internal/models/profession.go index efcf31a..628558d 100644 --- a/internal/models/profession.go +++ b/internal/models/profession.go @@ -70,10 +70,10 @@ func (input *ProfessionInput) ToProfession() *Profession { func (input *ProfessionInput) ApplyUpdate(q *orm.Query) (*orm.Query, error) { if !input.IsEmpty() { if input.Name != nil { - q.Set("name = ?", *input.Name) + q = q.Set("name = ?", *input.Name) } if input.Description != nil { - q.Set("description = ?", *input.Description) + q = q.Set("description = ?", *input.Description) } } diff --git a/internal/models/qualification.go b/internal/models/qualification.go index 2895f9a..f3d901c 100644 --- a/internal/models/qualification.go +++ b/internal/models/qualification.go @@ -42,7 +42,7 @@ type QualificationToProfession struct { QualificationID int `pg:"on_delete:CASCADE,unique:group_1" json:"qualificationID" xml:"qualificationID" gqlgen:"qualificationID"` Qualification *Qualification `pg:"rel:has-one" json:"qualification" xml:"qualification" gqlgen:"qualification"` ProfessionID int `pg:"on_delete:CASCADE,unique:group_1" json:"professionID" xml:"professionID" gqlgen:"professionID"` - Profession *Qualification `pg:"rel:has-one" json:"profession" xml:"profession" gqlgen:"profession"` + Profession *Profession `pg:"rel:has-one" json:"profession" xml:"profession" gqlgen:"profession"` } type QualificationInput struct { @@ -101,10 +101,16 @@ func (input *QualificationInput) ToQualification() *Qualification { func (input *QualificationInput) ApplyUpdate(q *orm.Query) (*orm.Query, error) { if !input.IsEmpty() { if input.Name != nil { - q.Set("name = ?", *input.Name) + q = q.Set("name = ?", *input.Name) + } + if input.Code != nil { + q = q.Set("code = ?", *input.Code) + } + if input.Formula != nil { + q = q.Set("formula = ?", *input.Formula) } if input.Description != nil { - q.Set("description = ?", *input.Description) + q = q.Set("description = ?", *input.Description) } } diff --git a/internal/models/question.go b/internal/models/question.go index be7257d..24c4293 100644 --- a/internal/models/question.go +++ b/internal/models/question.go @@ -130,31 +130,31 @@ func (input *QuestionInput) ToQuestion() *Question { func (input *QuestionInput) ApplyUpdate(q *orm.Query) (*orm.Query, error) { if !input.IsEmpty() { if input.Content != nil { - q.Set("content = ?", *input.Content) + q = q.Set("content = ?", *input.Content) } if input.From != nil { - q.Set("from = ?", *input.From) + q = q.Set("from = ?", *input.From) } if input.Explanation != nil { - q.Set("explanation = ?", *input.Explanation) + q = q.Set("explanation = ?", *input.Explanation) } if input.CorrectAnswer != nil { - q.Set("correct_answer = ?", *input.CorrectAnswer) + q = q.Set("correct_answer = ?", *input.CorrectAnswer) } if input.AnswerA != nil { - q.Set("answer_a = ?", *input.AnswerA) + q = q.Set("answer_a = ?", *input.AnswerA) } if input.AnswerB != nil { - q.Set("answer_b = ?", *input.AnswerB) + q = q.Set("answer_b = ?", *input.AnswerB) } if input.AnswerC != nil { - q.Set("answer_c = ?", *input.AnswerC) + q = q.Set("answer_c = ?", *input.AnswerC) } if input.AnswerD != nil { - q.Set("answer_d = ?", *input.AnswerD) + q = q.Set("answer_d = ?", *input.AnswerD) } if input.QualificationID != nil { - q.Set("qualification_id = ?", *input.QualificationID) + q = q.Set("qualification_id = ?", *input.QualificationID) } } diff --git a/internal/models/user.go b/internal/models/user.go index 59027e5..ed5013b 100644 --- a/internal/models/user.go +++ b/internal/models/user.go @@ -104,7 +104,7 @@ func (input *UserInput) ToUser() *User { func (input *UserInput) ApplyUpdate(q *orm.Query) (*orm.Query, error) { if !input.IsEmpty() { if input.DisplayName != nil { - q.Set("display_name = ?", *input.DisplayName) + q = q.Set("display_name = ?", *input.DisplayName) } if input.Password != nil { @@ -112,19 +112,19 @@ func (input *UserInput) ApplyUpdate(q *orm.Query) (*orm.Query, error) { if err != nil { return q, err } - q.Set("password = ?", string(hashedPassword)) + q = q.Set("password = ?", string(hashedPassword)) } if input.Email != nil { - q.Set("name = ?", *input.Email) + q = q.Set("name = ?", *input.Email) } if input.Role != nil { - q.Set("role = ?", *input.Role) + q = q.Set("role = ?", *input.Role) } if input.Activated != nil { - q.Set("activated = ?", *input.Activated) + q = q.Set("activated = ?", *input.Activated) } } diff --git a/internal/profession/repository/pg_repository.go b/internal/profession/repository/pg_repository.go index d6e318f..8464ff6 100644 --- a/internal/profession/repository/pg_repository.go +++ b/internal/profession/repository/pg_repository.go @@ -45,11 +45,9 @@ func (repo *pgRepository) Store(ctx context.Context, input *models.ProfessionInp } func (repo *pgRepository) UpdateMany(ctx context.Context, f *models.ProfessionFilter, input *models.ProfessionInput) ([]*models.Profession, error) { - items := []*models.Profession{} if _, err := repo. - Model(&items). + Model(&models.Profession{}). Context(ctx). - Returning("*"). Apply(input.ApplyUpdate). Apply(f.Where). Update(); err != nil && err != pg.ErrNoRows { @@ -58,6 +56,13 @@ func (repo *pgRepository) UpdateMany(ctx context.Context, f *models.ProfessionFi } return nil, errorutils.Wrap(err, messageFailedToSaveModel) } + items, _, err := repo.Fetch(ctx, &profession.FetchConfig{ + Count: false, + Filter: f, + }) + if err != nil { + return nil, err + } return items, nil } diff --git a/internal/qualification/repository/pg_repository.go b/internal/qualification/repository/pg_repository.go index 07b18a7..cdaabaa 100644 --- a/internal/qualification/repository/pg_repository.go +++ b/internal/qualification/repository/pg_repository.go @@ -5,6 +5,8 @@ import ( "fmt" "strings" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" sqlutils "github.com/zdam-egzamin-zawodowy/backend/pkg/utils/sql" errorutils "github.com/zdam-egzamin-zawodowy/backend/pkg/utils/error" @@ -68,19 +70,28 @@ func (repo *pgRepository) UpdateMany( ) ([]*models.Qualification, error) { items := []*models.Qualification{} err := repo.RunInTransaction(ctx, func(tx *pg.Tx) error { - if _, err := tx. + if input.Name != nil || input.Code != nil || input.Description != nil || input.Formula != nil { + if _, err := tx. + Model(&models.Qualification{}). + Context(ctx). + Apply(input.ApplyUpdate). + Apply(f.Where). + Update(); err != nil && err != pg.ErrNoRows { + if strings.Contains(err.Error(), "name") { + return errorutils.Wrap(err, messageNameIsAlreadyTaken) + } else if strings.Contains(err.Error(), "code") { + return errorutils.Wrap(err, messageCodeIsAlreadyTaken) + } + return errorutils.Wrap(err, messageFailedToSaveModel) + } + } + + if err := tx. Model(&items). Context(ctx). - Returning("*"). - Apply(input.ApplyUpdate). Apply(f.Where). - Update(); err != nil && err != pg.ErrNoRows { - if strings.Contains(err.Error(), "name") { - return errorutils.Wrap(err, messageNameIsAlreadyTaken) - } else if strings.Contains(err.Error(), "code") { - return errorutils.Wrap(err, messageCodeIsAlreadyTaken) - } - return errorutils.Wrap(err, messageFailedToSaveModel) + Select(); err != nil && err != pg.ErrNoRows { + return errorutils.Wrap(err, messageFailedToFetchModel) } qualificationIDs := make([]int, len(items)) @@ -107,7 +118,10 @@ func (repo *pgRepository) UpdateMany( }) } } - tx.Model(&toInsert).Insert() + _, err := tx.Model(&toInsert).Insert() + if err != nil { + logrus.Debug(errors.Wrap(err, "Couldn't insert []*models.QualificationToProfession{}")) + } } } diff --git a/internal/qualification/usecase/message.go b/internal/qualification/usecase/message.go index c4d9a7a..764d1ba 100644 --- a/internal/qualification/usecase/message.go +++ b/internal/qualification/usecase/message.go @@ -2,9 +2,9 @@ package usecase const ( messageInvalidID = "Niepoprawne ID." - messageItemNotFound = "Nie znaleziono zawodu." + messageItemNotFound = "Nie znaleziono kwalifikacji." messageEmptyPayload = "Nie wprowadzono jakichkolwiek danych." - messageNameIsRequired = "Nazwa zawodu jest wymagana." + messageNameIsRequired = "Nazwa kwalifikacji jest wymagana." messageCodeIsRequired = "Oznaczenie kwalifikacji jest wymagane." - messageNameIsTooLong = "Nazwa zawodu może się składać z maksymalnie %d znaków." + messageNameIsTooLong = "Nazwa kwalifikacji może się składać z maksymalnie %d znaków." ) diff --git a/internal/user/repository/pg_repository.go b/internal/user/repository/pg_repository.go index e8dab76..88ddb9e 100644 --- a/internal/user/repository/pg_repository.go +++ b/internal/user/repository/pg_repository.go @@ -45,11 +45,9 @@ func (repo *pgRepository) Store(ctx context.Context, input *models.UserInput) (* } func (repo *pgRepository) UpdateMany(ctx context.Context, f *models.UserFilter, input *models.UserInput) ([]*models.User, error) { - items := []*models.User{} if _, err := repo. - Model(&items). + Model(&models.User{}). Context(ctx). - Returning("*"). Apply(input.ApplyUpdate). Apply(f.Where). Update(); err != nil && err != pg.ErrNoRows { @@ -58,6 +56,13 @@ func (repo *pgRepository) UpdateMany(ctx context.Context, f *models.UserFilter, } return nil, errorutils.Wrap(err, messageFailedToSaveModel) } + items, _, err := repo.Fetch(ctx, &user.FetchConfig{ + Count: false, + Filter: f, + }) + if err != nil { + return nil, err + } return items, nil } diff --git a/internal/user/usecase/usecase.go b/internal/user/usecase/usecase.go index 2b0eb7e..745b6c7 100644 --- a/internal/user/usecase/usecase.go +++ b/internal/user/usecase/usecase.go @@ -162,7 +162,7 @@ func (ucase *usecase) validateInput(input *models.UserInput, opts validateOption return fmt.Errorf(messageInvalidRole) } } else if !opts.acceptNilValues { - return fmt.Errorf(messagePasswordIsRequired) + return fmt.Errorf(messageInvalidRole) } return nil