aboutsummaryrefslogtreecommitdiffstats
path: root/graphql/resolvers/mutation.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2020-06-21 22:12:04 +0200
committerMichael Muré <batolettre@gmail.com>2020-06-27 23:03:05 +0200
commit2ab6381a94d55fa22b80acdbb18849d6b24951f9 (patch)
tree99942b000955623ea7466b9fa4cc7dab37645df6 /graphql/resolvers/mutation.go
parent5f72b04ef8e84b1c367ca6874519706318e351f5 (diff)
downloadgit-bug-2ab6381a94d55fa22b80acdbb18849d6b24951f9.tar.gz
Reorganize the webUI and API code
Included in the changes: - create a new /api root package to hold all API code, migrate /graphql in there - git API handlers all use the cache instead of the repo directly - git API handlers are now tested - git API handlers now require a "repo" mux parameter - lots of untangling of API/handlers/middleware - less code in commands/webui.go
Diffstat (limited to 'graphql/resolvers/mutation.go')
-rw-r--r--graphql/resolvers/mutation.go208
1 files changed, 0 insertions, 208 deletions
diff --git a/graphql/resolvers/mutation.go b/graphql/resolvers/mutation.go
deleted file mode 100644
index 31505047..00000000
--- a/graphql/resolvers/mutation.go
+++ /dev/null
@@ -1,208 +0,0 @@
-package resolvers
-
-import (
- "context"
- "time"
-
- "github.com/MichaelMure/git-bug/bug"
- "github.com/MichaelMure/git-bug/cache"
- "github.com/MichaelMure/git-bug/graphql/graph"
- "github.com/MichaelMure/git-bug/graphql/graphqlidentity"
- "github.com/MichaelMure/git-bug/graphql/models"
-)
-
-var _ graph.MutationResolver = &mutationResolver{}
-
-type mutationResolver struct {
- cache *cache.MultiRepoCache
-}
-
-func (r mutationResolver) getRepo(ref *string) (*cache.RepoCache, error) {
- if ref != nil {
- return r.cache.ResolveRepo(*ref)
- }
-
- return r.cache.DefaultRepo()
-}
-
-func (r mutationResolver) getBug(repoRef *string, bugPrefix string) (*cache.RepoCache, *cache.BugCache, error) {
- repo, err := r.getRepo(repoRef)
- if err != nil {
- return nil, nil, err
- }
-
- bug, err := repo.ResolveBugPrefix(bugPrefix)
- if err != nil {
- return nil, nil, err
- }
- return repo, bug, nil
-}
-
-func (r mutationResolver) NewBug(ctx context.Context, input models.NewBugInput) (*models.NewBugPayload, error) {
- repo, err := r.getRepo(input.RepoRef)
- if err != nil {
- return nil, err
- }
-
- id, err := graphqlidentity.ForContext(ctx, repo)
- if err != nil {
- return nil, err
- }
-
- b, op, err := repo.NewBugRaw(id, time.Now().Unix(), input.Title, input.Message, input.Files, nil)
- if err != nil {
- return nil, err
- }
-
- return &models.NewBugPayload{
- ClientMutationID: input.ClientMutationID,
- Bug: models.NewLoadedBug(b.Snapshot()),
- Operation: op,
- }, nil
-}
-
-func (r mutationResolver) AddComment(ctx context.Context, input models.AddCommentInput) (*models.AddCommentPayload, error) {
- repo, b, err := r.getBug(input.RepoRef, input.Prefix)
- if err != nil {
- return nil, err
- }
-
- id, err := graphqlidentity.ForContext(ctx, repo)
- if err != nil {
- return nil, err
- }
-
- op, err := b.AddCommentRaw(id, time.Now().Unix(), input.Message, input.Files, nil)
- if err != nil {
- return nil, err
- }
-
- err = b.Commit()
- if err != nil {
- return nil, err
- }
-
- return &models.AddCommentPayload{
- ClientMutationID: input.ClientMutationID,
- Bug: models.NewLoadedBug(b.Snapshot()),
- Operation: op,
- }, nil
-}
-
-func (r mutationResolver) ChangeLabels(ctx context.Context, input *models.ChangeLabelInput) (*models.ChangeLabelPayload, error) {
- repo, b, err := r.getBug(input.RepoRef, input.Prefix)
- if err != nil {
- return nil, err
- }
-
- id, err := graphqlidentity.ForContext(ctx, repo)
- if err != nil {
- return nil, err
- }
-
- results, op, err := b.ChangeLabelsRaw(id, time.Now().Unix(), input.Added, input.Removed, nil)
- if err != nil {
- return nil, err
- }
-
- err = b.Commit()
- if err != nil {
- return nil, err
- }
-
- resultsPtr := make([]*bug.LabelChangeResult, len(results))
- for i, result := range results {
- resultsPtr[i] = &result
- }
-
- return &models.ChangeLabelPayload{
- ClientMutationID: input.ClientMutationID,
- Bug: models.NewLoadedBug(b.Snapshot()),
- Operation: op,
- Results: resultsPtr,
- }, nil
-}
-
-func (r mutationResolver) OpenBug(ctx context.Context, input models.OpenBugInput) (*models.OpenBugPayload, error) {
- repo, b, err := r.getBug(input.RepoRef, input.Prefix)
- if err != nil {
- return nil, err
- }
-
- id, err := graphqlidentity.ForContext(ctx, repo)
- if err != nil {
- return nil, err
- }
-
- op, err := b.OpenRaw(id, time.Now().Unix(), nil)
- if err != nil {
- return nil, err
- }
-
- err = b.Commit()
- if err != nil {
- return nil, err
- }
-
- return &models.OpenBugPayload{
- ClientMutationID: input.ClientMutationID,
- Bug: models.NewLoadedBug(b.Snapshot()),
- Operation: op,
- }, nil
-}
-
-func (r mutationResolver) CloseBug(ctx context.Context, input models.CloseBugInput) (*models.CloseBugPayload, error) {
- repo, b, err := r.getBug(input.RepoRef, input.Prefix)
- if err != nil {
- return nil, err
- }
-
- id, err := graphqlidentity.ForContext(ctx, repo)
- if err != nil {
- return nil, err
- }
-
- op, err := b.CloseRaw(id, time.Now().Unix(), nil)
- if err != nil {
- return nil, err
- }
-
- err = b.Commit()
- if err != nil {
- return nil, err
- }
-
- return &models.CloseBugPayload{
- ClientMutationID: input.ClientMutationID,
- Bug: models.NewLoadedBug(b.Snapshot()),
- Operation: op,
- }, nil
-}
-
-func (r mutationResolver) SetTitle(ctx context.Context, input models.SetTitleInput) (*models.SetTitlePayload, error) {
- repo, b, err := r.getBug(input.RepoRef, input.Prefix)
- if err != nil {
- return nil, err
- }
-
- id, err := graphqlidentity.ForContext(ctx, repo)
- if err != nil {
- return nil, err
- }
-
- op, err := b.SetTitleRaw(id, time.Now().Unix(), input.Title, nil)
- if err != nil {
- return nil, err
- }
-
- err = b.Commit()
- if err != nil {
- return nil, err
- }
-
- return &models.SetTitlePayload{
- ClientMutationID: input.ClientMutationID,
- Bug: models.NewLoadedBug(b.Snapshot()),
- Operation: op,
- }, nil
-}