package api import ( "database/sql" "net/http" "atlas9.dev/c/core/dbi" "atlas9.dev/c/core/iam" "atlas9.dev/c/demo/lib" ) type GrantsImpl struct { DB *sql.DB Guard lib.Guard Grants dbi.Factory[iam.GrantStore] } func (s *GrantsImpl) Add(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req Grants_AddReq if read(w, r, &req) { return } // Check access if check(w, r, s.Guard, iam.CapGrantsAdd, req.Grant.Tenant, "") { return } // Write to the database ctx := r.Context() err := dbi.ReadWrite(ctx, s.DB, func(tx dbi.DBI) error { return s.Grants(tx).Add(ctx, req.Grant) }) write(ctx, w, err, nil) } func (s *GrantsImpl) Remove(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req Grants_RemoveReq if read(w, r, &req) { return } // Check access if check(w, r, s.Guard, iam.CapGrantsRemove, req.Grant.Tenant, "") { return } // Write to the database ctx := r.Context() err := dbi.ReadWrite(ctx, s.DB, func(tx dbi.DBI) error { return s.Grants(tx).Remove(ctx, req.Grant) }) write(ctx, w, err, nil) } func (s *GrantsImpl) List(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req Grants_ListReq if read(w, r, &req) { return } // Check access if checkSystem(w, r, s.Guard, iam.CapGrantsList) { return } // Load the data from the database ctx := r.Context() var res Grants_ListRes err := dbi.ReadOnly(ctx, s.DB, func(tx dbi.DBI) error { return s.Grants(tx).ListForUser(ctx, req.UserID, req.Page, &res.Page) }) write(ctx, w, err, res) } func (s *GrantsImpl) ListByTenant(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req Grants_ListByTenantReq if read(w, r, &req) { return } // Check access if check(w, r, s.Guard, iam.CapGrantsList, req.Tenant, "") { return } // Load the data from the database ctx := r.Context() var res Grants_ListRes err := dbi.ReadOnly(ctx, s.DB, func(tx dbi.DBI) error { return s.Grants(tx).List(ctx, req.Tenant, req.Page, &res.Page) }) write(ctx, w, err, res) }