package api_impl import ( "database/sql" "net/http" "atlas9.dev/c/core/dbi" "atlas9.dev/c/core/iam" "atlas9.dev/c/demo/api" "atlas9.dev/c/demo/lib/access" ) type RolesImpl struct { DB *sql.DB Guard access.Guard Roles dbi.Factory[iam.RoleStore] Audit dbi.Factory[iam.AuditStore] } func (s *RolesImpl) ServeMux(mux *http.ServeMux) { mux.HandleFunc(api.Path_Roles_Save, s.Save) mux.HandleFunc(api.Path_Roles_Get, s.Get) mux.HandleFunc(api.Path_Roles_Delete, s.Delete) mux.HandleFunc(api.Path_Roles_List, s.List) mux.HandleFunc(api.Path_Roles_ListByTenant, s.ListByTenant) } func (s *RolesImpl) Save(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req api.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 { if err := s.Roles(tx).Save(ctx, &req.Role); err != nil { return err } return audit(ctx, s.Audit(tx), iam.AuditEntry{ Tenant: req.Role.Tenant, Action: "Roles_Save", Resource: req.Role.Slug, Detail: req.Role.Name, }) }) write(ctx, w, err, nil) } func (s *RolesImpl) Get(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req api.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 api.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 { if err := s.Roles(tx).Delete(ctx, req.Tenant, req.Slug); err != nil { return err } return audit(ctx, s.Audit(tx), iam.AuditEntry{ Tenant: req.Tenant, Action: "Roles_Delete", Resource: 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 api.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 api.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 api.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 api.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) }