v0.0.33 Уведомления о лайках
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
admin 2025-09-18 00:33:21 +03:00
parent edddc43d78
commit 3e717f8ecb
2 changed files with 106 additions and 114 deletions

View File

@ -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
}

View File

@ -48,3 +48,109 @@ func (r *likeResolver) NotifiedAt(ctx context.Context, obj *domain.Like) (*strin
formatted := obj.NotifiedAt.Format(time.RFC3339) formatted := obj.NotifiedAt.Format(time.RFC3339)
return &formatted, nil 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
}