145 lines
4.5 KiB
Go
145 lines
4.5 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"gitea.starryskymeow.cn/B309/datamarket/internal/service"
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/render"
|
|
)
|
|
|
|
type CreateAssetRequest struct {
|
|
AssetName string `json:"asset_name"`
|
|
AssetType string `json:"asset_type"`
|
|
Domain string `json:"domain"`
|
|
ApplicationScene *string `json:"application_scene"`
|
|
DataDescription string `json:"data_description"`
|
|
DataScale string `json:"data_scale"`
|
|
CollectionMethod string `json:"collection_method"`
|
|
LabelingStatus *string `json:"labeling_status"`
|
|
UpdateFrequency *string `json:"update_frequency"`
|
|
PrivacyLevel string `json:"privacy_level"`
|
|
PermissionMode string `json:"permission_mode"`
|
|
SupportsValidation bool `json:"supports_validation"`
|
|
SellerExpectedPriceMin *float64 `json:"seller_expected_price_min"`
|
|
SellerExpectedPriceMax *float64 `json:"seller_expected_price_max"`
|
|
}
|
|
|
|
func (req *CreateAssetRequest) Bind(_ *http.Request) error {
|
|
return nil
|
|
}
|
|
|
|
type ListAssetsQuery struct {
|
|
Limit int32 `schema:"limit"`
|
|
Offset int32 `schema:"offset"`
|
|
Keyword *string `schema:"keyword"`
|
|
AssetType *string `schema:"asset_type"`
|
|
Domain *string `schema:"domain"`
|
|
PrivacyLevel *string `schema:"privacy_level"`
|
|
SupportsValidation *bool `schema:"supports_validation"`
|
|
}
|
|
|
|
type UpdateStatusRequest struct {
|
|
Status string `json:"asset_status"`
|
|
}
|
|
|
|
func (req *UpdateStatusRequest) Bind(_ *http.Request) error {
|
|
return nil
|
|
}
|
|
|
|
func CreateAsset(svc service.AssetService) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
req := &CreateAssetRequest{}
|
|
if err := render.Bind(r, req); err != nil {
|
|
renderServiceError(w, r, service.NewValidationError("request data error: "+err.Error()))
|
|
return
|
|
}
|
|
|
|
result, err := svc.CreateAsset(r.Context(), service.AssetCreateInput{
|
|
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,
|
|
})
|
|
if err != nil {
|
|
renderServiceError(w, r, err)
|
|
return
|
|
}
|
|
|
|
renderSuccess(w, r, http.StatusCreated, "资产创建成功", result)
|
|
}
|
|
}
|
|
|
|
func ListAssets(svc service.AssetService) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var query ListAssetsQuery
|
|
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
|
|
renderServiceError(w, r, service.NewValidationError("request data error: "+err.Error()))
|
|
return
|
|
}
|
|
|
|
result, err := svc.ListAssets(r.Context(), service.AssetListInput{
|
|
Limit: query.Limit,
|
|
Offset: query.Offset,
|
|
Keyword: query.Keyword,
|
|
AssetType: query.AssetType,
|
|
Domain: query.Domain,
|
|
PrivacyLevel: query.PrivacyLevel,
|
|
SupportsValidation: query.SupportsValidation,
|
|
})
|
|
if err != nil {
|
|
renderServiceError(w, r, err)
|
|
return
|
|
}
|
|
|
|
renderSuccess(w, r, http.StatusOK, "success", ListResponse[service.Asset]{
|
|
List: result.List,
|
|
Total: result.Total,
|
|
Limit: result.Limit,
|
|
Offset: result.Offset,
|
|
})
|
|
}
|
|
}
|
|
|
|
func GetAsset(svc service.AssetService) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
result, err := svc.GetAsset(r.Context(), chi.URLParam(r, "id"))
|
|
if err != nil {
|
|
renderServiceError(w, r, err)
|
|
return
|
|
}
|
|
|
|
renderSuccess(w, r, http.StatusOK, "success", result)
|
|
}
|
|
}
|
|
|
|
func UpdateAssetStatus(svc service.AssetService) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
req := &UpdateStatusRequest{}
|
|
if err := render.Bind(r, req); err != nil {
|
|
renderServiceError(w, r, service.NewValidationError("request data error: "+err.Error()))
|
|
return
|
|
}
|
|
|
|
result, err := svc.UpdateAssetStatus(r.Context(), chi.URLParam(r, "id"), service.StatusUpdate{
|
|
Status: req.Status,
|
|
})
|
|
if err != nil {
|
|
renderServiceError(w, r, err)
|
|
return
|
|
}
|
|
|
|
renderSuccess(w, r, http.StatusOK, "状态更新成功", result)
|
|
}
|
|
}
|