Files
crawlab/core/controllers/export.go
Marvin Zhang 43691d0d8a refactor: enhance controller parameter handling and error responses
- Refactored export and filter controller methods to utilize structured parameter types for improved clarity and maintainability.
- Consolidated error handling and response generation, ensuring consistent API responses across methods.
- Updated function signatures to eliminate unnecessary context parameters, enhancing type safety and readability.
- Improved handling of query parameters and conditions, streamlining the logic for better performance and maintainability.
2025-03-13 17:43:55 +08:00

89 lines
2.6 KiB
Go

package controllers
import (
"fmt"
"github.com/crawlab-team/crawlab/core/constants"
"github.com/crawlab-team/crawlab/core/export"
"github.com/crawlab-team/crawlab/core/interfaces"
"github.com/gin-gonic/gin"
"github.com/juju/errors"
)
type PostExportParams struct {
Type string `path:"type" validate:"required"`
Target string `query:"target" validate:"required"`
Filter interfaces.Filter `query:"filter"`
}
func PostExport(_ *gin.Context, params *PostExportParams) (response *Response[string], err error) {
var exportId string
switch params.Type {
case constants.ExportTypeCsv:
exportId, err = export.GetCsvService().Export(params.Type, params.Target, params.Filter)
case constants.ExportTypeJson:
exportId, err = export.GetJsonService().Export(params.Type, params.Target, params.Filter)
default:
return GetErrorResponse[string](errors.BadRequestf("invalid export type: %s", params.Type))
}
if err != nil {
return GetErrorResponse[string](err)
}
return GetDataResponse(exportId)
}
type GetExportParams struct {
Type string `path:"type" validate:"required"`
Id string `path:"id" validate:"required"`
}
func GetExport(_ *gin.Context, params *GetExportParams) (response *Response[interfaces.Export], err error) {
var exp interfaces.Export
switch params.Type {
case constants.ExportTypeCsv:
exp, err = export.GetCsvService().GetExport(params.Id)
case constants.ExportTypeJson:
exp, err = export.GetJsonService().GetExport(params.Id)
default:
return GetErrorResponse[interfaces.Export](errors.BadRequestf("invalid export type: %s", params.Type))
}
if err != nil {
return GetErrorResponse[interfaces.Export](err)
}
return GetDataResponse(exp)
}
type GetExportDownloadParams struct {
Type string `path:"type" validate:"required"`
Id string `path:"id" validate:"required"`
}
func GetExportDownload(c *gin.Context, params *GetExportDownloadParams) (err error) {
var exp interfaces.Export
switch params.Type {
case constants.ExportTypeCsv:
exp, err = export.GetCsvService().GetExport(params.Id)
case constants.ExportTypeJson:
exp, err = export.GetJsonService().GetExport(params.Id)
default:
return errors.BadRequestf("invalid export type: %s", params.Type)
}
if err != nil {
return err
}
switch params.Type {
case constants.ExportTypeCsv:
c.Header("Content-Type", "text/csv")
case constants.ExportTypeJson:
c.Header("Content-Type", "text/plain")
default:
return errors.BadRequestf("invalid export type: %s", params.Type)
}
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", exp.GetDownloadPath()))
c.File(exp.GetDownloadPath())
return nil
}