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"
|
|
|
|
)
|
|
|
|
|
2020-08-13 06:28:18 +00:00
|
|
|
func ApplicationByID(db *gorm.DB, table, projectID, appID string) (*model.ApplicationView, error) {
|
2020-05-11 10:16:29 +00:00
|
|
|
app := new(model.ApplicationView)
|
2021-03-01 07:48:50 +00:00
|
|
|
projectIDQuery := &model.ApplicationSearchQuery{Key: proj_model.AppSearchKeyProjectID, Value: projectID, Method: domain.SearchMethodEquals}
|
|
|
|
appIDQuery := &model.ApplicationSearchQuery{Key: proj_model.AppSearchKeyAppID, Value: appID, Method: domain.SearchMethodEquals}
|
2020-08-13 06:28:18 +00:00
|
|
|
query := repository.PrepareGetByQuery(table, projectIDQuery, appIDQuery)
|
2020-05-11 10:16:29 +00:00
|
|
|
err := query(db, app)
|
2020-07-06 11:18:10 +00:00
|
|
|
if caos_errs.IsNotFound(err) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-DGdfx", "Errors.Application.NotFound")
|
|
|
|
}
|
2020-05-11 10:16:29 +00:00
|
|
|
return app, err
|
|
|
|
}
|
|
|
|
|
2020-08-05 16:32:25 +00:00
|
|
|
func ApplicationsByProjectID(db *gorm.DB, table, projectID string) ([]*model.ApplicationView, error) {
|
|
|
|
applications := make([]*model.ApplicationView, 0)
|
|
|
|
queries := []*proj_model.ApplicationSearchQuery{
|
2021-03-01 07:48:50 +00:00
|
|
|
{Key: proj_model.AppSearchKeyProjectID, Value: projectID, Method: domain.SearchMethodEquals},
|
2020-08-05 16:32:25 +00:00
|
|
|
}
|
|
|
|
query := repository.PrepareSearchQuery(table, model.ApplicationSearchRequest{Queries: queries})
|
|
|
|
_, err := query(db, &applications)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return applications, nil
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:50:04 +00:00
|
|
|
func ApplicationByOIDCClientID(db *gorm.DB, table, clientID string) (*model.ApplicationView, error) {
|
|
|
|
app := new(model.ApplicationView)
|
2021-03-01 07:48:50 +00:00
|
|
|
clientIDQuery := model.ApplicationSearchQuery{Key: proj_model.AppSearchKeyOIDCClientID, Value: clientID, Method: domain.SearchMethodEquals}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareGetByQuery(table, clientIDQuery)
|
2020-06-05 05:50:04 +00:00
|
|
|
err := query(db, app)
|
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-DG1qh", "Errors.Project.App.NotFound")
|
2020-07-06 11:18:10 +00:00
|
|
|
}
|
2020-06-05 05:50:04 +00:00
|
|
|
return app, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func ApplicationByProjectIDAndAppName(db *gorm.DB, table, projectID, appName string) (*model.ApplicationView, error) {
|
|
|
|
app := new(model.ApplicationView)
|
2021-03-01 07:48:50 +00:00
|
|
|
projectIDQuery := model.ApplicationSearchQuery{Key: proj_model.AppSearchKeyProjectID, Value: projectID, Method: domain.SearchMethodEquals}
|
|
|
|
appNameQuery := model.ApplicationSearchQuery{Key: proj_model.AppSearchKeyName, Value: appName, Method: domain.SearchMethodEquals}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareGetByQuery(table, projectIDQuery, appNameQuery)
|
2020-06-05 05:50:04 +00:00
|
|
|
err := query(db, app)
|
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-Jqw1z", "Errors.Project.App.NotFound")
|
2020-07-06 11:18:10 +00:00
|
|
|
}
|
2020-06-05 05:50:04 +00:00
|
|
|
return app, err
|
|
|
|
}
|
|
|
|
|
2020-07-30 12:37:55 +00:00
|
|
|
func SearchApplications(db *gorm.DB, table string, req *proj_model.ApplicationSearchRequest) ([]*model.ApplicationView, uint64, error) {
|
2020-05-11 10:16:29 +00:00
|
|
|
apps := make([]*model.ApplicationView, 0)
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.ApplicationSearchRequest{Limit: req.Limit, Offset: req.Offset, Queries: req.Queries})
|
2020-05-11 10:16:29 +00:00
|
|
|
count, err := query(db, &apps)
|
|
|
|
return apps, count, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func PutApplication(db *gorm.DB, table string, app *model.ApplicationView) error {
|
2020-06-25 06:01:13 +00:00
|
|
|
save := repository.PrepareSave(table)
|
2020-05-11 10:16:29 +00:00
|
|
|
return save(db, app)
|
|
|
|
}
|
|
|
|
|
2020-10-16 05:49:38 +00:00
|
|
|
func PutApplications(db *gorm.DB, table string, apps ...*model.ApplicationView) error {
|
|
|
|
save := repository.PrepareBulkSave(table)
|
|
|
|
s := make([]interface{}, len(apps))
|
|
|
|
for i, app := range apps {
|
|
|
|
s[i] = app
|
|
|
|
}
|
|
|
|
return save(db, s...)
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:29 +00:00
|
|
|
func DeleteApplication(db *gorm.DB, table, appID string) error {
|
2020-06-25 06:01:13 +00:00
|
|
|
delete := repository.PrepareDeleteByKey(table, model.ApplicationSearchKey(proj_model.AppSearchKeyAppID), appID)
|
2020-05-11 10:16:29 +00:00
|
|
|
return delete(db)
|
|
|
|
}
|
2020-08-05 16:32:25 +00:00
|
|
|
|
|
|
|
func DeleteApplicationsByProjectID(db *gorm.DB, table, projectID string) error {
|
|
|
|
delete := repository.PrepareDeleteByKey(table, model.ApplicationSearchKey(proj_model.AppSearchKeyProjectID), projectID)
|
|
|
|
return delete(db)
|
|
|
|
}
|