106 lines
3.1 KiB
Go
106 lines
3.1 KiB
Go
// comment_resolvers.go
|
|
package graph
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"tailly_back_v2/internal/domain"
|
|
"time"
|
|
)
|
|
|
|
type commentResolver struct{ *Resolver }
|
|
|
|
// Comment returns CommentResolver implementation.
|
|
func (r *Resolver) Comment() CommentResolver { return &commentResolver{r} }
|
|
|
|
// Post is the resolver for the post field.
|
|
func (r *commentResolver) Post(ctx context.Context, obj *domain.Comment) (*domain.Post, error) {
|
|
if obj == nil {
|
|
return nil, fmt.Errorf("comment is nil")
|
|
}
|
|
return r.Services.Post.GetByID(ctx, obj.PostID)
|
|
}
|
|
|
|
// Author is the resolver for the author field.
|
|
func (r *commentResolver) Author(ctx context.Context, obj *domain.Comment) (*domain.User, error) {
|
|
if obj == nil {
|
|
return nil, fmt.Errorf("comment is nil")
|
|
}
|
|
return r.Services.User.GetByID(ctx, obj.AuthorID)
|
|
}
|
|
|
|
// CreatedAt is the resolver for the createdAt field.
|
|
func (r *commentResolver) CreatedAt(ctx context.Context, obj *domain.Comment) (string, error) {
|
|
if obj == nil {
|
|
return "", fmt.Errorf("comment is nil")
|
|
}
|
|
return obj.CreatedAt.Format(time.RFC3339), nil
|
|
}
|
|
|
|
// UpdatedAt is the resolver for the updatedAt field.
|
|
func (r *commentResolver) UpdatedAt(ctx context.Context, obj *domain.Comment) (string, error) {
|
|
if obj == nil {
|
|
return "", fmt.Errorf("comment is nil")
|
|
}
|
|
return obj.UpdatedAt.Format(time.RFC3339), nil
|
|
}
|
|
|
|
// CreateComment is the resolver for the createComment field.
|
|
func (r *mutationResolver) CreateComment(ctx context.Context, postID int, content string) (*domain.Comment, error) {
|
|
userID, err := getUserIDFromContext(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return r.Services.Comment.Create(ctx, postID, userID, content)
|
|
}
|
|
|
|
// Comments is the resolver for the comments field.
|
|
func (r *queryResolver) Comments(ctx context.Context, postID int) ([]*domain.Comment, error) {
|
|
return r.Services.Comment.GetByPostID(ctx, postID)
|
|
}
|
|
|
|
// UpdateComment is the resolver for the updateComment field.
|
|
func (r *mutationResolver) UpdateComment(ctx context.Context, id int, content string) (*domain.Comment, error) {
|
|
userID, err := getUserIDFromContext(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Проверяем, что комментарий принадлежит пользователю
|
|
comment, err := r.Services.Comment.GetByID(ctx, id)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("comment not found")
|
|
}
|
|
|
|
if comment.AuthorID != userID {
|
|
return nil, fmt.Errorf("unauthorized to update this comment")
|
|
}
|
|
|
|
return r.Services.Comment.Update(ctx, id, content)
|
|
}
|
|
|
|
// DeleteComment is the resolver for the deleteComment field.
|
|
func (r *mutationResolver) DeleteComment(ctx context.Context, id int) (bool, error) {
|
|
userID, err := getUserIDFromContext(ctx)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
// Проверяем, что комментарий принадлежит пользователю
|
|
comment, err := r.Services.Comment.GetByID(ctx, id)
|
|
if err != nil {
|
|
return false, fmt.Errorf("comment not found")
|
|
}
|
|
|
|
if comment.AuthorID != userID {
|
|
return false, fmt.Errorf("unauthorized to delete this comment")
|
|
}
|
|
|
|
if err := r.Services.Comment.Delete(ctx, id); err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return true, nil
|
|
}
|