tailly_back_v2/internal/http/graph/like_notification_resolvers.go
admin d7b74cd403
All checks were successful
continuous-integration/drone/push Build is passing
v0.0.33 Уведомления о лайках
2025-09-17 23:37:15 +03:00

106 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package graph
import (
"context"
"fmt"
)
// GetLikeNotifications is the resolver for the getLikeNotifications field.
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
}
// 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
}