snapshot
This commit is contained in:
140
internal/handler/data_assets.go
Normal file
140
internal/handler/data_assets.go
Normal file
@@ -0,0 +1,140 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"gitea.starryskymeow.cn/B309/datamarket/internal/repository"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/render"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type CreateDataAssetRequest struct {
|
||||
AssetName string `json:"asset_name"`
|
||||
AssetType string `json:"asset_type"`
|
||||
Domain string `json:"domain"`
|
||||
ApplicationScene pgtype.Text `json:"application_scene"`
|
||||
DataDescription string `json:"data_description"`
|
||||
DataScale string `json:"data_scale"`
|
||||
CollectionMethod string `json:"collection_method"`
|
||||
LabelingStatus pgtype.Text `json:"labeling_status"`
|
||||
UpdateFrequency pgtype.Text `json:"update_frequency"`
|
||||
PrivacyLevel string `json:"privacy_level"`
|
||||
PermissionMode string `json:"permission_mode"`
|
||||
SupportsValidation bool `json:"supports_validation"`
|
||||
SellerExpectedPriceMin pgtype.Numeric `json:"seller_expected_price_min"`
|
||||
SellerExpectedPriceMax pgtype.Numeric `json:"seller_expected_price_max"`
|
||||
}
|
||||
|
||||
func (req *CreateDataAssetRequest) Bind(r *http.Request) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type ListDataAssetsParams struct {
|
||||
Limit *int32 `json:"limit" schema:"limit"`
|
||||
Offset *int32 `json:"offset" schema:"offset"`
|
||||
Keyword *string `json:"keyword" schema:"keyword"`
|
||||
AssetType *string `json:"asset_type" schema:"asset_type"`
|
||||
Domain *string `json:"domain" schema:"domain"`
|
||||
PrivacyLevel *string `json:"privacy_level" schema:"privacy_level"`
|
||||
SupportsValidation *bool `json:"supports_validation" schema:"supports_validation"`
|
||||
}
|
||||
|
||||
// CreateDataAsset POST /api/assets
|
||||
func CreateDataAsset(queries *repository.Queries) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
req := &CreateDataAssetRequest{}
|
||||
err := render.Bind(r, req)
|
||||
if err != nil {
|
||||
render.Status(r, http.StatusBadRequest)
|
||||
render.JSON(w, r, Response[any]{
|
||||
Code: http.StatusBadRequest,
|
||||
Message: "request data error: " + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
dataAsset, err := queries.CreateDataAsset(r.Context(), repository.CreateDataAssetParams{
|
||||
AssetName: req.AssetName,
|
||||
AssetType: req.AssetType,
|
||||
Domain: req.Domain,
|
||||
ApplicationScene: req.ApplicationScene,
|
||||
DataDescription: req.DataDescription,
|
||||
DataScale: req.DataScale,
|
||||
CollectionMethod: req.CollectionMethod,
|
||||
LabelingStatus: req.LabelingStatus,
|
||||
UpdateFrequency: req.UpdateFrequency,
|
||||
PrivacyLevel: req.PrivacyLevel,
|
||||
PermissionMode: req.PermissionMode,
|
||||
SupportsValidation: req.SupportsValidation,
|
||||
SellerExpectedPriceMin: req.SellerExpectedPriceMin,
|
||||
SellerExpectedPriceMax: req.SellerExpectedPriceMax,
|
||||
AssetStatus: "已上架",
|
||||
})
|
||||
|
||||
render.Status(r, http.StatusCreated)
|
||||
render.JSON(w, r, Response[repository.DataAsset]{
|
||||
Code: 0,
|
||||
Message: "create assets successfully",
|
||||
Data: dataAsset,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// GetDataAsset GET /api/assets/{id}
|
||||
func GetDataAsset(queries *repository.Queries) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var id pgtype.UUID
|
||||
err := id.Scan(chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
render.Status(r, http.StatusBadRequest)
|
||||
render.JSON(w, r, Response[any]{
|
||||
Code: http.StatusBadRequest,
|
||||
Message: "request data error: " + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
dataAsset, err := queries.GetDataAsset(r.Context(), id)
|
||||
if err != nil {
|
||||
if err.Error() == "no rows in result set" {
|
||||
render.Status(r, http.StatusNotFound)
|
||||
render.JSON(w, r, Response[any]{
|
||||
Code: http.StatusNotFound,
|
||||
Message: "data_asset not found",
|
||||
})
|
||||
} else {
|
||||
render.Status(r, http.StatusInternalServerError)
|
||||
render.JSON(w, r, Response[any]{
|
||||
Code: http.StatusInternalServerError,
|
||||
Message: "internal server error",
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
render.Status(r, http.StatusOK)
|
||||
render.JSON(w, r, Response[repository.DataAsset]{
|
||||
Code: 0,
|
||||
Message: "get asset successfully",
|
||||
Data: dataAsset,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ListDataAssets GET /api/assets
|
||||
func ListDataAssets(queries *repository.Queries) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
params := &ListDataAssetsParams{}
|
||||
if err := decoder.Decode(params, r.URL.Query()); err != nil {
|
||||
render.Status(r, http.StatusBadRequest)
|
||||
render.JSON(w, r, Response[any]{
|
||||
Code: http.StatusBadRequest,
|
||||
Message: "request data error: " + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
log.Println(params)
|
||||
}
|
||||
}
|
||||
11
internal/handler/handler.go
Normal file
11
internal/handler/handler.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package handler
|
||||
|
||||
import "github.com/gorilla/schema"
|
||||
|
||||
type Response[T any] struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data T `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
var decoder = schema.NewDecoder()
|
||||
Reference in New Issue
Block a user