135 lines
2.9 KiB
Go
135 lines
2.9 KiB
Go
|
package graph
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"strconv"
|
||
|
|
||
|
"forge.cadoles.com/Cadoles/daddy/internal/model"
|
||
|
"github.com/pkg/errors"
|
||
|
)
|
||
|
|
||
|
func handleWorkgroups(ctx context.Context) ([]*model.Workgroup, error) {
|
||
|
db, err := getDB(ctx)
|
||
|
if err != nil {
|
||
|
return nil, errors.WithStack(err)
|
||
|
}
|
||
|
|
||
|
repo := model.NewWorkgroupRepository(db)
|
||
|
|
||
|
workgroups, err := repo.FindWorkgroups(ctx)
|
||
|
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
|
||
|
}
|