no message

master
DESKTOP-4RNDQIC\29019 2021-01-12 23:39:31 +08:00
parent 0861b0d590
commit 35347859a0
5 changed files with 86 additions and 23 deletions

View File

@ -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

View File

@ -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{

View File

@ -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) //删除备忘录
}

View File

@ -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"`
}

48
utils/OrmTimeHook.go Normal file
View File

@ -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)
}