From 35347859a04ede3011c14223b68ea37cf3b0ebc0 Mon Sep 17 00:00:00 2001 From: "DESKTOP-4RNDQIC\\29019" <290198252@qq.com> Date: Tue, 12 Jan 2021 23:39:31 +0800 Subject: [PATCH] no message --- controller/blog.go | 26 ++++++++++++++++++++++-- db/utils.go | 3 --- main.go | 9 +++------ model/book.go | 23 ++++++++++----------- utils/OrmTimeHook.go | 48 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 23 deletions(-) create mode 100644 utils/OrmTimeHook.go diff --git a/controller/blog.go b/controller/blog.go index c1af37d..86209cd 100644 --- a/controller/blog.go +++ b/controller/blog.go @@ -379,14 +379,36 @@ func CreateBook(c *gin.Context) { defer func() { c.JSON(200, resp) }() - var req model.Memo + var req model.Book e := c.BindJSON(&req) if nil != e { logs.Error(e.Error()) resp.Msg = "wrong input" return } - e = model.CreateMemo(req) + req.ID = 0 + e = db.GetOrm().Create(&req).Error + if nil != e { + logs.Error(e.Error()) + return + } + resp.Data = nil + resp.Msg = "OK" + resp.Status = 0 +} + +func UpdateBook(c *gin.Context) { + resp := RespBase{"unkown error", -231, nil} + defer func() { + c.JSON(200, resp) + }() + var req model.Book + e := c.BindJSON(&req) + if nil != e { + resp.Msg = "wrong input" + return + } + e = db.GetOrm().Model(&model.Book{}).Save(&req).Error if nil != e { logs.Error(e.Error()) return diff --git a/db/utils.go b/db/utils.go index ffccd9b..d143479 100644 --- a/db/utils.go +++ b/db/utils.go @@ -31,7 +31,6 @@ func Atoi(s string, d ...int) int { return i } - // Atoi64 转换成整型int64 func Atoi64(s string, d ...int64) int64 { i, err := strconv.ParseInt(s, 10, 64) @@ -45,7 +44,6 @@ func Atoi64(s string, d ...int64) int64 { return i } - // 返回一个带有Null值的数据库字符串 func NewNullString(s string) sql.NullString { if len(s) == 0 { @@ -56,7 +54,6 @@ func NewNullString(s string) sql.NullString { Valid: true, } } - // 返回一个带有Null值的数据库整形 func NewNullInt64(s int64, isNull bool) sql.NullInt64 { return sql.NullInt64{ diff --git a/main.go b/main.go index 948e022..3d5edc1 100644 --- a/main.go +++ b/main.go @@ -96,11 +96,8 @@ func main() { InitLogs() InitRedisConfig() InitMysql() - //InitElasticSearch() + InitElasticSearch() - //o := db.GetMongoDb() - //mgo = mgo - //config.GetMongoDBConfig() r := gin.Default() store := sessions.NewCookieStore([]byte("secret123")) r.Use(sessions.Middleware("sess_store", store)) @@ -173,8 +170,8 @@ func main() { - api.PUT("/book", controller.CreateMemo) // 备忘录新建 - api.POST("/book", controller.UpdateMemo) // 备忘录更新 + api.PUT("/book", controller.CreateBook) // 备忘录新建 + api.POST("/book", controller.UpdateBook) // 备忘录更新 api.POST("/books", controller.GetMemos) // 备忘录批量 api.POST("/delbook", controller.DeleteMemos) //删除备忘录 } diff --git a/model/book.go b/model/book.go index 6954aa2..8d72e45 100644 --- a/model/book.go +++ b/model/book.go @@ -1,20 +1,19 @@ package model - -import ( - "time" -) +import "background/utils" // Book sss type Book struct { - ID int64 `sql:"id" json:"id"` - BookName string `sql:"book_name" json:"book_name"` - Author string `sql:"size" json:"size"` - Title string `sql:"title" json:"title"` - Tag string `sql:"tag" json:"json"` - CreateTime time.Time `sql:"create_time" json:"create_time"` + ID int64 `gorm:"column:id;primaryKey;default:0" sql:"id" json:"id"` + BookName string `gorm:"column:book_name" sql:"book_name" json:"book_name"` + Author string `gorm:"column:author" sql:"size" json:"size"` + Title string `gorm:"column:title" sql:"title" json:"title"` + Tag string `gorm:"column:tag" sql:"tag" json:"json"` + CreateTime utils.OrmTime ` gorm:"column:create_time" sql:"create_time" json:"create_time"` +} +// 设置User的表名为`profiles` +func (Book) TableName() string { + return "book" } - // ReadHistory sss type ReadHistory struct { ID int64 `sql:"id" json:"id"` - } \ No newline at end of file diff --git a/utils/OrmTimeHook.go b/utils/OrmTimeHook.go new file mode 100644 index 0000000..813ab5f --- /dev/null +++ b/utils/OrmTimeHook.go @@ -0,0 +1,48 @@ +package utils +import ( + "fmt" + "time" + "database/sql/driver" + "reflect" +) + +type OrmTime struct { + time.Time +} + +//重写 MarshaJSON 方法,在此方法中实现自定义格式的转换;程序中解析到JSON +func (t OrmTime) MarshalJSON() ([]byte, error) { + formatted := fmt.Sprintf("\"%s\"", t.Format("2006-01-02 15:04:05")) + return []byte(formatted), nil +} +//JSON中解析到程序中 +func (t *OrmTime) UnmarshalJSON(data []byte) (err error) { + now, err := time.ParseInLocation(`"`+"2006-01-02 15:04:05"+`"`, string(data), time.Local) + *t = OrmTime{Time: now} + return +} +//写入数据库时会调用该方法将自定义时间类型转换并写入数据库 +func (t OrmTime) Value() (driver.Value, error) { + var zeroTime time.Time + if t.Time.UnixNano() == zeroTime.UnixNano() { + return nil, nil + } + return t.Time, nil +} +//读取数据库时会调用该方法将时间数据转换成自定义时间类型 +func (t *OrmTime) Scan(v interface{}) error { + typename := reflect.TypeOf(v).Kind().String() + x := reflect.Slice.String() + if typename == x { + tmp,ok := v.([]byte) + if ok{ + ft,e := time.Parse("2006-01-02 15:04:05",string(tmp)) + if nil != e{ + return fmt.Errorf("can not convert %v to timestamp", v) + } + *t = OrmTime{Time:ft} + return nil + } + } + return fmt.Errorf("can not convert %v to timestamp", v) +}