Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions internal/apps/bots/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/goriiin/kotyari-bots_backend/internal/delivery_grpc/profiles_validator"
delivery "github.com/goriiin/kotyari-bots_backend/internal/delivery_http/bots"
gen "github.com/goriiin/kotyari-bots_backend/internal/gen/bots"
"github.com/goriiin/kotyari-bots_backend/internal/logger"
repo "github.com/goriiin/kotyari-bots_backend/internal/repo/bots"
usecase "github.com/goriiin/kotyari-bots_backend/internal/usecase/bots"
"github.com/goriiin/kotyari-bots_backend/pkg/cors"
Expand All @@ -41,6 +42,8 @@ func (b *App) Run() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

l := logger.NewLogger("bots-app", &b.config.ConfigBase)

pool, err := postgres.GetPool(ctx, b.config.Database)
if err != nil {
return fmt.Errorf("postgres.GetPool: %w", err)
Expand All @@ -63,7 +66,7 @@ func (b *App) Run() error {
profileValidator := profiles_validator.NewGrpcValidator(profilesClient)
profileGateway := profiles_getter.NewProfileGateway(profilesClient)
botsUsecase := usecase.NewService(botsRepo, profileValidator, profileGateway)
botsHandler := delivery.NewHandler(botsUsecase)
botsHandler := delivery.NewHandler(botsUsecase, l)

svr, err := gen.NewServer(botsHandler)
if err != nil {
Expand All @@ -90,7 +93,7 @@ func (b *App) Run() error {
return fmt.Errorf("failed to listen for grpc: %w", err)
}
grpcServer := grpc.NewServer()
botGrpcServer := bots.NewServer(botsUsecase)
botGrpcServer := bots.NewServer(botsUsecase, l)
botgrpc.RegisterBotServiceServer(grpcServer, botGrpcServer)

go func() {
Expand Down
5 changes: 2 additions & 3 deletions internal/apps/posts_command_producer/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ func NewPostsCommandProducerApp(config *PostsCommandProducerConfig) (*PostsComma
return nil, err
}

// TODO: ???
log := logger.NewLogger("xdd", &config.ConfigBase)
log := logger.NewLogger("posts-command-producer", &config.ConfigBase)

reader := consumer.NewKafkaConsumer(log, &config.KafkaCons)

Expand All @@ -52,7 +51,7 @@ func NewPostsCommandProducerApp(config *PostsCommandProducerConfig) (*PostsComma
return nil, err
}

handler := posts_command_producer.NewPostsHandler(grpc, p)
handler := posts_command_producer.NewPostsHandler(grpc, p, log)

return &PostsCommandProducerApp{
handler: handler,
Expand Down
5 changes: 4 additions & 1 deletion internal/apps/posts_query/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/go-faster/errors"
postsQueryHandler "github.com/goriiin/kotyari-bots_backend/internal/delivery_http/posts/posts_query"
gen "github.com/goriiin/kotyari-bots_backend/internal/gen/posts/posts_query"
"github.com/goriiin/kotyari-bots_backend/internal/logger"
postsQueryRepo "github.com/goriiin/kotyari-bots_backend/internal/repo/posts/posts_query"
"github.com/goriiin/kotyari-bots_backend/pkg/postgres"
)
Expand All @@ -23,6 +24,8 @@ type PostsQueryApp struct {
}

func NewPostsQueryApp(config *PostsQueryConfig) (*PostsQueryApp, error) {
log := logger.NewLogger("posts-query", &config.ConfigBase)

pool, err := postgres.GetPool(context.Background(), config.Database)

if err != nil {
Expand All @@ -31,7 +34,7 @@ func NewPostsQueryApp(config *PostsQueryConfig) (*PostsQueryApp, error) {

repo := postsQueryRepo.NewPostsQueryRepo(pool)

handler := postsQueryHandler.NewPostsQueryHandler(repo)
handler := postsQueryHandler.NewPostsQueryHandler(repo, log)

return &PostsQueryApp{
handler: handler,
Expand Down
7 changes: 5 additions & 2 deletions internal/apps/profiles/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
deliverygrpc "github.com/goriiin/kotyari-bots_backend/internal/delivery_grpc/profiles"
deliveryhttp "github.com/goriiin/kotyari-bots_backend/internal/delivery_http/profiles"
gen "github.com/goriiin/kotyari-bots_backend/internal/gen/profiles"
"github.com/goriiin/kotyari-bots_backend/internal/logger"
repo "github.com/goriiin/kotyari-bots_backend/internal/repo/profiles"
usecase "github.com/goriiin/kotyari-bots_backend/internal/usecase/profiles"
"github.com/goriiin/kotyari-bots_backend/pkg/cors"
Expand All @@ -37,6 +38,8 @@ func (p *ProfilesApp) Run() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

l := logger.NewLogger("profiles-app", &p.config.ConfigBase)

pool, err := postgres.GetPool(ctx, p.config.Database)
if err != nil {
return fmt.Errorf("postgres.GetPool: %w", err)
Expand All @@ -46,8 +49,8 @@ func (p *ProfilesApp) Run() error {
profilesRepo := repo.NewRepository(pool)
profilesUsecase := usecase.NewService(profilesRepo)

grpcHandler := deliverygrpc.NewGRPCHandler(profilesUsecase)
httpHandler := deliveryhttp.NewHTTPHandler(profilesUsecase)
grpcHandler := deliverygrpc.NewGRPCHandler(profilesUsecase, l)
httpHandler := deliveryhttp.NewHTTPHandler(profilesUsecase, l)

g, gCtx := errgroup.WithContext(ctx)

Expand Down
2 changes: 2 additions & 0 deletions internal/delivery_grpc/bots/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
func (s *Server) GetBot(ctx context.Context, req *botgrpc.GetBotRequest) (*botgrpc.Bot, error) {
id, err := uuid.Parse(req.GetId())
if err != nil {
s.log.Error(err, true, "GetBot: parse id")
return nil, ierrors.DomainToGRPCError(constants.ErrInvalid)
}

botModel, err := s.usecase.Get(ctx, id)
if err != nil {
s.log.Error(err, true, "GetBot: get bot")
return nil, ierrors.DomainToGRPCError(err)
}

Expand Down
5 changes: 4 additions & 1 deletion internal/delivery_grpc/bots/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/google/uuid"
bot_grpc "github.com/goriiin/kotyari-bots_backend/api/protos/bots/gen"
"github.com/goriiin/kotyari-bots_backend/internal/logger"
"github.com/goriiin/kotyari-bots_backend/internal/model"
)

Expand All @@ -15,10 +16,12 @@ type Usecase interface {
type Server struct {
bot_grpc.UnimplementedBotServiceServer
usecase Usecase
log *logger.Logger
}

func NewServer(usecase Usecase) *Server {
func NewServer(usecase Usecase, log *logger.Logger) *Server {
return &Server{
usecase: usecase,
log: log,
}
}
4 changes: 3 additions & 1 deletion internal/delivery_grpc/profiles/exist.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ func (h *GRPCHandler) ProfilesExist(ctx context.Context, req *profiles.ProfilesE
for _, idStr := range req.ProfileIds {
id, err := uuid.Parse(idStr)
if err != nil {
continue // пропускаем невалидные UUID
h.log.Warn("ProfilesExist: parse id", err)
continue
}
profileUUIDs = append(profileUUIDs, id)
}
Expand All @@ -24,6 +25,7 @@ func (h *GRPCHandler) ProfilesExist(ctx context.Context, req *profiles.ProfilesE

existenceMap, err := h.u.Exist(ctx, profileUUIDs)
if err != nil {
h.log.Error(err, true, "ProfilesExist: exist")
return nil, ierrors.DomainToGRPCError(err)
}

Expand Down
4 changes: 3 additions & 1 deletion internal/delivery_grpc/profiles/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ func (h *GRPCHandler) GetProfiles(ctx context.Context, req *profiles.GetProfiles
for _, idStr := range req.ProfileIds {
id, err := uuid.Parse(idStr)
if err != nil {
continue // Игнорируем невалидные UUID
h.log.Warn("GetProfiles: parse id", err)
continue
}
profileUUIDs = append(profileUUIDs, id)
}
Expand All @@ -23,6 +24,7 @@ func (h *GRPCHandler) GetProfiles(ctx context.Context, req *profiles.GetProfiles

profileModels, err := h.u.GetByIDs(ctx, profileUUIDs)
if err != nil {
h.log.Error(err, true, "GetProfiles: get by ids")
return nil, err
}

Expand Down
11 changes: 8 additions & 3 deletions internal/delivery_grpc/profiles/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/google/uuid"
profiles "github.com/goriiin/kotyari-bots_backend/api/protos/bot_profile/gen"
"github.com/goriiin/kotyari-bots_backend/internal/logger"
"github.com/goriiin/kotyari-bots_backend/internal/model"
)

Expand All @@ -15,9 +16,13 @@ type usecase interface {

type GRPCHandler struct {
profiles.UnimplementedProfilesServiceServer
u usecase
u usecase
log *logger.Logger
}

func NewGRPCHandler(u usecase) *GRPCHandler {
return &GRPCHandler{u: u}
func NewGRPCHandler(u usecase, log *logger.Logger) *GRPCHandler {
return &GRPCHandler{
u: u,
log: log,
}
}
2 changes: 2 additions & 0 deletions internal/delivery_http/bots/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ func (h *Handler) CreateBot(ctx context.Context, req *gen.BotInput) (gen.CreateB
ProfileIDs: profiles,
})
if err != nil {
h.log.Error(err, true, "CreateBot: create bot")
return nil, err
}

bot, profs, err := h.u.GetWithProfiles(ctx, created.ID)
if err != nil {
h.log.Error(err, true, "CreateBot: get with profiles")
return nil, err
}
return modelToDTO(&bot, profs), nil
Expand Down
3 changes: 1 addition & 2 deletions internal/delivery_http/bots/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package bots

import (
"context"
"log"

gen "github.com/goriiin/kotyari-bots_backend/internal/gen/bots"
)

func (h *Handler) DeleteBotById(ctx context.Context, params gen.DeleteBotByIdParams) (gen.DeleteBotByIdRes, error) {
log.Println("delete:", params.BotId)
err := h.u.Delete(ctx, params.BotId)
if err != nil {
h.log.Error(err, true, "DeleteBotById: delete")
return nil, err
}

Expand Down
2 changes: 2 additions & 0 deletions internal/delivery_http/bots/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ func (h *Handler) GetBotById(ctx context.Context, params gen.GetBotByIdParams) (
}, nil
}
if errors.Is(err, constants.ErrServiceUnavailable) {
h.log.Error(err, true, "GetBotById: service unavailable")
return &gen.GetBotByIdInternalServerError{
ErrorCode: constants.ServiceUnavailableMsg,
Message: err.Error(),
}, nil
}
h.log.Error(err, true, "GetBotById: get with profiles")
return nil, err
}

Expand Down
11 changes: 8 additions & 3 deletions internal/delivery_http/bots/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/google/uuid"
"github.com/goriiin/kotyari-bots_backend/internal/logger"
"github.com/goriiin/kotyari-bots_backend/internal/model"
)

Expand All @@ -20,9 +21,13 @@ type usecase interface {
}

type Handler struct {
u usecase
u usecase
log *logger.Logger
}

func NewHandler(usecase usecase) *Handler {
return &Handler{u: usecase}
func NewHandler(usecase usecase, log *logger.Logger) *Handler {
return &Handler{
u: usecase,
log: log,
}
}
7 changes: 1 addition & 6 deletions internal/delivery_http/bots/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,22 @@ package bots

import (
"context"
"log"

gen "github.com/goriiin/kotyari-bots_backend/internal/gen/bots"
)

func (h *Handler) ListBots(ctx context.Context) (gen.ListBotsRes, error) {
log.Println("ListBots")
bots, err := h.u.List(ctx)
if err != nil {
log.Println(err)
h.log.Error(err, true, "ListBots: list")
return nil, err
}

log.Println("ListBots", bots)
genBots := make([]gen.Bot, len(bots))
for i, b := range bots {
genBots[i] = *modelToDTO(&b.Bot, b.Profiles)
}

log.Println("bots list:", len(bots), genBots)

return &gen.BotList{
Data: genBots,
NextCursor: gen.OptNilString{},
Expand Down
3 changes: 3 additions & 0 deletions internal/delivery_http/bots/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func (h Handler) AddProfileToBot(ctx context.Context, params gen.AddProfileToBot
Message: "bot or profile not found",
}, nil
}
h.log.Error(err, true, "AddProfileToBot: add profile")
return &gen.AddProfileToBotInternalServerError{
ErrorCode: constants.InternalMsg,
Message: err.Error(),
Expand All @@ -32,6 +33,7 @@ func (h Handler) RemoveProfileFromBot(ctx context.Context, params gen.RemoveProf
Message: "bot not found",
}, nil
}
h.log.Error(err, true, "RemoveProfileFromBot: remove profile")
return &gen.RemoveProfileFromBotInternalServerError{
ErrorCode: constants.InternalMsg,
Message: err.Error(),
Expand All @@ -49,6 +51,7 @@ func (h Handler) GetBotProfiles(ctx context.Context, params gen.GetBotProfilesPa
Message: "bot not found",
}, nil
}
h.log.Error(err, true, "GetBotProfiles: get with profiles")
return &gen.GetBotProfilesInternalServerError{
ErrorCode: constants.InternalMsg,
Message: err.Error(),
Expand Down
1 change: 1 addition & 0 deletions internal/delivery_http/bots/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
func (h *Handler) SearchBots(ctx context.Context, params bots.SearchBotsParams) (bots.SearchBotsRes, error) {
foundBots, err := h.u.Search(ctx, params.Q)
if err != nil {
h.log.Error(err, true, "SearchBots: search")
return &bots.SearchBotsInternalServerError{
ErrorCode: constants.InternalMsg,
Message: err.Error(),
Expand Down
1 change: 1 addition & 0 deletions internal/delivery_http/bots/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
func (h *Handler) SummaryBots(ctx context.Context) (bots.SummaryBotsRes, error) {
summary, err := h.u.GetSummary(ctx)
if err != nil {
h.log.Error(err, true, "SummaryBots: get summary")
return &bots.Error{
ErrorCode: constants.InternalMsg,
Message: err.Error(),
Expand Down
2 changes: 2 additions & 0 deletions internal/delivery_http/bots/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
func (h *Handler) UpdateBotById(ctx context.Context, req *gen.BotInput, params gen.UpdateBotByIdParams) (gen.UpdateBotByIdRes, error) {
_, err := h.u.Update(ctx, dtoToModel(req, params.BotId))
if err != nil {
h.log.Error(err, true, "UpdateBotById: update")
return nil, err
}

Expand All @@ -22,6 +23,7 @@ func (h *Handler) UpdateBotById(ctx context.Context, req *gen.BotInput, params g
Message: err.Error(),
}, nil
}
h.log.Error(err, true, "UpdateBotById: get with profiles")
return nil, err
}

Expand Down
Loading