package graph import ( "context" "strconv" "forge.cadoles.com/Cadoles/daddy/internal/model" "github.com/pkg/errors" ) func handleWorkgroups(ctx context.Context, filter *model.WorkgroupsFilter) ([]*model.Workgroup, error) { db, err := getDB(ctx) if err != nil { return nil, errors.WithStack(err) } repo := model.NewWorkgroupRepository(db) criteria := make([]interface{}, 0) if filter != nil { if len(filter.Ids) > 0 { criteria = append(criteria, "id in (?)", filter.Ids) } } workgroups, err := repo.FindWorkgroups(ctx, criteria...) if err != nil { return nil, errors.WithStack(err) } return workgroups, nil } func handleJoinWorkgroup(ctx context.Context, rawWorkgroupID string) (*model.Workgroup, error) { workgroupID, err := parseWorkgroupID(rawWorkgroupID) if err != nil { return nil, errors.WithStack(err) } user, db, err := getSessionUser(ctx) if err != nil { return nil, errors.WithStack(err) } repo := model.NewWorkgroupRepository(db) workgroup, err := repo.AddUserToWorkgroup(ctx, user.ID, workgroupID) if err != nil { return nil, errors.WithStack(err) } return workgroup, nil } func handleLeaveWorkgroup(ctx context.Context, rawWorkgroupID string) (*model.Workgroup, error) { workgroupID, err := parseWorkgroupID(rawWorkgroupID) if err != nil { return nil, errors.WithStack(err) } user, db, err := getSessionUser(ctx) if err != nil { return nil, errors.WithStack(err) } repo := model.NewWorkgroupRepository(db) workgroup, err := repo.RemoveUserFromWorkgroup(ctx, user.ID, workgroupID) if err != nil { return nil, errors.WithStack(err) } return workgroup, nil } func handleCreateWorkgroup(ctx context.Context, changes model.WorkgroupChanges) (*model.Workgroup, error) { db, err := getDB(ctx) if err != nil { return nil, errors.WithStack(err) } repo := model.NewWorkgroupRepository(db) workgroup, err := repo.CreateWorkgroup(ctx, changes) if err != nil { return nil, errors.WithStack(err) } return workgroup, nil } func handleCloseWorkgroup(ctx context.Context, rawWorkgroupID string) (*model.Workgroup, error) { workgroupID, err := parseWorkgroupID(rawWorkgroupID) if err != nil { return nil, errors.WithStack(err) } db, err := getDB(ctx) if err != nil { return nil, errors.WithStack(err) } repo := model.NewWorkgroupRepository(db) workgroup, err := repo.CloseWorkgroup(ctx, workgroupID) if err != nil { return nil, errors.WithStack(err) } return workgroup, nil } func handleUpdateWorkgroup(ctx context.Context, rawWorkgroupID string, changes model.WorkgroupChanges) (*model.Workgroup, error) { workgroupID, err := parseWorkgroupID(rawWorkgroupID) if err != nil { return nil, errors.WithStack(err) } db, err := getDB(ctx) if err != nil { return nil, errors.WithStack(err) } repo := model.NewWorkgroupRepository(db) workgroup, err := repo.UpdateWorkgroup(ctx, workgroupID, changes) if err != nil { return nil, errors.WithStack(err) } return workgroup, nil } func parseWorkgroupID(workgroupID string) (uint, error) { workgroupID64, err := strconv.ParseUint(workgroupID, 10, 32) if err != nil { return 0, errors.WithStack(err) } return uint(workgroupID64), nil }