115 lines
3.5 KiB
Go
115 lines
3.5 KiB
Go
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
|
||
}
|