package history import ( "context" "fmt" "log" "net/http" "github.com/labstack/echo/v4" "go.uber.org/zap" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/dto" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/interface/swagger" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models" ) type historyService interface { GetHistoryList(context.Context, *dto.GetHistories) (*models.PaginationResponse[models.History], errors.Error) GetRecentTariffs(context.Context, string) ([]models.TariffID, errors.Error) // new } type Deps struct { Logger *zap.Logger HistoryService historyService } type Controller struct { logger *zap.Logger historyService historyService } func New(deps Deps) *Controller { if deps.Logger == nil { log.Panicln("logger is nil on ") } if deps.HistoryService == nil { log.Panicln("HistoryService is nil on ") } return &Controller{ logger: deps.Logger, historyService: deps.HistoryService, } } func (receiver *Controller) GetHistoryList(ctx echo.Context, params swagger.GetHistoryParams) error { userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string) if !ok { receiver.logger.Error("failed to convert jwt payload to string on of ") return errors.HTTP(ctx, errors.New( fmt.Errorf("failed to convert jwt payload to string: %s", userID), errors.ErrInvalidArgs, )) } histories, err := receiver.historyService.GetHistoryList(ctx.Request().Context(), &dto.GetHistories{ Type: params.Type, UserID: userID, Pagination: &models.Pagination{ Page: int64(*params.Page), Limit: int64(*params.Limit), }, }) if err != nil { receiver.logger.Error("failed to get histories on of ", zap.Error(err)) return errors.HTTP(ctx, err) } return ctx.JSON(http.StatusOK, histories) } // TODO:tests. func (receiver *Controller) GetRecentTariffs(ctx echo.Context) error { userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string) if !ok { receiver.logger.Error("failed to convert jwt payload to string on of ") return errors.HTTP(ctx, errors.New( fmt.Errorf("failed to convert jwt payload to string: %s", userID), errors.ErrInvalidArgs, )) } tariffs, err := receiver.historyService.GetRecentTariffs(ctx.Request().Context(), userID) if err != nil { receiver.logger.Error("failed to get recent tariffs on of ", zap.String("userId", userID), zap.Error(err), ) return errors.HTTP(ctx, err) } return ctx.JSON(http.StatusOK, tariffs) }