diff --git a/server.go b/server.go index 40a2838..6dc5dc2 100644 --- a/server.go +++ b/server.go @@ -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(¬ificationID) + + 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",