package api import ( "database/sql" "net/http" "atlas9.dev/c/core/dbi" "atlas9.dev/c/core/iam" "atlas9.dev/c/demo/lib" ) type RolesImpl struct { DB *sql.DB Guard lib.Guard Roles dbi.Factory[iam.RoleStore] } func (s *RolesImpl) Save(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req Roles_SaveReq if read(w, r, &req) { return } // Check access if check(w, r, s.Guard, iam.CapRolesSave, req.Role.Tenant, "") { return } // Write to the database ctx := r.Context() err := dbi.ReadWrite(ctx, s.DB, func(tx dbi.DBI) error { return s.Roles(tx).Save(ctx, &req.Role) }) write(ctx, w, err, nil) } func (s *RolesImpl) Get(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req Roles_GetReq if read(w, r, &req) { return } // Check access if check(w, r, s.Guard, iam.CapRolesGet, req.Tenant, "") { return } // Get role from database ctx := r.Context() var role *iam.Role err := dbi.ReadOnly(ctx, s.DB, func(tx dbi.DBI) error { return s.Roles(tx).Get(ctx, req.Tenant, req.Slug, role) }) write(ctx, w, err, role) } func (s *RolesImpl) Delete(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req Roles_DeleteReq if read(w, r, &req) { return } // Check access if check(w, r, s.Guard, iam.CapRolesDelete, req.Tenant, "") { return } // Delete the record from the database ctx := r.Context() err := dbi.ReadWrite(ctx, s.DB, func(tx dbi.DBI) error { return s.Roles(tx).Delete(ctx, req.Tenant, req.Slug) }) write(ctx, w, err, nil) } func (s *RolesImpl) List(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req Roles_ListReq if read(w, r, &req) { return } // Check access if check(w, r, s.Guard, iam.CapRolesList, req.Tenant, "") { return } // Load the data from the database ctx := r.Context() var res Roles_ListRes err := dbi.ReadOnly(ctx, s.DB, func(tx dbi.DBI) error { return s.Roles(tx).List(ctx, req.Tenant, req.Page, &res.Page) }) write(ctx, w, err, res) } func (s *RolesImpl) ListByTenant(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req Roles_ListByTenantReq if read(w, r, &req) { return } // Check access if check(w, r, s.Guard, iam.CapRolesList, req.Tenant, "") { return } // Load the data from the database ctx := r.Context() var res Roles_ListRes err := dbi.ReadOnly(ctx, s.DB, func(tx dbi.DBI) error { return s.Roles(tx).List(ctx, req.Tenant, req.Page, &res.Page) }) write(ctx, w, err, res) }