Skip to content

Commit

Permalink
add refresh daily likes
Browse files Browse the repository at this point in the history
  • Loading branch information
KranjQ committed Dec 15, 2024
1 parent 8285aaf commit 9a7ac19
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
24 changes: 23 additions & 1 deletion cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -36,23 +37,29 @@ 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
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}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit 9a7ac19

Please sign in to comment.