diff --git a/cmd/main/main.go b/cmd/main/main.go index 680c8cd..9a4bd00 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -195,7 +195,7 @@ func main() { uploadImage := uploadimage.NewHandler(imageUseCase, authClient, logger) deleteImage := deleteimage.NewHandler(imageUseCase, logger) getProfile := getprofile.NewHandler(imageUseCase, personalitiesClient, logger) - getCurrentProfile := getcurrentprofile.NewHandler(imageUseCase, personalitiesClient, authClient, logger) + getCurrentProfile := getcurrentprofile.NewHandler(imageUseCase, personalitiesClient, authClient, paymentsClient, logger) updateProfile := updateprofile.NewHandler(personalitiesClient, authClient, imageUseCase, logger) addReaction := addreaction.NewHandler(communicationsClient, authClient, personalitiesClient, communicationsClient, paymentsClient, imageUseCase, websocketUsecase, logger) getMatches := getmatches.NewHandler(communicationsClient, authClient, personalitiesClient, imageUseCase, logger) @@ -304,6 +304,11 @@ func main() { fmt.Printf("Error starting server: %v\n", err) } }() + stopRefresh := make(chan bool) + refreshTicker := time.NewTicker(15 * time.Second) + defer refreshTicker.Stop() + + go RefreshDailyLikes(ctx, paymentsClient, refreshTicker, stopRefresh) stop := make(chan os.Signal, 1) signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM) @@ -318,3 +323,20 @@ func main() { fmt.Println("Сервер завершил работу.") } + +func RefreshDailyLikes(ctx context.Context, client grpcpayments.PaymentClient, ticker *time.Ticker, done chan bool) { + for { + select { + case <-done: + fmt.Println("stop refresh") + return + case <-ticker.C: + req := &grpcpayments.RefreshDailyLikeBalanceRequest{} + _, err := client.RefreshDailyLikeBalance(ctx, req) + if err != nil { + fmt.Printf("Error stop refreshing daily likes: %v\n", err) + return + } + } + } +} diff --git a/internal/pkg/personalities/delivery/http/getcurrentprofile/handler.go b/internal/pkg/personalities/delivery/http/getcurrentprofile/handler.go index 5f76ac6..8935dc2 100644 --- a/internal/pkg/personalities/delivery/http/getcurrentprofile/handler.go +++ b/internal/pkg/personalities/delivery/http/getcurrentprofile/handler.go @@ -4,6 +4,7 @@ import ( "context" "github.com/go-park-mail-ru/2024_2_SaraFun/internal/models" generatedAuth "github.com/go-park-mail-ru/2024_2_SaraFun/internal/pkg/auth/delivery/grpc/gen" + generatedPayments "github.com/go-park-mail-ru/2024_2_SaraFun/internal/pkg/payments/delivery/grpc/gen" generatedPersonalities "github.com/go-park-mail-ru/2024_2_SaraFun/internal/pkg/personalities/delivery/grpc/gen" "github.com/go-park-mail-ru/2024_2_SaraFun/internal/utils/consts" "github.com/mailru/easyjson" @@ -36,8 +37,11 @@ type SessionClient interface { } type Response struct { - Profile models.Profile `json:"profile"` - Images []models.Image `json:"images"` + Profile models.Profile `json:"profile"` + Images []models.Image `json:"images"` + MoneyBalance int `json:"money_balance"` + DailyLikesBalance int `json:"daily_likes_balance"` + PurchasedLikesBalance int `json:"purchased_likes_balance"` } //easyjson:skip @@ -45,14 +49,17 @@ type Handler struct { imageService ImageService personalitiesClient generatedPersonalities.PersonalitiesClient sessionClient generatedAuth.AuthClient + paymentsClient generatedPayments.PaymentClient logger *zap.Logger } -func NewHandler(imageService ImageService, personalitiesClient generatedPersonalities.PersonalitiesClient, sessionClient generatedAuth.AuthClient, logger *zap.Logger) *Handler { +func NewHandler(imageService ImageService, personalitiesClient generatedPersonalities.PersonalitiesClient, + sessionClient generatedAuth.AuthClient, paymentsClient generatedPayments.PaymentClient, logger *zap.Logger) *Handler { return &Handler{ imageService: imageService, personalitiesClient: personalitiesClient, sessionClient: sessionClient, + paymentsClient: paymentsClient, logger: logger} } @@ -105,6 +112,15 @@ func (h *Handler) Handle(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } + + getBalancesReq := &generatedPayments.GetAllBalanceRequest{UserID: userId.UserId} + balance, err := h.paymentsClient.GetAllBalance(ctx, getBalancesReq) + if err != nil { + h.logger.Error("getbalanceserror", zap.Error(err)) + http.Error(w, "не удалось получить баланс", http.StatusInternalServerError) + return + } + profileResponse := models.Profile{ ID: int(profile.Profile.ID), FirstName: profile.Profile.FirstName, @@ -116,8 +132,11 @@ func (h *Handler) Handle(w http.ResponseWriter, r *http.Request) { BirthdayDate: profile.Profile.BirthDate, } response := Response{ - Profile: profileResponse, - Images: links, + Profile: profileResponse, + Images: links, + MoneyBalance: int(balance.MoneyBalance), + DailyLikesBalance: int(balance.DailyLikeBalance), + PurchasedLikesBalance: int(balance.PurchasedLikeBalance), } jsonData, err := easyjson.Marshal(response) if err != nil {