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/errors" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/swagger" "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/utils/echotools" ) type historyService interface { CreateHistory(context.Context, *models.History) (*models.History, errors.Error) GetHistoryList(context.Context, *models.Pagination) (*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 == nil { log.Panicln("deps is nil on ") } 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) CreateHistory(ctx echo.Context) error { request, bindErr := echotools.Bind[swagger.Add2historyJSONRequestBody](ctx) if bindErr != nil { receiver.logger.Error("failed to bind body on of ", zap.Error(bindErr)) return echotools.ResponseError(ctx, errors.New( fmt.Errorf("failed to parse body on of : %w", bindErr), errors.ErrInvalidArgs, )) } createdHistory, err := receiver.historyService.CreateHistory(ctx.Request().Context(), &models.History{ UserID: request.UserId, Type: models.HistoryType(request.Type), Comment: request.Comment, RawDetails: request.RawDetails, }) if err != nil { receiver.logger.Error("failed to create history on of ", zap.Error(err)) return echotools.ResponseError(ctx, err) } return ctx.JSON(http.StatusOK, createdHistory) } func (receiver *Controller) GetHistoryList(ctx echo.Context, params swagger.GetHistoryParams) error { histories, err := receiver.historyService.GetHistoryList(ctx.Request().Context(), &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 echotools.ResponseError(ctx, err) } return ctx.JSON(http.StatusOK, histories) }