2020-06-15 12:50:39 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2020-07-06 11:18:10 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2020-06-15 12:50:39 +00:00
|
|
|
global_model "github.com/caos/zitadel/internal/model"
|
|
|
|
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-06-15 12:50:39 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ProjectByID(db *gorm.DB, table, projectID string) (*model.ProjectView, error) {
|
|
|
|
project := new(model.ProjectView)
|
|
|
|
|
2020-06-23 12:47:47 +00:00
|
|
|
projectIDQuery := model.ProjectSearchQuery{Key: proj_model.ProjectViewSearchKeyProjectID, Value: projectID, Method: global_model.SearchMethodEquals}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareGetByQuery(table, projectIDQuery)
|
2020-06-15 12:50:39 +00:00
|
|
|
err := query(db, project)
|
2020-07-06 11:18:10 +00:00
|
|
|
if caos_errs.IsNotFound(err) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-NEO7W", "Errors.Project.NotFound")
|
|
|
|
}
|
2020-06-15 12:50:39 +00:00
|
|
|
return project, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectsByResourceOwner(db *gorm.DB, table, orgID string) ([]*model.ProjectView, error) {
|
|
|
|
projects := make([]*model.ProjectView, 0)
|
|
|
|
queries := []*proj_model.ProjectViewSearchQuery{
|
2020-07-07 17:28:19 +00:00
|
|
|
{Key: proj_model.ProjectViewSearchKeyResourceOwner, Value: orgID, Method: global_model.SearchMethodEquals},
|
2020-06-15 12:50:39 +00:00
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.ProjectSearchRequest{Queries: queries})
|
2020-06-15 12:50:39 +00:00
|
|
|
_, err := query(db, &projects)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return projects, nil
|
|
|
|
}
|
|
|
|
|
2020-07-30 12:37:55 +00:00
|
|
|
func SearchProjects(db *gorm.DB, table string, req *proj_model.ProjectViewSearchRequest) ([]*model.ProjectView, uint64, error) {
|
2020-06-15 12:50:39 +00:00
|
|
|
projects := make([]*model.ProjectView, 0)
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.ProjectSearchRequest{Limit: req.Limit, Offset: req.Offset, Queries: req.Queries})
|
2020-06-15 12:50:39 +00:00
|
|
|
count, err := query(db, &projects)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return projects, count, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func PutProject(db *gorm.DB, table string, project *model.ProjectView) error {
|
2020-06-25 06:01:13 +00:00
|
|
|
save := repository.PrepareSave(table)
|
2020-06-15 12:50:39 +00:00
|
|
|
return save(db, project)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteProject(db *gorm.DB, table, projectID string) error {
|
2020-06-25 06:01:13 +00:00
|
|
|
delete := repository.PrepareDeleteByKey(table, model.ProjectSearchKey(proj_model.ProjectViewSearchKeyProjectID), projectID)
|
2020-06-15 12:50:39 +00:00
|
|
|
return delete(db)
|
|
|
|
}
|