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 UsersImpl struct { DB *sql.DB Guard access.Guard Users dbi.Factory[iam.UserStore] } func (s *UsersImpl) ServeMux(mux *http.ServeMux) { mux.HandleFunc(api.Path_Users_GetByEmail, s.GetByEmail) mux.HandleFunc(api.Path_Users_List, s.List) mux.HandleFunc(api.Path_Users_ListByTenant, s.ListByTenant) } func (s *UsersImpl) GetByEmail(w http.ResponseWriter, r *http.Request) { var req api.Users_GetByEmailReq if read(w, r, &req) { return } if checkSystem(w, r, s.Guard, iam.CapUsersGetByEmail) { return } ctx := r.Context() var res api.Users_GetByEmailRes err := dbi.ReadOnly(ctx, s.DB, func(tx dbi.DBI) error { return s.Users(tx).GetByEmail(ctx, req.Email, &res.User) }) write(ctx, w, err, res) } func (s *UsersImpl) List(w http.ResponseWriter, r *http.Request) { var req api.Users_ListReq if read(w, r, &req) { return } if checkSystem(w, r, s.Guard, iam.CapUsersList) { return } ctx := r.Context() var res api.Users_ListRes err := dbi.ReadOnly(ctx, s.DB, func(tx dbi.DBI) error { return s.Users(tx).List(ctx, req.Page, &res.Page) }) write(ctx, w, err, res) } func (s *UsersImpl) ListByTenant(w http.ResponseWriter, r *http.Request) { var req api.Users_ListByTenantReq if read(w, r, &req) { return } if check(w, r, s.Guard, iam.CapGrantsList, req.Tenant, "") { return } ctx := r.Context() var res api.Users_ListByTenantRes err := dbi.ReadOnly(ctx, s.DB, func(tx dbi.DBI) error { return s.Users(tx).ListByTenant(ctx, req.Tenant, req.Page, &res.Page) }) write(ctx, w, err, res) }