package loansdocument import ( "errors" "net/http" "strconv" "strings" "online-back/internal/appresult" "online-back/internal/handlers" "online-back/internal/identity/rbac" "online-back/internal/identity/rbac/perm" "online-back/internal/shared" "online-back/pkg/logging" "github.com/gin-gonic/gin" "github.com/jackc/pgx/v4" ) const baseURL = "/loans-documents" type handler struct { logger *logging.Logger repository Repository } func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler { return &handler{logger: logger, repository: repository} } func (h *handler) Register(router *gin.RouterGroup) { group := rbac.Group(router, baseURL, rbac.Catalog(perm.Loans)) group.GET("", h.list) group.GET("/:id", h.getByID) group.POST("", h.create) group.PUT("/:id", h.update) group.PUT("/:id/active", h.updateActive) group.DELETE("/:id", h.delete) } // list godoc // @Summary List loan documents // @Description Returns paginated loan documents with optional filters // @Tags loan-document // @Produce json // @Security BearerAuth // @Param document_name query string false "Filter by name" // @Param active query bool false "Filter by active" // @Param page query int false "Page number" default(1) // @Param limit query int false "Items per page" default(10) // @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm) // @Success 200 {object} appresult.AppSuccess // @Failure 400 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /loans-documents [get] func (h *handler) list(c *gin.Context) { filter, ok := parseListFilter(c) if !ok { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } result, err := h.repository.List(c.Request.Context(), filter) if err != nil { h.logger.Errorf("list loan documents failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } c.JSON(http.StatusOK, appresult.SuccessWithData(result)) } // getByID godoc // @Summary Get loan document by ID // @Description Returns a single loan document // @Tags loan-document // @Produce json // @Security BearerAuth // @Param id path int true "Loan document ID" // @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm) // @Success 200 {object} appresult.AppSuccess // @Failure 400 {object} appresult.AppError // @Failure 404 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /loans-documents/{id} [get] func (h *handler) getByID(c *gin.Context) { id, ok := shared.ParseID(c.Param("id")) if !ok { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } item, err := h.repository.GetByID(c.Request.Context(), id) if errors.Is(err, pgx.ErrNoRows) { c.JSON(http.StatusNotFound, appresult.ErrNotFound) return } if err != nil { h.logger.Errorf("get loan document failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } c.JSON(http.StatusOK, appresult.SuccessWithData(item)) } // create godoc // @Summary Create loan document // @Description Creates a new loan document // @Tags loan-document // @Accept json // @Produce json // @Security BearerAuth // @Param body body CreateLoanDocumentDTO true "Loan document payload" // @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm) // @Success 201 {object} appresult.AppSuccess // @Failure 400 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /loans-documents [post] func (h *handler) create(c *gin.Context) { var dto CreateLoanDocumentDTO if err := c.ShouldBindJSON(&dto); err != nil { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } item, err := h.repository.Create(c.Request.Context(), dto) if err != nil { h.logger.Errorf("create loan document failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } c.JSON(http.StatusCreated, appresult.SuccessWithData(item)) } // update godoc // @Summary Update loan document // @Description Updates an existing loan document // @Tags loan-document // @Accept json // @Produce json // @Security BearerAuth // @Param id path int true "Loan document ID" // @Param body body UpdateLoanDocumentDTO true "Loan document payload" // @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm) // @Success 200 {object} appresult.AppSuccess // @Failure 400 {object} appresult.AppError // @Failure 404 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /loans-documents/{id} [put] func (h *handler) update(c *gin.Context) { id, ok := shared.ParseID(c.Param("id")) if !ok { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } var dto UpdateLoanDocumentDTO if err := c.ShouldBindJSON(&dto); err != nil { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } item, err := h.repository.Update(c.Request.Context(), id, dto) if errors.Is(err, pgx.ErrNoRows) { c.JSON(http.StatusNotFound, appresult.ErrNotFound) return } if err != nil { h.logger.Errorf("update loan document failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } c.JSON(http.StatusOK, appresult.SuccessWithData(item)) } // updateActive godoc // @Summary Update loan document active // @Description Enables or disables a loan document // @Tags loan-document // @Accept json // @Produce json // @Security BearerAuth // @Param id path int true "Loan document ID" // @Param body body UpdateLoanDocumentActiveDTO true "Active payload" // @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm) // @Success 200 {object} appresult.AppSuccess // @Failure 400 {object} appresult.AppError // @Failure 404 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /loans-documents/{id}/active [put] func (h *handler) updateActive(c *gin.Context) { id, ok := shared.ParseID(c.Param("id")) if !ok { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } var dto UpdateLoanDocumentActiveDTO if err := c.ShouldBindJSON(&dto); err != nil { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } item, err := h.repository.UpdateActive(c.Request.Context(), id, dto.Active) if errors.Is(err, pgx.ErrNoRows) { c.JSON(http.StatusNotFound, appresult.ErrNotFound) return } if err != nil { h.logger.Errorf("update loan document active failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } c.JSON(http.StatusOK, appresult.SuccessWithData(item)) } // delete godoc // @Summary Delete loan document // @Description Deletes a loan document // @Tags loan-document // @Produce json // @Security BearerAuth // @Param id path int true "Loan document ID" // @Param lang query string false "Language of localized fields; all returns the full {en, ru, tm} objects" Enums(tm, ru, en, all) default(tm) // @Success 200 {object} appresult.AppSuccess // @Failure 400 {object} appresult.AppError // @Failure 404 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /loans-documents/{id} [delete] func (h *handler) delete(c *gin.Context) { id, ok := shared.ParseID(c.Param("id")) if !ok { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) { c.JSON(http.StatusNotFound, appresult.ErrNotFound) return } else if err != nil { h.logger.Errorf("delete loan document failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } c.JSON(http.StatusOK, appresult.SuccessWithData("loan document removed")) } func parseListFilter(c *gin.Context) (ListFilter, bool) { filter := ListFilter{ Page: shared.DefaultPage, Limit: shared.DefaultLimit, } if raw := c.Query("page"); raw != "" { page, err := strconv.Atoi(raw) if err != nil || page < 1 { return ListFilter{}, false } filter.Page = page } if raw := c.Query("limit"); raw != "" { limit, err := strconv.Atoi(raw) if err != nil || limit < 1 || limit > shared.MaxLimit { return ListFilter{}, false } filter.Limit = limit } if raw := strings.TrimSpace(c.Query("document_name")); raw != "" { filter.DocumentName = raw } if raw := c.Query("active"); raw != "" { active, err := strconv.ParseBool(raw) if err != nil { return ListFilter{}, false } filter.Active = &active } return filter, true }