Browse Source

添加校验

yjp 1 year ago
parent
commit
43d4257d2b

+ 6 - 1
convenient/binding/bind_item.go

@@ -38,11 +38,16 @@ func (item *BindItem[O]) bind(binder *Binder, middlewares ...api.Handler) {
 		panic("需要指定应用服务函数")
 	}
 
+	var outputZero O
+	outputZeroType := reflect.TypeOf(outputZero)
+	if outputZeroType != nil && outputZeroType.Kind() == reflect.Pointer {
+		panic("bind的输出类型不能使用指针类型")
+	}
+
 	// 给单个路由增加中间件
 	handlers := []api.Handler{
 		func(c *api.Context) {
 			var params request.Params
-			outputZero := reflectutils.Zero[O]()
 
 			// 有请求数据
 			if item.RequestParams != nil {

+ 1 - 1
convenient/binding/request/common.go

@@ -83,6 +83,6 @@ type QueryWithID interface {
 }
 
 type BaseQueryWithID struct {
-	IDPath
+	IDQuery
 	BaseQuery
 }

+ 2 - 2
examples/examples/project/application/domain/identity/request_params.go

@@ -29,10 +29,10 @@ type (
 
 	UpdateStudentsOfIdentityJsonBody struct {
 		request.IDJsonBody
-		StudentIDs string `json:"studentIds" assign:"toField:StudentIDs"`
+		StudentIDs []string `json:"studentIds" assign:"toField:StudentIDs"`
 	}
 
 	QueryStudentsOfIdentityQueryParams struct {
-		request.QueryWithID
+		request.BaseQueryWithID
 	}
 )

+ 2 - 2
examples/examples/project/application/domain/student/request_params.go

@@ -42,10 +42,10 @@ type (
 
 	UpdateIdentitiesOfStudentJsonBody struct {
 		request.IDJsonBody
-		IdentityIDs string `json:"identityIds" assign:"toField:IdentityIDs"`
+		IdentityIDs []string `json:"identityIds" assign:"toField:IdentityIDs"`
 	}
 
 	QueryIdentitiesOfStudentQueryParams struct {
-		request.QueryWithID
+		request.BaseQueryWithID
 	}
 )

+ 1 - 1
examples/examples/project/application/service/identity.go

@@ -20,7 +20,7 @@ func (app *Identity) Destroy() error {
 func (app *Identity) v1(appInstance *application.App) {
 	v1Binder := appInstance.Binder(application.RouterPrefix, "v1")
 
-	entity.BindSimple[*identity.Info](v1Binder, &entity.Simple[*identity.Info]{
+	entity.BindSimple[identity.Info](v1Binder, &entity.Simple[identity.Info]{
 		Entity:             &identity.Entity{},
 		Schema:             dbSchema,
 		CreateJsonBody:     &identity.CreateJsonBody{},

+ 1 - 1
examples/examples/project/application/service/student.go

@@ -20,7 +20,7 @@ func (app *Student) Destroy() error {
 func (app *Student) v1(appInstance *application.App) {
 	v1Binder := appInstance.Binder(application.RouterPrefix, "v1")
 
-	entity.BindSimple[*student.Info](v1Binder, &entity.Simple[*student.Info]{
+	entity.BindSimple[student.Info](v1Binder, &entity.Simple[student.Info]{
 		Entity:             &student.Entity{},
 		Schema:             dbSchema,
 		CreateJsonBody:     &student.CreateJsonBody{},

+ 1 - 1
examples/examples/project/application/service/student_and_family.go

@@ -21,7 +21,7 @@ func (app *StudentAndFamily) Destroy() error {
 func (app *StudentAndFamily) v1(appInstance *application.App) {
 	v1Binder := appInstance.Binder(application.RouterPrefix, "v1")
 
-	one2one.BindSimple(v1Binder, &one2one.Simple[student.Info, *family.Info]{
+	one2one.BindSimple(v1Binder, &one2one.Simple[student.Info, family.Info]{
 		Left:                          &student.Entity{},
 		Right:                         &family.Entity{},
 		Schema:                        dbSchema,

+ 15 - 0
examples/examples/project/main.go

@@ -35,6 +35,13 @@ import (
 // curl -X GET "http://localhost:31000/example/v1/family/get?id=8a3af91da12c4a3eb040ffb535693f1a"
 // curl -X DELETE "http://localhost:31000/example/v1/family/8a3af91da12c4a3eb040ffb535693f1a/delete"
 
+// Identity
+// curl -X POST -H "Content-Type: application/json" -d '{"name":"test"}' "http://localhost:31000/example/v1/identity/create"
+// curl -X PUT -H "Content-Type: application/json" -d '{"id":"6e12ee71397746b8920fa94e5f229366", "name":"test-new"}' "http://localhost:31000/example/v1/identity/update"
+// curl -X GET "http://localhost:31000/example/v1/identity/query?name=test-new&pageNo=1&pageSize=1"
+// curl -X GET "http://localhost:31000/example/v1/identity/get?id=6e12ee71397746b8920fa94e5f229366"
+// curl -X DELETE "http://localhost:31000/example/v1/identity/6e12ee71397746b8920fa94e5f229366/delete"
+
 // Student-Family
 // curl -X POST -H "Content-Type: application/json" -d '{"id":"fc3e96926aac46268ae783c4ad675d43", "familyId": "ed86bbb59a80429eb654d7f9f13ff116"}' "http://localhost:31000/example/v1/student/family/update"
 // curl -X GET "http://localhost:31000/example/v1/student/family/query?id=fc3e96926aac46268ae783c4ad675d43"
@@ -45,6 +52,14 @@ import (
 // curl -X GET "http://localhost:31000/example/v1/family/student/query?id=ed86bbb59a80429eb654d7f9f13ff116"
 // curl -X GET "http://localhost:31000/example/v1/family/student/queryWith?id=ed86bbb59a80429eb654d7f9f13ff116"
 
+// Student-Identity
+// curl -X POST -H "Content-Type: application/json" -d '{"id":"d21d93c7f189441fac5d27f14fdce13b", "identityIds": ["42b305bb292c4082a3e91a2967fe303c"]}' "http://localhost:31000/example/v1/student/identity/update"
+// curl -X GET "http://localhost:31000/example/v1/student/identity/query?id=d21d93c7f189441fac5d27f14fdce13b"
+
+// Identity-Student
+// curl -X POST -H "Content-Type: application/json" -d '{"id":"42b305bb292c4082a3e91a2967fe303c", "studentIds": ["d21d93c7f189441fac5d27f14fdce13b"]}' "http://localhost:31000/example/v1/identity/student/update"
+// curl -X GET "http://localhost:31000/example/v1/identity/student/query?id=42b305bb292c4082a3e91a2967fe303c"
+
 func main() {
 	application.NewApp()
 	defer application.DestroyApp()