github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/blogweb_gin/controllers/update_article_controller.go (about) 1 package controllers 2 3 import ( 4 "github.com/gin-gonic/gin" 5 "github.com/qiuhoude/go-web/blogweb_gin/logs" 6 "github.com/qiuhoude/go-web/blogweb_gin/models" 7 "net/http" 8 "strconv" 9 ) 10 11 func UpdateArticleGet(c *gin.Context) { 12 13 //获取session 14 islogin := GetSession(c) 15 16 idStr := c.Query("id") 17 id, _ := strconv.Atoi(idStr) 18 //获取id所对应的文章信息 19 art := models.QueryArticleWithId(id) 20 21 c.HTML(http.StatusOK, "write_article.html", 22 gin.H{"IsLogin": islogin, "Title": art.Title, "Tags": art.Tags, "Short": art.Short, "Content": art.Content, "Id": art.Id}) 23 } 24 25 func UpdateArticlePost(c *gin.Context) { 26 27 //获取表单信息 28 idstr := c.PostForm("id") 29 id, _ := strconv.Atoi(idstr) 30 logs.Info.Println("postid:", id) 31 32 title := c.PostForm("title") 33 tags := c.PostForm("tags") 34 short := c.PostForm("short") 35 content := c.PostForm("content") 36 logs.Info.Printf("title:%s,tags:%s\n", title, tags) 37 38 //实例化model,将它出入到数据库中 39 40 art := models.Article{ 41 Id: id, 42 Title: title, 43 Tags: tags, 44 Short: short, 45 Content: content, 46 } 47 _, err := models.UpdateArticle(art) 48 49 //返回数据给浏览器 50 response := gin.H{} 51 if err == nil { 52 //无误 53 response = gin.H{"code": 1, "message": "ok"} 54 } else { 55 response = gin.H{"code": 0, "message": "error"} 56 } 57 c.JSON(http.StatusOK, response) 58 }