From 7e5f9a4561555b1bb3e79505b515eed3f211b983 Mon Sep 17 00:00:00 2001 From: viletyy Date: Fri, 28 Jun 2019 13:18:54 +0800 Subject: [PATCH] =?UTF-8?q?[ADD]=E6=96=87=E7=AB=A0=E7=BC=96=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/index.go | 1 - controllers/note.go | 25 +++++++++--- models/core.go | 8 ++++ models/note.go | 4 +- models/user.go | 16 +++----- views/details.html | 95 ++++++++++++++++++++++++++++---------------- views/note_new.html | 8 +++- 7 files changed, 102 insertions(+), 55 deletions(-) diff --git a/controllers/index.go b/controllers/index.go index d644e67..bb84198 100644 --- a/controllers/index.go +++ b/controllers/index.go @@ -60,7 +60,6 @@ func (c *IndexController) GetDetail() { c.TplName = "details.html" } - //留言 // @router /message [get] func (c *IndexController) GetMessage() { diff --git a/controllers/note.go b/controllers/note.go index 62963c7..b053572 100644 --- a/controllers/note.go +++ b/controllers/note.go @@ -2,6 +2,7 @@ package controllers import ( "bytes" + "fmt" "github.com/PuerkitoBio/goquery" "github.com/jinzhu/gorm" "liteblog/models" @@ -30,16 +31,11 @@ func (c *NoteController) Save() { // 判空 title := c.GetMustString("title", "标题不能为空") content := c.GetMustString("content", "内容不能为空") - log.Print(title) - log.Print(content) // 获取内容摘要 summary, _ := getSummary(content) - log.Print(summary) // 根据key查询文章 note, err := models.QueryNoteByKeyAndUserId(key, int(c.User.ID)) var n models.Note - log.Print(note) - log.Print(err) if err != nil { if err != gorm.ErrRecordNotFound { c.Abort500(syserrors.NewError("保存失败!", err)) @@ -53,6 +49,7 @@ func (c *NoteController) Save() { UserId: int(c.User.ID), } } else { + log.Print("进入了修改过程") //查询不报错,这存在文章,那就做更新文章操作 n = note n.Title = title @@ -64,9 +61,25 @@ func (c *NoteController) Save() { if err := models.SaveNote(&n); err != nil { c.Abort500(syserrors.NewError("保存失败!", err)) } - c.JSONOk("成功") + c.JSONOk("成功", fmt.Sprintf("/details/%s", key)) } +// 编辑文章 +// @router /edit/:key [get] +func (c *NoteController) EditPage() { + key := c.Ctx.Input.Param(":key") + note, err := models.QueryNoteByKeyAndUserId(key, int(c.User.ID)) + + if err != nil { + c.Abort500(syserrors.NewError("文章不存在!", err)) + } + + c.Data["note"] = note + c.Data["key"] = key + c.TplName = "note_new.html" +} + + func (c *NoteController) NestPrepare() { c.MustLogin() if c.User.Role != 0 { diff --git a/models/core.go b/models/core.go index 8a49ac7..b8810ad 100644 --- a/models/core.go +++ b/models/core.go @@ -7,10 +7,18 @@ import ( _ "github.com/jinzhu/gorm" _ "github.com/go-sql-driver/mysql" _ "github.com/jinzhu/gorm/dialects/mysql" + "time" ) var db = initDbConnect() +type Model struct { + ID uint `gorm:"primary_key" json:"id"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + DeletedAt *time.Time `sql:"index" json:"deleted_at"` +} + func init() { //SetMaxOpenConns用于设置最大打开的连接数 //SetMaxIdleConns用于设置闲置的连接数 diff --git a/models/note.go b/models/note.go index cc5650a..79d56df 100644 --- a/models/note.go +++ b/models/note.go @@ -2,11 +2,11 @@ package models import ( "fmt" - "github.com/jinzhu/gorm" ) + type Note struct { - gorm.Model + Model Key string `gorm:"unique_index; not null"` // 文章唯一标识 UserId int User User diff --git a/models/user.go b/models/user.go index 0329de6..8266fb7 100644 --- a/models/user.go +++ b/models/user.go @@ -1,16 +1,12 @@ package models -import ( - "github.com/jinzhu/gorm" -) - type User struct { - gorm.Model - Name string `gorm:"unique_index"` - Email string `gorm:"unique_index"` - Avatar string - Pwd string - Role int `gorm:"default:1"` // 0 管理员 1正常用户 + Model + Name string `gorm:"unique_index" json:"name"` + Email string `gorm:"unique_index" json:"email"` + Avatar string `json:"avatar"` + Pwd string `json:"-"` + Role int `gorm:"default:1" json:"role"` // 0 管理员 1正常用户 } func QueryUserByEmailAndPassword(email, password string) (user User, err error) { diff --git a/views/details.html b/views/details.html index d2cdd55..19658b6 100755 --- a/views/details.html +++ b/views/details.html @@ -7,43 +7,70 @@ {{ template "shares/link.html" .}} - {{ template "shares/header.html" .}} -
-
-
-
- {{ template "shares/note_tpl.html" .note}} -
- -
- 评论 - 写评论 + {{ template "shares/header.html" .}} +
+
+
+
+ {{ template "shares/note_tpl.html" .note}} +
+ + +
+
+ +
+

+ 一片空白 + +

+

父爱如山,不善表达。回想十多年前,总记得父亲有个宽厚的肩膀,小小的自己跨坐在上面,越过人山人海去看更广阔的天空,那个时候期望自己有一双翅膀,能够像鸟儿一样飞得高,看得远。虽然父亲有时会和自己开玩笑,但在做错事的时候会受到严厉的训斥。父亲有双粗糙的大手掌。

+
-
-
- -
-

- 一片空白 - -

-

父爱如山,不善表达。回想十多年前,总记得父亲有个宽厚的肩膀,小小的自己跨坐在上面,越过人山人海去看更广阔的天空,那个时候期望自己有一双翅膀,能够像鸟儿一样飞得高,看得远。虽然父亲有时会和自己开玩笑,但在做错事的时候会受到严厉的训斥。父亲有双粗糙的大手掌。

-
-
-
- -
-

- 一片空白 - -

-

父爱如山,不善表达。回想十多年前,总记得父亲有个宽厚的肩膀,小小的自己跨坐在上面,越过人山人海去看更广阔的天空,那个时候期望自己有一双翅膀,能够像鸟儿一样飞得高,看得远。虽然父亲有时会和自己开玩笑,但在做错事的时候会受到严厉的训斥。父亲有双粗糙的大手掌。

-
-
+
+ +
+

+ 一片空白 + +

+

父爱如山,不善表达。回想十多年前,总记得父亲有个宽厚的肩膀,小小的自己跨坐在上面,越过人山人海去看更广阔的天空,那个时候期望自己有一双翅膀,能够像鸟儿一样飞得高,看得远。虽然父亲有时会和自己开玩笑,但在做错事的时候会受到严厉的训斥。父亲有双粗糙的大手掌。

+
-
+
- {{ template "shares/footer.html" .}} +
+ {{ template "shares/footer.html" .}} + \ No newline at end of file diff --git a/views/note_new.html b/views/note_new.html index 2fef1e5..12ede48 100644 --- a/views/note_new.html +++ b/views/note_new.html @@ -31,7 +31,7 @@
@@ -40,7 +40,11 @@
-
+
+ {{ if .note }} + {{ str2html .note.Content }} + {{ end }} +