zitadel/internal/project/repository/view/application_view.go
Livio Amstutz 26634505ba
fix: proper error message when user (and other objects) not found (#337)
* fix: proper error message when user not found by loginname

* add more not found and fix some typos
2020-07-06 13:18:10 +02:00

61 lines
2.6 KiB
Go

package view
import (
caos_errs "github.com/caos/zitadel/internal/errors"
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"
"github.com/caos/zitadel/internal/view/repository"
"github.com/jinzhu/gorm"
)
func ApplicationByID(db *gorm.DB, table, appID string) (*model.ApplicationView, error) {
app := new(model.ApplicationView)
query := repository.PrepareGetByKey(table, model.ApplicationSearchKey(proj_model.AppSearchKeyAppID), appID)
err := query(db, app)
if caos_errs.IsNotFound(err) {
return nil, caos_errs.ThrowNotFound(nil, "VIEW-DGdfx", "Errors.Application.NotFound")
}
return app, err
}
func ApplicationByOIDCClientID(db *gorm.DB, table, clientID string) (*model.ApplicationView, error) {
app := new(model.ApplicationView)
clientIDQuery := model.ApplicationSearchQuery{Key: proj_model.AppSearchKeyOIDCClientID, Value: clientID, Method: global_model.SearchMethodEquals}
query := repository.PrepareGetByQuery(table, clientIDQuery)
err := query(db, app)
if caos_errs.IsNotFound(err) {
return nil, caos_errs.ThrowNotFound(nil, "VIEW-DG1qh", "Errors.Project.AppNotFound")
}
return app, err
}
func ApplicationByProjectIDAndAppName(db *gorm.DB, table, projectID, appName string) (*model.ApplicationView, error) {
app := new(model.ApplicationView)
projectIDQuery := model.ApplicationSearchQuery{Key: proj_model.AppSearchKeyProjectID, Value: projectID, Method: global_model.SearchMethodEquals}
appNameQuery := model.ApplicationSearchQuery{Key: proj_model.AppSearchKeyName, Value: appName, Method: global_model.SearchMethodEquals}
query := repository.PrepareGetByQuery(table, projectIDQuery, appNameQuery)
err := query(db, app)
if caos_errs.IsNotFound(err) {
return nil, caos_errs.ThrowNotFound(nil, "VIEW-Jqw1z", "Errors.Project.AppNotFound")
}
return app, err
}
func SearchApplications(db *gorm.DB, table string, req *proj_model.ApplicationSearchRequest) ([]*model.ApplicationView, int, error) {
apps := make([]*model.ApplicationView, 0)
query := repository.PrepareSearchQuery(table, model.ApplicationSearchRequest{Limit: req.Limit, Offset: req.Offset, Queries: req.Queries})
count, err := query(db, &apps)
return apps, count, err
}
func PutApplication(db *gorm.DB, table string, app *model.ApplicationView) error {
save := repository.PrepareSave(table)
return save(db, app)
}
func DeleteApplication(db *gorm.DB, table, appID string) error {
delete := repository.PrepareDeleteByKey(table, model.ApplicationSearchKey(proj_model.AppSearchKeyAppID), appID)
return delete(db)
}