appresult.Localize applies to all /api/admin routes, so each handler's godoc now declares lang (tm, ru, en, all; default tm) and Swagger UI offers it as a dropdown. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
212 lines
6.7 KiB
Go
212 lines
6.7 KiB
Go
package backup
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"os"
|
|
|
|
"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 = "/backups"
|
|
|
|
type handler struct {
|
|
logger *logging.Logger
|
|
service *Service
|
|
}
|
|
|
|
func NewHandler(logger *logging.Logger, service *Service) handlers.Handler {
|
|
return &handler{logger: logger, service: service}
|
|
}
|
|
|
|
func (h *handler) Register(router *gin.RouterGroup) {
|
|
group := rbac.Group(router, baseURL, rbac.RequireStaff(perm.Backups))
|
|
group.GET("", h.list)
|
|
group.POST("", h.create)
|
|
group.GET("/:id/download", h.download)
|
|
group.GET("/:id", h.getByID)
|
|
group.DELETE("/:id", h.delete)
|
|
}
|
|
|
|
// list godoc
|
|
// @Summary List backups
|
|
// @Description Returns paginated SQL backup files
|
|
// @Tags backups
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param search query string false "Filter by file name"
|
|
// @Param status query string false "Filter by status (pending, completed, failed)"
|
|
// @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 /backups [get]
|
|
func (h *handler) list(c *gin.Context) {
|
|
filter, ok := parseListFilter(c)
|
|
if !ok {
|
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
|
return
|
|
}
|
|
|
|
result, err := h.service.List(c.Request.Context(), filter)
|
|
if err != nil {
|
|
h.logger.Errorf("list backups failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
|
|
}
|
|
|
|
// getByID godoc
|
|
// @Summary Get backup by ID
|
|
// @Description Returns a single backup record
|
|
// @Tags backups
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Backup 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 /backups/{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.service.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 backup failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
// create godoc
|
|
// @Summary Create SQL backup
|
|
// @Description Runs pg_dump and stores the resulting .sql file
|
|
// @Tags backups
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @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 500 {object} appresult.AppError
|
|
// @Router /backups [post]
|
|
func (h *handler) create(c *gin.Context) {
|
|
item, err := h.service.Create(c.Request.Context(), currentUserID(c))
|
|
if err != nil {
|
|
h.logger.Errorf("create backup failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
// download godoc
|
|
// @Summary Download backup SQL file
|
|
// @Description Downloads the .sql dump for a completed backup
|
|
// @Tags backups
|
|
// @Produce application/sql
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Backup 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 {file} file
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 404 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /backups/{id}/download [get]
|
|
func (h *handler) download(c *gin.Context) {
|
|
id, ok := shared.ParseID(c.Param("id"))
|
|
if !ok {
|
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
|
return
|
|
}
|
|
|
|
item, path, err := h.service.FilePath(c.Request.Context(), id)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
|
return
|
|
}
|
|
if err != nil {
|
|
h.logger.Errorf("download backup failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
if item.Status != "completed" {
|
|
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
|
return
|
|
}
|
|
if _, err := os.Stat(path); err != nil {
|
|
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
|
return
|
|
}
|
|
|
|
c.FileAttachment(path, item.FileName)
|
|
}
|
|
|
|
// delete godoc
|
|
// @Summary Delete backup
|
|
// @Description Deletes a backup record and its SQL file
|
|
// @Tags backups
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Backup 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 /backups/{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.service.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 backup failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData("backup removed"))
|
|
}
|
|
|
|
func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
|
page, limit, ok := parsePageLimit(c)
|
|
if !ok {
|
|
return ListFilter{}, false
|
|
}
|
|
filter := ListFilter{Page: page, Limit: limit, Search: queryValue(c, "search", "q")}
|
|
|
|
if raw := queryValue(c, "status"); raw != "" {
|
|
filter.Status = &raw
|
|
}
|
|
return filter, true
|
|
}
|