157 lines
4.9 KiB
Go
157 lines
4.9 KiB
Go
package graph
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"tailly_back_v2/internal/domain"
|
||
"time"
|
||
)
|
||
|
||
type likeResolver struct{ *Resolver }
|
||
|
||
// Like returns LikeResolver implementation.
|
||
func (r *Resolver) Like() LikeResolver { return &likeResolver{r} }
|
||
|
||
// Post is the resolver for the post field.
|
||
func (r *likeResolver) Post(ctx context.Context, obj *domain.Like) (*domain.Post, error) {
|
||
post, err := r.Services.Post.GetByID(ctx, obj.PostID)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("failed to get post: %w", err)
|
||
}
|
||
return post, nil
|
||
}
|
||
|
||
// User is the resolver for the user field.
|
||
func (r *likeResolver) User(ctx context.Context, obj *domain.Like) (*domain.User, error) {
|
||
user, err := r.Services.User.GetByID(ctx, obj.UserID)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("failed to get user: %w", err)
|
||
}
|
||
return user, nil
|
||
}
|
||
|
||
// CreatedAt is the resolver for the createdAt field.
|
||
func (r *likeResolver) CreatedAt(ctx context.Context, obj *domain.Like) (string, error) {
|
||
return obj.CreatedAt.Format(time.RFC3339), nil
|
||
}
|
||
|
||
// IsRead is the resolver for the isRead field.
|
||
func (r *likeResolver) IsRead(ctx context.Context, obj *domain.Like) (bool, error) {
|
||
return obj.IsRead, nil
|
||
}
|
||
|
||
// NotifiedAt is the resolver for the notifiedAt field.
|
||
func (r *likeResolver) NotifiedAt(ctx context.Context, obj *domain.Like) (*string, error) {
|
||
if obj.NotifiedAt.IsZero() {
|
||
return nil, nil
|
||
}
|
||
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
|
||
}
|