From 3e717f8ecb854453a2a2739b06d517b4b431d678 Mon Sep 17 00:00:00 2001 From: admin Date: Thu, 18 Sep 2025 00:33:21 +0300 Subject: [PATCH] =?UTF-8?q?v0.0.33=20=D0=A3=D0=B2=D0=B5=D0=B4=D0=BE=D0=BC?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BE=20=D0=BB=D0=B0=D0=B9?= =?UTF-8?q?=D0=BA=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../http/graph/like_notification_resolvers.go | 114 ------------------ internal/http/graph/like_resolvers.go | 106 ++++++++++++++++ 2 files changed, 106 insertions(+), 114 deletions(-) delete mode 100644 internal/http/graph/like_notification_resolvers.go diff --git a/internal/http/graph/like_notification_resolvers.go b/internal/http/graph/like_notification_resolvers.go deleted file mode 100644 index b473e1c..0000000 --- a/internal/http/graph/like_notification_resolvers.go +++ /dev/null @@ -1,114 +0,0 @@ -package graph - -import ( - "context" - "fmt" - "tailly_back_v2/internal/domain" - "time" -) - -// MarkLikeNotificationAsRead is the resolver for the markLikeNotificationAsRead field. -func (r *mutationResolver) MarkLikeNotificationAsRead(ctx context.Context, notificationID int) (*MarkLikeNotificationReadResult, error) { - userID, err := getUserIDFromContext(ctx) - if err != nil { - return nil, fmt.Errorf("authentication required: %w", err) - } - - err = r.Services.Like.MarkLikeNotificationAsRead(ctx, notificationID, userID) - if err != nil { - return nil, fmt.Errorf("failed to mark notification as read: %w", err) - } - - return &MarkLikeNotificationReadResult{ - Success: true, - Message: "Notification marked as read successfully", - }, nil -} - -// MarkAllLikeNotificationsAsRead is the resolver for the markAllLikeNotificationsAsRead field. -func (r *mutationResolver) MarkAllLikeNotificationsAsRead(ctx context.Context) (*MarkLikeNotificationReadResult, error) { - userID, err := getUserIDFromContext(ctx) - if err != nil { - return nil, fmt.Errorf("authentication required: %w", err) - } - - err = r.Services.Like.MarkAllLikeNotificationsAsRead(ctx, userID) - if err != nil { - return nil, fmt.Errorf("failed to mark all notifications as read: %w", err) - } - - return &MarkLikeNotificationReadResult{ - Success: true, - Message: "All notifications marked as read successfully", - }, nil -} -func (r *queryResolver) GetLikeNotifications(ctx context.Context, unreadOnly *bool, limit *int, offset *int) (*LikeNotificationsResponse, error) { - userID, err := getUserIDFromContext(ctx) - if err != nil { - return nil, fmt.Errorf("authentication required: %w", err) - } - - unreadOnlyVal := false - if unreadOnly != nil { - unreadOnlyVal = *unreadOnly - } - - limitVal := 20 - if limit != nil { - limitVal = *limit - } - - offsetVal := 0 - if offset != nil { - offsetVal = *offset - } - - notifications, totalCount, unreadCount, err := r.Services.Like.GetLikeNotifications(ctx, userID, unreadOnlyVal, limitVal, offsetVal) - if err != nil { - return nil, fmt.Errorf("failed to get like notifications: %w", err) - } - - // Заполняем дополнительные поля для GraphQL - for _, notification := range notifications { - // Получаем информацию о пользователе, который поставил лайк - likerUser, err := r.Services.User.GetByID(ctx, notification.LikerID) - if err != nil { - return nil, fmt.Errorf("failed to get liker user: %w", err) - } - notification.SetLiker(likerUser) - - // Получаем информацию о посте - post, err := r.Services.Post.GetByID(ctx, notification.PostID) - if err != nil { - return nil, fmt.Errorf("failed to get post: %w", err) - } - - // Получаем автора поста - postAuthor, err := r.Services.User.GetByID(ctx, post.AuthorID) - if err != nil { - return nil, fmt.Errorf("failed to get post author: %w", err) - } - - // Устанавливаем автора поста - post.AuthorID = postAuthor.ID - notification.SetPost(post) - - // Форматируем дату - notification.SetCreatedAtStr() - } - - return &LikeNotificationsResponse{ - Notifications: notifications, - TotalCount: totalCount, - UnreadCount: unreadCount, - }, nil -} - -// LikeNotification returns LikeNotificationResolver implementation. -func (r *Resolver) LikeNotification() LikeNotificationResolver { return &likeNotificationResolver{r} } - -type likeNotificationResolver struct{ *Resolver } - -func (r *likeNotificationResolver) CreatedAt(ctx context.Context, obj *domain.LikeNotification) (string, error) { - return obj.CreatedAt.Format(time.RFC3339), nil -} diff --git a/internal/http/graph/like_resolvers.go b/internal/http/graph/like_resolvers.go index 58c52da..5fd2205 100644 --- a/internal/http/graph/like_resolvers.go +++ b/internal/http/graph/like_resolvers.go @@ -48,3 +48,109 @@ func (r *likeResolver) NotifiedAt(ctx context.Context, obj *domain.Like) (*strin formatted := obj.NotifiedAt.Format(time.RFC3339) return &formatted, nil } + +// MarkLikeNotificationAsRead is the resolver for the markLikeNotificationAsRead field. +func (r *mutationResolver) MarkLikeNotificationAsRead(ctx context.Context, notificationID int) (*MarkLikeNotificationReadResult, error) { + userID, err := getUserIDFromContext(ctx) + if err != nil { + return nil, fmt.Errorf("authentication required: %w", err) + } + + err = r.Services.Like.MarkLikeNotificationAsRead(ctx, notificationID, userID) + if err != nil { + return nil, fmt.Errorf("failed to mark notification as read: %w", err) + } + + return &MarkLikeNotificationReadResult{ + Success: true, + Message: "Notification marked as read successfully", + }, nil +} + +// MarkAllLikeNotificationsAsRead is the resolver for the markAllLikeNotificationsAsRead field. +func (r *mutationResolver) MarkAllLikeNotificationsAsRead(ctx context.Context) (*MarkLikeNotificationReadResult, error) { + userID, err := getUserIDFromContext(ctx) + if err != nil { + return nil, fmt.Errorf("authentication required: %w", err) + } + + err = r.Services.Like.MarkAllLikeNotificationsAsRead(ctx, userID) + if err != nil { + return nil, fmt.Errorf("failed to mark all notifications as read: %w", err) + } + + return &MarkLikeNotificationReadResult{ + Success: true, + Message: "All notifications marked as read successfully", + }, nil +} +func (r *queryResolver) GetLikeNotifications(ctx context.Context, unreadOnly *bool, limit *int, offset *int) (*LikeNotificationsResponse, error) { + userID, err := getUserIDFromContext(ctx) + if err != nil { + return nil, fmt.Errorf("authentication required: %w", err) + } + + unreadOnlyVal := false + if unreadOnly != nil { + unreadOnlyVal = *unreadOnly + } + + limitVal := 20 + if limit != nil { + limitVal = *limit + } + + offsetVal := 0 + if offset != nil { + offsetVal = *offset + } + + notifications, totalCount, unreadCount, err := r.Services.Like.GetLikeNotifications(ctx, userID, unreadOnlyVal, limitVal, offsetVal) + if err != nil { + return nil, fmt.Errorf("failed to get like notifications: %w", err) + } + + // Заполняем дополнительные поля для GraphQL + for _, notification := range notifications { + // Получаем информацию о пользователе, который поставил лайк + likerUser, err := r.Services.User.GetByID(ctx, notification.LikerID) + if err != nil { + return nil, fmt.Errorf("failed to get liker user: %w", err) + } + notification.SetLiker(likerUser) + + // Получаем информацию о посте + post, err := r.Services.Post.GetByID(ctx, notification.PostID) + if err != nil { + return nil, fmt.Errorf("failed to get post: %w", err) + } + + // Получаем автора поста + postAuthor, err := r.Services.User.GetByID(ctx, post.AuthorID) + if err != nil { + return nil, fmt.Errorf("failed to get post author: %w", err) + } + + // Устанавливаем автора поста + post.AuthorID = postAuthor.ID + notification.SetPost(post) + + // Форматируем дату + notification.SetCreatedAtStr() + } + + return &LikeNotificationsResponse{ + Notifications: notifications, + TotalCount: totalCount, + UnreadCount: unreadCount, + }, nil +} + +// LikeNotification returns LikeNotificationResolver implementation. +func (r *Resolver) LikeNotification() LikeNotificationResolver { return &likeNotificationResolver{r} } + +type likeNotificationResolver struct{ *Resolver } + +func (r *likeNotificationResolver) CreatedAt(ctx context.Context, obj *domain.LikeNotification) (string, error) { + return obj.CreatedAt.Format(time.RFC3339), nil +}