You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							104 lines
						
					
					
						
							3.0 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							104 lines
						
					
					
						
							3.0 KiB
						
					
					
				
								package queries
							 | 
						|
								
							 | 
						|
								import (
							 | 
						|
									"context"
							 | 
						|
									"errors"
							 | 
						|
									"time"
							 | 
						|
								
							 | 
						|
									"git.aiterp.net/rpdata/api/internal/auth"
							 | 
						|
									"git.aiterp.net/rpdata/api/models/stories"
							 | 
						|
								
							 | 
						|
									"git.aiterp.net/rpdata/api/graph2/input"
							 | 
						|
									"git.aiterp.net/rpdata/api/models"
							 | 
						|
									"git.aiterp.net/rpdata/api/models/chapters"
							 | 
						|
								)
							 | 
						|
								
							 | 
						|
								// Queries
							 | 
						|
								
							 | 
						|
								func (r *resolver) Chapter(ctx context.Context, id string) (models.Chapter, error) {
							 | 
						|
									return chapters.FindID(id)
							 | 
						|
								}
							 | 
						|
								
							 | 
						|
								// Mutations
							 | 
						|
								
							 | 
						|
								func (r *mutationResolver) AddChapter(ctx context.Context, input input.ChapterAddInput) (models.Chapter, error) {
							 | 
						|
									story, err := stories.FindID(input.StoryID)
							 | 
						|
									if err != nil {
							 | 
						|
										return models.Chapter{}, errors.New("Story not found")
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									token := auth.TokenFromContext(ctx)
							 | 
						|
									if !token.Permitted("member", "story.add") {
							 | 
						|
										return models.Chapter{}, errors.New("Unauthorized")
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									author := token.UserID
							 | 
						|
									if input.Author != nil && *input.Author != author {
							 | 
						|
										if !token.Permitted("story.add") {
							 | 
						|
											return models.Chapter{}, errors.New("False pretender")
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										author = *input.Author
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									if !story.Open && story.Author != author {
							 | 
						|
										return models.Chapter{}, errors.New("Story is not open")
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									return chapters.Add(story, input.Title, author, input.Source, time.Now(), input.FictionalDate)
							 | 
						|
								}
							 | 
						|
								
							 | 
						|
								func (r *mutationResolver) MoveChapter(ctx context.Context, input input.ChapterMoveInput) (models.Chapter, error) {
							 | 
						|
									chapter, err := chapters.FindID(input.ID)
							 | 
						|
									if err != nil {
							 | 
						|
										return models.Chapter{}, errors.New("Chapter not found")
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									token := auth.TokenFromContext(ctx)
							 | 
						|
									if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.move") {
							 | 
						|
										return models.Chapter{}, errors.New("You are not allowed to move this chapter")
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									target, err := stories.FindID(input.StoryID)
							 | 
						|
									if err != nil {
							 | 
						|
										return models.Chapter{}, errors.New("Target story not found")
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									if !target.Open && !token.PermittedUser(target.Author, "member", "chapter.move") {
							 | 
						|
										return models.Chapter{}, errors.New("You are not permitted to move chapters to this story")
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									return chapters.Move(chapter, target)
							 | 
						|
								}
							 | 
						|
								
							 | 
						|
								func (r *mutationResolver) EditChapter(ctx context.Context, input input.ChapterEditInput) (models.Chapter, error) {
							 | 
						|
									chapter, err := chapters.FindID(input.ID)
							 | 
						|
									if err != nil {
							 | 
						|
										return models.Chapter{}, errors.New("Chapter not found")
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									token := auth.TokenFromContext(ctx)
							 | 
						|
									if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.edit") {
							 | 
						|
										return models.Chapter{}, errors.New("Unauthorized")
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									if input.ClearFictionalDate != nil && *input.ClearFictionalDate == true {
							 | 
						|
										input.FictionalDate = &time.Time{}
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									return chapters.Edit(chapter, input.Title, input.Source, input.FictionalDate)
							 | 
						|
								}
							 | 
						|
								
							 | 
						|
								func (r *mutationResolver) RemoveChapter(ctx context.Context, input input.ChapterRemoveInput) (models.Chapter, error) {
							 | 
						|
									chapter, err := chapters.FindID(input.ID)
							 | 
						|
									if err != nil {
							 | 
						|
										return models.Chapter{}, errors.New("Chapter not found")
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									token := auth.TokenFromContext(ctx)
							 | 
						|
									if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.remove") {
							 | 
						|
										return models.Chapter{}, errors.New("Unauthorized")
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									return chapters.Remove(chapter)
							 | 
						|
								}
							 |