package api import ( "database/sql" "net/http" "atlas9.dev/c/core/dbi" "atlas9.dev/c/core/iam" "atlas9.dev/c/demo/lib" ) type GroupsImpl struct { DB *sql.DB Guard lib.Guard Groups dbi.Factory[iam.GroupStore] } func (s *GroupsImpl) Save(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req Groups_SaveReq if read(w, r, &req) { return } // Write to the database ctx := r.Context() err := dbi.ReadWrite(ctx, s.DB, func(tx dbi.DBI) error { return s.Groups(tx).Save(ctx, &req.Group) }) write(ctx, w, err, nil) } func (s *GroupsImpl) Get(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req Groups_GetReq if read(w, r, &req) { return } // Get group from database ctx := r.Context() var group iam.Group err := dbi.ReadOnly(ctx, s.DB, func(tx dbi.DBI) error { return s.Groups(tx).Get(ctx, req.Tenant, req.Path, &group) }) write(ctx, w, err, group) } func (s *GroupsImpl) Delete(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req Groups_DeleteReq if read(w, r, &req) { return } // Delete the record from the database ctx := r.Context() err := dbi.ReadWrite(ctx, s.DB, func(tx dbi.DBI) error { return s.Groups(tx).Delete(ctx, req.Tenant, req.Path) }) write(ctx, w, err, nil) } func (s *GroupsImpl) List(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req Groups_ListReq if read(w, r, &req) { return } // Load the data from the database ctx := r.Context() var res Groups_ListRes err := dbi.ReadOnly(ctx, s.DB, func(tx dbi.DBI) error { return s.Groups(tx).List(ctx, req.Tenant, req.Page, &res.Page) }) write(ctx, w, err, res) } func (s *GroupsImpl) ListByTenant(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req Groups_ListByTenantReq if read(w, r, &req) { return } // Load the data from the database ctx := r.Context() var res Groups_ListRes err := dbi.ReadOnly(ctx, s.DB, func(tx dbi.DBI) error { return s.Groups(tx).List(ctx, req.Tenant, req.Page, &res.Page) }) write(ctx, w, err, res) } func (s *GroupsImpl) AddMember(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req Groups_AddMemberReq if read(w, r, &req) { return } // Write to the database ctx := r.Context() err := dbi.ReadWrite(ctx, s.DB, func(tx dbi.DBI) error { return s.Groups(tx).AddMember(ctx, req.Tenant, req.Group, req.UserID) }) write(ctx, w, err, nil) } func (s *GroupsImpl) RemoveMember(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req Groups_RemoveMemberReq if read(w, r, &req) { return } // Write to the database ctx := r.Context() err := dbi.ReadWrite(ctx, s.DB, func(tx dbi.DBI) error { return s.Groups(tx).RemoveMember(ctx, req.Tenant, req.Group, req.UserID) }) write(ctx, w, err, nil) } func (s *GroupsImpl) ListMembers(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req Groups_ListMembersReq if read(w, r, &req) { return } // Load the data from the database ctx := r.Context() var res Groups_ListMembersRes err := dbi.ReadOnly(ctx, s.DB, func(tx dbi.DBI) error { return s.Groups(tx).ListMembers(ctx, req.Tenant, req.Group, req.Page, &res.Page) }) write(ctx, w, err, res) } func (s *GroupsImpl) ListMemberGroups(w http.ResponseWriter, r *http.Request) { // Read and validate the request body var req Groups_ListMemberGroupsReq if read(w, r, &req) { return } // Load the data from the database ctx := r.Context() var res Groups_ListMemberGroupsRes err := dbi.ReadOnly(ctx, s.DB, func(tx dbi.DBI) error { return s.Groups(tx).ListMemberGroups(ctx, req.UserID, req.Page, &res.Page) }) write(ctx, w, err, res) }