customer/internal/interface/controller/rest/history/history.go

96 lines
2.7 KiB
Go
Raw Normal View History

2023-06-22 09:36:43 +00:00
package history
import (
"context"
2023-11-20 22:07:13 +00:00
"fmt"
2023-06-22 09:36:43 +00:00
"log"
"net/http"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
2023-09-14 10:07:28 +00:00
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/dto"
2023-06-22 09:36:43 +00:00
"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 {
2023-09-14 10:07:28 +00:00
GetHistoryList(context.Context, *dto.GetHistories) (*models.PaginationResponse[models.History], errors.Error)
GetRecentTariffs(context.Context, string) ([]models.TariffID, errors.Error) // new
2023-06-22 09:36:43 +00:00
}
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 <New (history controller)>")
}
if deps.HistoryService == nil {
log.Panicln("HistoryService is nil on <New (history controller)>")
}
return &Controller{
logger: deps.Logger,
historyService: deps.HistoryService,
}
}
func (receiver *Controller) GetHistoryList(ctx echo.Context, params swagger.GetHistoryParams) error {
2023-11-20 22:07:13 +00:00
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
if !ok {
receiver.logger.Error("failed to convert jwt payload to string on <GetAccount> of <AccountController>")
return errors.HTTP(ctx, errors.New(
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
errors.ErrInvalidArgs,
))
}
2023-09-14 10:07:28 +00:00
histories, err := receiver.historyService.GetHistoryList(ctx.Request().Context(), &dto.GetHistories{
2023-11-20 22:07:13 +00:00
Type: params.Type,
UserID: userID,
2023-09-14 10:07:28 +00:00
Pagination: &models.Pagination{
Page: int64(*params.Page),
Limit: int64(*params.Limit),
},
2023-06-22 09:36:43 +00:00
})
if err != nil {
receiver.logger.Error("failed to get histories on <GetHistoryList> of <HistoryController>", zap.Error(err))
return errors.HTTP(ctx, err)
}
return ctx.JSON(http.StatusOK, histories)
}
2023-11-22 17:31:17 +00:00
// TODO:tests.
2023-11-22 17:31:17 +00:00
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 <GetRecentTariffs> of <HistoryController>")
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 <GetRecentTariffs> of <HistoryController>",
zap.String("userId", userID),
zap.Error(err),
)
return errors.HTTP(ctx, err)
}
return ctx.JSON(http.StatusOK, tariffs)
}