mirror of
https://github.com/hanzoai/ca.git
synced 2026-11-03 18:08:27 +00:00
152 lines
4.5 KiB
Go
152 lines
4.5 KiB
Go
package query
|
|
|
|
import (
|
|
"github.com/effective-security/trustyca/api/pb"
|
|
"github.com/effective-security/trustyca/internal/db/schema"
|
|
"github.com/effective-security/xdb"
|
|
"github.com/effective-security/xdb/xsql"
|
|
)
|
|
|
|
// RegisterProject returns SQL query to create a project
|
|
func RegisterProject(args ...any) (string, string) {
|
|
const key = "RegisterProject"
|
|
return xsql.Postgres.GetOrCreateQuery(key, func(name string) xsql.Builder {
|
|
q := schema.ProjectTableInfo.
|
|
InsertInto().
|
|
Returning(schema.ProjectTableInfo.AllColumns())
|
|
|
|
q.NewRow().
|
|
Set(schema.Project.ID.Name, nil).
|
|
Set(schema.Project.OrgID.Name, nil).
|
|
Set(schema.Project.Alias.Name, nil).
|
|
Set(schema.Project.Name.Name, nil).
|
|
Set(schema.Project.Description.Name, nil).
|
|
SetExpr(schema.Project.Status.Name, "2").
|
|
SetExpr(schema.Project.CreatedAt.Name, exprNow).
|
|
SetExpr(schema.Project.UpdatedAt.Name, exprNow)
|
|
return q
|
|
})
|
|
}
|
|
|
|
// UpdateProjectRequest defines request for updating a project
|
|
type UpdateProjectRequest struct {
|
|
ID uint64
|
|
Name string
|
|
Description string
|
|
Status pb.ItemStatus_Enum
|
|
}
|
|
|
|
// QueryParams returns the query params builder
|
|
func (r *UpdateProjectRequest) QueryParams() xdb.QueryParams {
|
|
b := xdb.NewQueryParams("UpdateProject")
|
|
|
|
if r.Name != "" {
|
|
b.Set(schema.Project.Name.Position, r.Name)
|
|
}
|
|
if r.Description != "" {
|
|
b.Set(schema.Project.Description.Position, r.Description)
|
|
}
|
|
if r.Status != pb.ItemStatus_Unknown {
|
|
b.Set(schema.Project.Status.Position, r.Status)
|
|
}
|
|
b.AddArgs(r.ID)
|
|
return b
|
|
}
|
|
|
|
// UpdateProject returns SQL query to update a project
|
|
func UpdateProject(args ...any) (string, string) {
|
|
p := xdb.GetQueryParams(args...)
|
|
key := p.Name()
|
|
|
|
return xsql.Postgres.GetOrCreateQuery(key, func(name string) xsql.Builder {
|
|
q := schema.ProjectTableInfo.
|
|
Update().
|
|
Where(schema.Project.ID.Name+" = ?", nil).
|
|
Returning(schema.ProjectTableInfo.AllColumns())
|
|
|
|
if p.IsSet(schema.Project.Name.Position) {
|
|
q.Set(schema.Project.Name.Name, nil)
|
|
}
|
|
if p.IsSet(schema.Project.Description.Position) {
|
|
q.Set(schema.Project.Description.Name, nil)
|
|
}
|
|
if p.IsSet(schema.Project.Status.Position) {
|
|
q.Set(schema.Project.Status.Name, nil)
|
|
}
|
|
q.SetExpr(schema.Project.UpdatedAt.Name, exprNow)
|
|
return q
|
|
})
|
|
}
|
|
|
|
// ListProjectsRequest defines request for list operation on projects
|
|
type ListProjectsRequest struct {
|
|
// OrgID specifies the org
|
|
OrgID uint64
|
|
// Status specifies the status of the projects to filter by
|
|
Status pb.ItemStatus_Enum
|
|
// IDs limits the result to the projects with the IDs;
|
|
// used to return only the projects the caller has a grant for
|
|
IDs []uint64
|
|
// Limit specifies maximum number of records to return
|
|
Limit uint32
|
|
// Offset specifies the offset for offset pagination
|
|
Offset uint32
|
|
// Cursor specifies the position for cursor pagination, decoded by
|
|
// ParseNameCursor
|
|
Cursor Cursor
|
|
}
|
|
|
|
// Validate fails a request that carries the cursor of another list
|
|
func (r *ListProjectsRequest) Validate() error {
|
|
return r.Cursor.checkKey(keyName)
|
|
}
|
|
|
|
// QueryParams returns the query params builder
|
|
func (r *ListProjectsRequest) QueryParams() xdb.QueryParams {
|
|
b := xdb.NewQueryParams("ListProjects")
|
|
if r.OrgID != 0 {
|
|
b.Set(schema.Project.OrgID.Position, r.OrgID)
|
|
}
|
|
if r.Status != pb.ItemStatus_Unknown {
|
|
b.Set(schema.Project.Status.Position, r.Status)
|
|
}
|
|
if r.IDs != nil {
|
|
b.Set(schema.Project.ID.Position, idArray(r.IDs))
|
|
}
|
|
setPage(b, r.Limit, r.Offset, r.Cursor)
|
|
return b
|
|
}
|
|
|
|
// ListProjects returns SQL query: the projects by name, then by id, paged
|
|
// by offset or by a cursor on that key (ParseNameCursor, EncodeNameCursor)
|
|
func ListProjects(args ...any) (string, string) {
|
|
p := xdb.GetQueryParams(args...)
|
|
key := p.Name()
|
|
|
|
return xsql.Postgres.GetOrCreateQuery(key, func(name string) xsql.Builder {
|
|
q := schema.ProjectTableInfo.Select()
|
|
if p.IsSet(schema.Project.OrgID.Position) {
|
|
q.Where(schema.Project.OrgID.Name+" = ?", nil)
|
|
}
|
|
if p.IsSet(schema.Project.Status.Position) {
|
|
q.Where(schema.Project.Status.Name+" = ?", nil)
|
|
}
|
|
if p.IsSet(schema.Project.ID.Position) {
|
|
q.Where(schema.Project.ID.Name+" = ANY(?)", nil)
|
|
}
|
|
pageBy(q, p, OrderDirectionAsc, schema.Project.Name.Name, schema.Project.ID.Name)
|
|
return q
|
|
})
|
|
}
|
|
|
|
// GetProjectByAlias returns SQL query to find a project by org and alias
|
|
func GetProjectByAlias(args ...any) (string, string) {
|
|
const key = "GetProjectByAlias"
|
|
return xsql.Postgres.GetOrCreateQuery(key, func(name string) xsql.Builder {
|
|
return schema.ProjectTableInfo.
|
|
Select().
|
|
Where(schema.Project.OrgID.Name+" = ?", nil).
|
|
Where(schema.Project.Alias.Name+" = ?", nil)
|
|
})
|
|
}
|