2020-05-11 10:16:29 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2021-03-01 07:48:50 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2020-07-06 11:18:10 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2020-05-11 10:16:29 +00:00
|
|
|
proj_model "github.com/caos/zitadel/internal/project/model"
|
|
|
|
"github.com/caos/zitadel/internal/project/repository/view/model"
|
2020-06-25 06:01:13 +00:00
|
|
|
"github.com/caos/zitadel/internal/view/repository"
|
2020-05-11 10:16:29 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ProjectMemberByIDs(db *gorm.DB, table, projectID, userID string) (*model.ProjectMemberView, error) {
|
|
|
|
role := new(model.ProjectMemberView)
|
|
|
|
|
2021-03-01 07:48:50 +00:00
|
|
|
projectIDQuery := model.ProjectMemberSearchQuery{Key: proj_model.ProjectMemberSearchKeyProjectID, Value: projectID, Method: domain.SearchMethodEquals}
|
|
|
|
userIDQuery := model.ProjectMemberSearchQuery{Key: proj_model.ProjectMemberSearchKeyUserID, Value: userID, Method: domain.SearchMethodEquals}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareGetByQuery(table, projectIDQuery, userIDQuery)
|
2020-05-11 10:16:29 +00:00
|
|
|
err := query(db, role)
|
2020-07-06 11:18:10 +00:00
|
|
|
if caos_errs.IsNotFound(err) {
|
2021-01-28 05:35:26 +00:00
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-EgWQ2", "Errors.Project.Member.NotExisting")
|
2020-07-06 11:18:10 +00:00
|
|
|
}
|
2020-05-11 10:16:29 +00:00
|
|
|
return role, err
|
|
|
|
}
|
|
|
|
|
2020-08-05 16:32:25 +00:00
|
|
|
func ProjectMembersByProjectID(db *gorm.DB, table, projectID string) ([]*model.ProjectMemberView, error) {
|
|
|
|
members := make([]*model.ProjectMemberView, 0)
|
|
|
|
queries := []*proj_model.ProjectMemberSearchQuery{
|
2021-03-01 07:48:50 +00:00
|
|
|
&proj_model.ProjectMemberSearchQuery{Key: proj_model.ProjectMemberSearchKeyProjectID, Value: projectID, Method: domain.SearchMethodEquals},
|
2020-08-05 16:32:25 +00:00
|
|
|
}
|
|
|
|
query := repository.PrepareSearchQuery(table, model.ProjectMemberSearchRequest{Queries: queries})
|
|
|
|
_, err := query(db, &members)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return members, nil
|
|
|
|
}
|
|
|
|
|
2020-07-30 12:37:55 +00:00
|
|
|
func SearchProjectMembers(db *gorm.DB, table string, req *proj_model.ProjectMemberSearchRequest) ([]*model.ProjectMemberView, uint64, error) {
|
2020-05-11 10:16:29 +00:00
|
|
|
roles := make([]*model.ProjectMemberView, 0)
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.ProjectMemberSearchRequest{Limit: req.Limit, Offset: req.Offset, Queries: req.Queries})
|
2020-05-11 10:16:29 +00:00
|
|
|
count, err := query(db, &roles)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return roles, count, nil
|
|
|
|
}
|
|
|
|
func ProjectMembersByUserID(db *gorm.DB, table string, userID string) ([]*model.ProjectMemberView, error) {
|
|
|
|
members := make([]*model.ProjectMemberView, 0)
|
|
|
|
queries := []*proj_model.ProjectMemberSearchQuery{
|
2021-03-01 07:48:50 +00:00
|
|
|
&proj_model.ProjectMemberSearchQuery{Key: proj_model.ProjectMemberSearchKeyUserID, Value: userID, Method: domain.SearchMethodEquals},
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.ProjectMemberSearchRequest{Queries: queries})
|
2020-05-11 10:16:29 +00:00
|
|
|
_, err := query(db, &members)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return members, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func PutProjectMember(db *gorm.DB, table string, role *model.ProjectMemberView) error {
|
2020-06-25 06:01:13 +00:00
|
|
|
save := repository.PrepareSave(table)
|
2020-05-11 10:16:29 +00:00
|
|
|
return save(db, role)
|
|
|
|
}
|
|
|
|
|
2020-08-26 11:01:37 +00:00
|
|
|
func PutProjectMembers(db *gorm.DB, table string, members ...*model.ProjectMemberView) error {
|
|
|
|
save := repository.PrepareBulkSave(table)
|
|
|
|
m := make([]interface{}, len(members))
|
|
|
|
for i, member := range members {
|
|
|
|
m[i] = member
|
|
|
|
}
|
|
|
|
return save(db, m...)
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:29 +00:00
|
|
|
func DeleteProjectMember(db *gorm.DB, table, projectID, userID string) error {
|
|
|
|
role, err := ProjectMemberByIDs(db, table, projectID, userID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
delete := repository.PrepareDeleteByObject(table, role)
|
2020-05-11 10:16:29 +00:00
|
|
|
return delete(db)
|
|
|
|
}
|
2020-08-05 16:32:25 +00:00
|
|
|
|
|
|
|
func DeleteProjectMembersByProjectID(db *gorm.DB, table, projectID string) error {
|
|
|
|
delete := repository.PrepareDeleteByKey(table, model.ProjectMemberSearchKey(proj_model.ProjectMemberSearchKeyProjectID), projectID)
|
|
|
|
return delete(db)
|
|
|
|
}
|