106 lines
3.2 KiB
Go
106 lines
3.2 KiB
Go
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
|
||
}
|