demo
This commit is contained in:
85
internal/handler/validations.go
Normal file
85
internal/handler/validations.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"gitea.starryskymeow.cn/B309/datamarket/internal/service"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/render"
|
||||
)
|
||||
|
||||
type CreateValidationRequest struct {
|
||||
AssetID string `json:"asset_id"`
|
||||
RequestID string `json:"request_id"`
|
||||
ValidationType string `json:"validation_type"`
|
||||
}
|
||||
|
||||
func (req *CreateValidationRequest) Bind(_ *http.Request) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type ListPageQuery struct {
|
||||
Limit int32 `schema:"limit"`
|
||||
Offset int32 `schema:"offset"`
|
||||
OrderStatus string `schema:"order_status"`
|
||||
}
|
||||
|
||||
func CreateValidation(svc service.ValidationService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
req := &CreateValidationRequest{}
|
||||
if err := render.Bind(r, req); err != nil {
|
||||
renderServiceError(w, r, service.NewValidationError("request data error: "+err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
result, err := svc.CreateValidation(r.Context(), service.ValidationCreateInput{
|
||||
AssetID: req.AssetID,
|
||||
RequestID: req.RequestID,
|
||||
ValidationType: req.ValidationType,
|
||||
})
|
||||
if err != nil {
|
||||
renderServiceError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
renderSuccess(w, r, http.StatusCreated, "验证申请提交成功", result)
|
||||
}
|
||||
}
|
||||
|
||||
func GetValidation(svc service.ValidationService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
result, err := svc.GetValidation(r.Context(), chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
renderServiceError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
renderSuccess(w, r, http.StatusOK, "success", result)
|
||||
}
|
||||
}
|
||||
|
||||
func ListValidations(svc service.ValidationService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var query ListPageQuery
|
||||
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
|
||||
renderServiceError(w, r, service.NewValidationError("request data error: "+err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
result, err := svc.ListValidations(r.Context(), service.ValidationListInput{
|
||||
Limit: query.Limit,
|
||||
Offset: query.Offset,
|
||||
})
|
||||
if err != nil {
|
||||
renderServiceError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
renderSuccess(w, r, http.StatusOK, "success", ListResponse[service.Validation]{
|
||||
List: result.List,
|
||||
Total: result.Total,
|
||||
Limit: result.Limit,
|
||||
Offset: result.Offset,
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user