Drop narration, history and restated-code comments across the repo, keep short ones for security boundaries, upstream quirks and ordering constraints, and record the convention in CLAUDE.md. Co-Authored-By: Claude Opus 5.5 <[email protected]>
105 lines
2.8 KiB
Go
105 lines
2.8 KiB
Go
package graph
|
|
|
|
// This file will be automatically regenerated based on the schema, any resolver
|
|
// implementations
|
|
// will be copied through when generating and any unknown code will be moved to the end.
|
|
// Code generated by github.com/99designs/gqlgen version v0.17.98
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"adam-french.co.uk/backend/graph/model"
|
|
"adam-french.co.uk/backend/models"
|
|
)
|
|
|
|
// CreatePost is the resolver for the createPost field.
|
|
func (r *mutationResolver) CreatePost(ctx context.Context, input model.CreatePostInput) (*models.Post, error) {
|
|
userID, ok := UserIDFromCtx(ctx)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unauthorized")
|
|
}
|
|
|
|
// Stops a stray click publishing a blank post.
|
|
if strings.TrimSpace(input.Title) == "" {
|
|
return nil, fmt.Errorf("a post needs a title")
|
|
}
|
|
|
|
post := models.Post{Title: input.Title, Content: input.Content, AuthorID: userID}
|
|
if err := r.Store.DB.Create(&post).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &post, nil
|
|
}
|
|
|
|
// UpdatePost is the resolver for the updatePost field.
|
|
func (r *mutationResolver) UpdatePost(ctx context.Context, id int, input model.UpdatePostInput) (*models.Post, error) {
|
|
userID, ok := UserIDFromCtx(ctx)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unauthorized")
|
|
}
|
|
|
|
var post models.Post
|
|
if err := r.Store.DB.First(&post, id).Error; err != nil {
|
|
return nil, fmt.Errorf("post not found")
|
|
}
|
|
|
|
// Only the author may edit, even among admins.
|
|
if post.AuthorID != userID {
|
|
return nil, fmt.Errorf("you can only update your own posts")
|
|
}
|
|
|
|
// Full replace: an omitted field is blanked.
|
|
post.Title = input.Title
|
|
post.Content = input.Content
|
|
if err := r.Store.DB.Save(&post).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &post, nil
|
|
}
|
|
|
|
// DeletePost is the resolver for the deletePost field.
|
|
func (r *mutationResolver) DeletePost(ctx context.Context, id int) (*models.Post, error) {
|
|
userID, ok := UserIDFromCtx(ctx)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unauthorized")
|
|
}
|
|
|
|
var post models.Post
|
|
if err := r.Store.DB.First(&post, id).Error; err != nil {
|
|
return nil, fmt.Errorf("post not found")
|
|
}
|
|
|
|
if post.AuthorID != userID {
|
|
return nil, fmt.Errorf("you can only delete your own posts")
|
|
}
|
|
|
|
if err := r.Store.DB.Delete(&post).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &post, nil
|
|
}
|
|
|
|
// Posts is the resolver for the posts field.
|
|
func (r *queryResolver) Posts(ctx context.Context) ([]*models.Post, error) {
|
|
// Without Preload, author resolves to null silently.
|
|
var posts []models.Post
|
|
if err := r.Store.DB.Preload("Author").Order("created_at DESC").Find(&posts).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return ptrs(posts), nil
|
|
}
|
|
|
|
// Post is the resolver for the post field.
|
|
func (r *queryResolver) Post(ctx context.Context, id int) (*models.Post, error) {
|
|
var post models.Post
|
|
if err := r.Store.DB.Preload("Author").First(&post, id).Error; err != nil {
|
|
return nil, fmt.Errorf("post not found")
|
|
}
|
|
return &post, nil
|
|
}
|