171 lines
5.0 KiB
Go
171 lines
5.0 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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// CommentsCount is the resolver for the commentsCount field.
|
|
func (r *postResolver) CommentsCount(ctx context.Context, obj *domain.Post) (int, error) {
|
|
count, err := r.Services.Comment.GetCountComments(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
|
|
}
|
|
|
|
// GetUserPosts is the resolver for the getUserPosts field.
|
|
func (r *queryResolver) GetUserPosts(ctx context.Context, userID int) ([]*domain.Post, error) {
|
|
posts, err := r.Services.Post.GetByAuthorID(ctx, userID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get posts: %w", err)
|
|
}
|
|
return posts, nil
|
|
}
|