feat: rabbitMQ is now calling from seatchtitles

This commit is contained in:
Iron_Felix 2025-11-30 02:57:11 +03:00
parent c6cebb0ed2
commit 77a63a1c74
7 changed files with 237 additions and 124 deletions

View file

@ -4,16 +4,29 @@ import (
"encoding/json"
"fmt"
oapi "nyanimedb/api"
"nyanimedb/modules/backend/rmq"
sqlc "nyanimedb/sql"
"strconv"
)
type Server struct {
db *sqlc.Queries
type Handler struct {
publisher *rmq.Publisher
}
func NewServer(db *sqlc.Queries) Server {
return Server{db: db}
func New(publisher *rmq.Publisher) *Handler {
return &Handler{publisher: publisher}
}
type Server struct {
db *sqlc.Queries
publisher *rmq.Publisher // ← добавьте это поле
}
func NewServer(db *sqlc.Queries, publisher *rmq.Publisher) *Server {
return &Server{
db: db,
publisher: publisher,
}
}
func sql2StorageType(s *sqlc.StorageTypeT) (*oapi.StorageType, error) {

View file

@ -5,8 +5,10 @@ import (
"encoding/json"
"fmt"
oapi "nyanimedb/api"
"nyanimedb/modules/backend/rmq"
sqlc "nyanimedb/sql"
"strconv"
"time"
"github.com/jackc/pgx/v5"
log "github.com/sirupsen/logrus"
@ -154,6 +156,29 @@ func (s Server) GetTitle(ctx context.Context, request oapi.GetTitleRequestObject
}
func (s Server) GetTitles(ctx context.Context, request oapi.GetTitlesRequestObject) (oapi.GetTitlesResponseObject, error) {
if request.Params.ExtSearch != nil && *request.Params.ExtSearch {
// Публикуем событие — как и просили
event := rmq.RabbitRequest{
Name: "Attack on titans",
// Status oapi.TitleStatus `json:"titlestatus,omitempty"`
// Rating float64 `json:"titleraring,omitempty"`
// Year int32 `json:"year,omitempty"`
// Season oapi.ReleaseSeason `json:"season,omitempty"`
Timestamp: time.Now(),
}
// Контекст с таймаутом (не блокируем ответ)
publishCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
if err := s.publisher.Publish(publishCtx, "events.user", event); err != nil {
log.Errorf("RMQ publish failed (non-critical): %v", err)
} else {
log.Infof("RMQ publish succeed %v", err)
}
}
opai_titles := make([]oapi.Title, 0)
word := Word2Sqlc(request.Params.Word)