tailly_back_v2/internal/http/graph/post_resolvers.go
madipo2611 f9f1e16b1b
All checks were successful
continuous-integration/drone/push Build is passing
v0.0.13 Скорректирован метод Comments в postResolver
2025-08-06 11:28:23 +03:00

162 lines
4.7 KiB
Go

package graph
import (
"context"
"fmt"
"tailly_back_v2/internal/domain"
"time"
)
type postResolver struct{ *Resolver }
// Post is the resolver for the post field.
func (r *queryResolver) Post(ctx context.Context, id int) (*domain.Post, error) {
post, err := r.Services.Post.GetByID(ctx, id)
if err != nil {
return nil, fmt.Errorf("failed to get post: %w", err)
}
return post, nil
}
// Posts is the resolver for the posts field.
func (r *queryResolver) Posts(ctx context.Context) ([]*domain.Post, error) {
posts, err := r.Services.Post.GetAll(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get posts: %w", err)
}
return posts, nil
}
// Post returns PostResolver implementation.
func (r *Resolver) Post() PostResolver { return &postResolver{r} }
// Author is the resolver for the author field.
func (r *postResolver) Author(ctx context.Context, obj *domain.Post) (*domain.User, error) {
if obj.AuthorID == 0 {
return nil, fmt.Errorf("post has no author ID")
}
author, err := r.Services.User.GetByID(ctx, obj.AuthorID)
if err != nil {
return nil, fmt.Errorf("failed to get author: %w", err)
}
return author, nil
}
// Comments is the resolver for the comments field.
func (r *postResolver) Comments(ctx context.Context, obj *domain.Post) ([]*domain.Comment, error) {
comments, err := r.Services.Comment.GetByPostID(ctx, obj.ID)
if err != nil {
return nil, fmt.Errorf("failed to get comments: %w", err)
}
return comments, nil
}
// Likes is the resolver for the likes field.
func (r *postResolver) Likes(ctx context.Context, obj *domain.Post) ([]*domain.Like, error) {
likes, err := r.Services.Like.GetByPostID(ctx, obj.ID)
if err != nil {
return nil, fmt.Errorf("failed to get likes: %w", err)
}
return likes, nil
}
// LikesCount is the resolver for the likesCount field.
func (r *postResolver) LikesCount(ctx context.Context, obj *domain.Post) (int, error) {
count, err := r.Services.Like.GetCountForPost(ctx, obj.ID)
if err != nil {
return 0, fmt.Errorf("failed to get likes count: %w", err)
}
return count, nil
}
// IsLiked is the resolver for the isLiked field.
func (r *postResolver) IsLiked(ctx context.Context, obj *domain.Post) (bool, error) {
userID, err := getUserIDFromContext(ctx)
if err != nil {
return false, nil // Return false if user is not authenticated
}
liked, err := r.Services.Like.CheckIfLiked(ctx, userID, obj.ID)
if err != nil {
return false, fmt.Errorf("failed to check like status: %w", err)
}
return liked, nil
}
// CreatedAt is the resolver for the createdAt field.
func (r *postResolver) CreatedAt(ctx context.Context, obj *domain.Post) (string, error) {
return obj.CreatedAt.Format(time.RFC3339), nil
}
// UpdatedAt is the resolver for the updatedAt field.
func (r *postResolver) UpdatedAt(ctx context.Context, obj *domain.Post) (string, error) {
return obj.UpdatedAt.Format(time.RFC3339), nil
}
// LikePost is the resolver for the likePost field.
func (r *mutationResolver) LikePost(ctx context.Context, postID int) (*domain.Like, error) {
userID, err := getUserIDFromContext(ctx)
if err != nil {
return nil, fmt.Errorf("unauthorized: %w", err)
}
like, err := r.Services.Like.LikePost(ctx, userID, postID)
if err != nil {
return nil, fmt.Errorf("failed to like post: %w", err)
}
return like, nil
}
// UnlikePost is the resolver for the unlikePost field.
func (r *mutationResolver) UnlikePost(ctx context.Context, postID int) (bool, error) {
userID, err := getUserIDFromContext(ctx)
if err != nil {
return false, fmt.Errorf("unauthorized: %w", err)
}
err = r.Services.Like.UnlikePost(ctx, userID, postID)
if err != nil {
return false, fmt.Errorf("failed to unlike post: %w", err)
}
return true, nil
}
// CreatePost is the resolver for the createPost field.
func (r *mutationResolver) CreatePost(ctx context.Context, title string, content string) (*domain.Post, error) {
userID, err := getUserIDFromContext(ctx)
if err != nil {
return nil, fmt.Errorf("unauthorized: %w", err)
}
post, err := r.Services.Post.Create(ctx, userID, title, content)
if err != nil {
return nil, fmt.Errorf("failed to create post: %w", err)
}
return post, nil
}
// DeletePost is the resolver for the deletePost field.
func (r *mutationResolver) DeletePost(ctx context.Context, id int) (bool, error) {
userID, err := getUserIDFromContext(ctx)
if err != nil {
return false, fmt.Errorf("unauthorized: %w", err)
}
post, err := r.Services.Post.GetByID(ctx, id)
if err != nil {
return false, fmt.Errorf("failed to get post: %w", err)
}
if post.AuthorID != userID {
return false, fmt.Errorf("you can only delete your own posts")
}
err = r.Services.Post.Delete(ctx, id)
if err != nil {
return false, fmt.Errorf("failed to delete post: %w", err)
}
return true, nil
}