|
@@ -5,6 +5,12 @@ import (
|
|
|
"encoding/csv"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
+ "reflect"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "sync/atomic"
|
|
|
+ "time"
|
|
|
+
|
|
|
"git.nspix.com/golang/micro/gateway/http"
|
|
|
"git.nspix.com/golang/rest/orm/query"
|
|
|
"git.nspix.com/golang/rest/orm/schema"
|
|
@@ -12,11 +18,6 @@ import (
|
|
|
"git.nspix.com/golang/rest/scenario"
|
|
|
"gorm.io/gorm"
|
|
|
"gorm.io/gorm/clause"
|
|
|
- "reflect"
|
|
|
- "strconv"
|
|
|
- "strings"
|
|
|
- "sync/atomic"
|
|
|
- "time"
|
|
|
)
|
|
|
|
|
|
var seq int64
|
|
@@ -204,7 +205,7 @@ func (e *Entity) actionIndex(c *http.Context) (err error) {
|
|
|
search := query.New(e.db)
|
|
|
if e.Callbacks.BeforeQuery != nil {
|
|
|
if err = e.Callbacks.BeforeQuery(e.Value, search, c); err != nil {
|
|
|
- return c.Error(8004, err.Error())
|
|
|
+ return c.Error(8001, err.Error())
|
|
|
}
|
|
|
}
|
|
|
searchSchemas := schema.VisibleField(e.Module, e.stmt.Table, scenario.Search)
|
|
@@ -214,7 +215,7 @@ func (e *Entity) actionIndex(c *http.Context) (err error) {
|
|
|
search.Offset(pv * pagesize).Limit(pagesize)
|
|
|
//添加排序支持
|
|
|
if err = search.All(models.Interface()); err != nil {
|
|
|
- return c.Error(8004, err.Error())
|
|
|
+ return c.Error(8002, err.Error())
|
|
|
}
|
|
|
return c.Success(map[string]interface{}{
|
|
|
"page": page,
|
|
@@ -227,16 +228,16 @@ func (e *Entity) actionIndex(c *http.Context) (err error) {
|
|
|
func (e *Entity) actionCreate(c *http.Context) (err error) {
|
|
|
val := reflect.New(e.refType).Interface()
|
|
|
if err = c.Bind(val); err != nil {
|
|
|
- return c.Error(8002, err.Error())
|
|
|
+ return c.Error(1005, err.Error())
|
|
|
}
|
|
|
if e.Callbacks.BeforeInsert != nil {
|
|
|
if err = e.Callbacks.BeforeInsert(val, c); err != nil {
|
|
|
- return c.Error(8004, err.Error())
|
|
|
+ return c.Error(8003, err.Error())
|
|
|
}
|
|
|
}
|
|
|
if e.Callbacks.BeforeSave != nil {
|
|
|
if err = e.Callbacks.BeforeSave(val, c); err != nil {
|
|
|
- return c.Error(8004, err.Error())
|
|
|
+ return c.Error(8005, err.Error())
|
|
|
}
|
|
|
}
|
|
|
sess := e.db.Create(val)
|
|
@@ -244,7 +245,7 @@ func (e *Entity) actionCreate(c *http.Context) (err error) {
|
|
|
if err, ok := sess.Error.(*validator.StructError); ok {
|
|
|
return c.Error(1001, err.Error())
|
|
|
} else {
|
|
|
- return c.Error(8004, sess.Error.Error())
|
|
|
+ return c.Error(8006, sess.Error.Error())
|
|
|
}
|
|
|
}
|
|
|
if e.Callbacks.AfterInsert != nil {
|
|
@@ -266,16 +267,16 @@ func (e *Entity) actionUpdate(c *http.Context) (err error) {
|
|
|
return c.Error(8004, err.Error())
|
|
|
}
|
|
|
if err = c.Bind(val); err != nil {
|
|
|
- return c.Error(8002, err.Error())
|
|
|
+ return c.Error(1005, err.Error())
|
|
|
}
|
|
|
if e.Callbacks.BeforeUpdate != nil {
|
|
|
if err = e.Callbacks.BeforeUpdate(val, c); err != nil {
|
|
|
- return c.Error(8004, err.Error())
|
|
|
+ return c.Error(8007, err.Error())
|
|
|
}
|
|
|
}
|
|
|
if e.Callbacks.BeforeSave != nil {
|
|
|
if err = e.Callbacks.BeforeSave(val, c); err != nil {
|
|
|
- return c.Error(8004, err.Error())
|
|
|
+ return c.Error(8005, err.Error())
|
|
|
}
|
|
|
}
|
|
|
sess := e.db.Model(val).Updates(val)
|
|
@@ -283,7 +284,7 @@ func (e *Entity) actionUpdate(c *http.Context) (err error) {
|
|
|
if err, ok := sess.Error.(*validator.StructError); ok {
|
|
|
return c.Error(1001, err.Error())
|
|
|
} else {
|
|
|
- return c.Error(8004, sess.Error.Error())
|
|
|
+ return c.Error(8008, sess.Error.Error())
|
|
|
}
|
|
|
}
|
|
|
if e.Callbacks.AfterUpdate != nil {
|
|
@@ -306,7 +307,7 @@ func (e *Entity) actionDelete(c *http.Context) (err error) {
|
|
|
}
|
|
|
if e.Callbacks.BeforeDelete != nil {
|
|
|
if err = e.Callbacks.BeforeDelete(val, c); err != nil {
|
|
|
- return c.Error(8004, err.Error())
|
|
|
+ return c.Error(8009, err.Error())
|
|
|
}
|
|
|
}
|
|
|
if err = e.db.Where(e.primaryKey()+"=?", c.ParamValue("id")).Model(val).Delete(val).Error; err == nil {
|
|
@@ -318,7 +319,7 @@ func (e *Entity) actionDelete(c *http.Context) (err error) {
|
|
|
"id": idStr,
|
|
|
})
|
|
|
} else {
|
|
|
- return c.Error(8009, err.Error())
|
|
|
+ return c.Error(8010, err.Error())
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -350,7 +351,7 @@ func (e *Entity) actionExport(c *http.Context) (err error) {
|
|
|
//添加排序支持
|
|
|
e.buildSortable(c, search)
|
|
|
if err = search.All(models.Interface()); err != nil {
|
|
|
- return c.Error(8004, err.Error())
|
|
|
+ return c.Error(8002, err.Error())
|
|
|
}
|
|
|
c.Response().Header().Set("Content-Type", "text/csv")
|
|
|
c.Response().Header().Set("Content-Disposition", fmt.Sprintf("attachment;filename=%s.csv", e.naming.plural+"-"+time.Now().Format("20060102")))
|