customer/internal/interface/controller/rest/history/history.go
2023-09-14 10:07:28 +00:00

60 lines
1.6 KiB
Go

package history
import (
"context"
"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)
}
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 {
histories, err := receiver.historyService.GetHistoryList(ctx.Request().Context(), &dto.GetHistories{
Type: params.Type,
Pagination: &models.Pagination{
Page: int64(*params.Page),
Limit: int64(*params.Limit),
},
})
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)
}