package main import ( "context" "database/sql" "atlas9.dev/c/core" "atlas9.dev/c/core/dbi" "atlas9.dev/c/core/iam" "atlas9.dev/c/core/routes" ) type GroupApi struct { DB *sql.DB Groups dbi.Factory[iam.GroupStore] } type GetGroupReq struct { ID core.ID } type GetGroupByPathReq struct { Path core.Path } type DeleteGroupReq struct { ID core.ID } type DeleteGroupRes struct{} func (s *GroupApi) Save(ctx context.Context, req iam.Group) (*iam.Group, error) { _, err := dbi.ReadWrite(ctx, s.DB, func(tx dbi.DBI) (bool, error) { return s.Groups(tx).Save(ctx, &req) }) if err != nil { return nil, err } return &req, nil } func (s *GroupApi) Get(ctx context.Context, req GetGroupReq) (*iam.Group, error) { return dbi.ReadOnly(ctx, s.DB, func(tx dbi.DBI) (*iam.Group, error) { return s.Groups(tx).Get(ctx, req.ID) }) } func (s *GroupApi) GetByPath(ctx context.Context, req GetGroupByPathReq) (*iam.Group, error) { return dbi.ReadOnly(ctx, s.DB, func(tx dbi.DBI) (*iam.Group, error) { return s.Groups(tx).GetByPath(ctx, req.Path) }) } func (s *GroupApi) Delete(ctx context.Context, req DeleteGroupReq) (*DeleteGroupRes, error) { return dbi.ReadWrite(ctx, s.DB, func(tx dbi.DBI) (*DeleteGroupRes, error) { return &DeleteGroupRes{}, s.Groups(tx).Delete(ctx, req.ID) }) } type ListGroupsReq struct { Page core.PageReq } type ListGroupsRes core.Page[iam.Group] func (s *GroupApi) List(ctx context.Context, req ListGroupsReq) (*ListGroupsRes, error) { page, err := dbi.ReadOnly(ctx, s.DB, func(tx dbi.DBI) (core.Page[iam.Group], error) { return s.Groups(tx).List(ctx, req.Page) }) if err != nil { return nil, err } res := ListGroupsRes(page) return &res, nil } type ListGroupsByNamespaceReq struct { Namespace string Page core.PageReq } func (s *GroupApi) ListByNamespace(ctx context.Context, req ListGroupsByNamespaceReq) (*ListGroupsRes, error) { page, err := dbi.ReadOnly(ctx, s.DB, func(tx dbi.DBI) (core.Page[iam.Group], error) { return s.Groups(tx).ListByNamespace(ctx, req.Namespace, req.Page) }) if err != nil { return nil, err } res := ListGroupsRes(page) return &res, nil } func GroupRoutes(svc *GroupApi) []routes.Route { return []routes.Route{ routes.RPC("/groups/save", svc.Save), routes.RPC("/groups/get", svc.Get), routes.RPC("/groups/get-by-path", svc.GetByPath), routes.RPC("/groups/delete", svc.Delete), routes.RPC("/groups/list", svc.List), routes.RPC("/groups/list-by-namespace", svc.ListByNamespace), } }