v.0.2 Добавлено создание уведомления при подписке
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
admin 2025-09-16 22:33:43 +03:00
parent 38d91d9f10
commit 52ce331f37

View File

@ -2,17 +2,18 @@ package main
import (
"context"
"log"
"net"
"os"
"tailly_subscribers/proto"
"time"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/status"
"log"
"net"
"os"
"tailly_subscribers/proto"
"time"
)
type server struct {
@ -34,7 +35,6 @@ func (s *server) FollowUser(ctx context.Context, req *proto.FollowRequest) (*pro
return nil, status.Error(codes.InvalidArgument, "cannot follow yourself")
}
// ИСПРАВЛЕНИЕ: Используем QueryRow чтобы получить количество вставленных строк
var subscriptionID int32
err := s.db.QueryRow(ctx, `
INSERT INTO subscriptions (follower_id, following_id)
@ -45,7 +45,6 @@ func (s *server) FollowUser(ctx context.Context, req *proto.FollowRequest) (*pro
if err != nil {
if err == pgx.ErrNoRows {
// Конфликт - подписка уже существует
return &proto.FollowResponse{
Success: false,
Message: "Already following this user",
@ -54,6 +53,19 @@ func (s *server) FollowUser(ctx context.Context, req *proto.FollowRequest) (*pro
return nil, status.Errorf(codes.Internal, "failed to follow: %v", err)
}
var notificationID int32
err = s.db.QueryRow(ctx, `
INSERT INTO subscription_notifications (subscription_id)
VALUES ($1)
RETURNING id`,
subscriptionID).Scan(&notificationID)
if err != nil {
s.logger.Printf("Warning: failed to create notification for subscription %d: %v", subscriptionID, err)
} else {
s.logger.Printf("Created notification %d for subscription %d", notificationID, subscriptionID)
}
return &proto.FollowResponse{
Success: true,
Message: "Successfully followed user",