Skip to content

Commit

Permalink
增加删除命令行,和服务端的处理
Browse files Browse the repository at this point in the history
  • Loading branch information
zachrey committed Jun 11, 2018
1 parent 03e6480 commit 32b8f98
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 22 deletions.
13 changes: 12 additions & 1 deletion cmd/main.go
Expand Up @@ -97,7 +97,18 @@ func upload(filePath string) {
}

func remove(fileName string) {
fmt.Println("fileName", fileName)
res, err := http.Get("http://localhost:8888/remove?name=" + fileName)
if err != nil {
log.Fatalf("Remove failed: %s\n", err)
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
var dataMap map[string]interface{}
json.Unmarshal(body, &dataMap)
if dataMap["status"] != 0 {
log.Fatalf("Post failed: %s\n", dataMap["msg"])
}
fmt.Println("[SUCCESS] Remove file is successfully. ", fileName)
}

func checkErr(e error) {
Expand Down
54 changes: 54 additions & 0 deletions controllers/removeFile.go
@@ -0,0 +1,54 @@
package controllers

import (
"log"
"net/http"
"os"
"sync"

"github.com/gin-gonic/gin"
"github.com/zachrey/blog/models"
)

var wg sync.WaitGroup

// RemoveFile 根据标题删除文章及相关项
func RemoveFile(c *gin.Context) {
wg.Add(3)
fileName := c.Query("name")
if fileName == "" {
c.JSON(http.StatusOK, gin.H{
"status": 1,
"msg": "Filed name is empty.",
})
return
}

post := models.GetPostByTitle(fileName)

// 删除分类关联表
go func() {
models.RemovePCByPostID(post.Id)
wg.Done()
}()
// 删除标签关联表
go func() {
models.RemovePLByPostID(post.Id)
wg.Done()
}()
// 删除文件
go func() {
err := os.Remove("./posts/" + post.FileName)
if err != nil {
log.Println("[ERROR] ", err)
}
wg.Done()
}()
wg.Wait()
models.RemovePostByID(post.Id)

c.JSON(http.StatusOK, gin.H{
"status": 0,
"msg": "删除文章成功!",
})
}
Binary file modified database/test.db
Binary file not shown.
18 changes: 2 additions & 16 deletions gin.log
@@ -1,16 +1,2 @@
[GIN] 2018/06/10 - 21:02:19 | 200 | 998.6µs | 127.0.0.1 | GET /get-posts
[GIN] 2018/06/10 - 21:02:21 | 200 | 1.9988ms | 127.0.0.1 | GET /get-labels
[GIN] 2018/06/10 - 21:02:23 | 200 | 999µs | 127.0.0.1 | GET /get-posts-by-label/20
[GIN] 2018/06/10 - 21:02:24 | 200 | 1.9976ms | 127.0.0.1 | GET /get-html-str/32
[GIN] 2018/06/10 - 21:02:26 | 200 | 1.9985ms | 127.0.0.1 | GET /get-labels
[GIN] 2018/06/10 - 21:02:26 | 200 | 4.998ms | 127.0.0.1 | GET /get-posts-by-label/21
[GIN] 2018/06/10 - 21:02:28 | 200 | 999.4µs | 127.0.0.1 | GET /get-html-str/32
[GIN] 2018/06/10 - 21:02:30 | 200 | 1.0002ms | 127.0.0.1 | GET /get-categoies
[GIN] 2018/06/10 - 21:02:31 | 200 | 1.9984ms | 127.0.0.1 | GET /get-posts-by-category/82
[GIN] 2018/06/10 - 21:02:32 | 200 | 3.9973ms | 127.0.0.1 | GET /get-html-str/32
[GIN] 2018/06/10 - 21:02:34 | 200 | 0s | 127.0.0.1 | GET /get-categoies
[GIN] 2018/06/10 - 21:02:35 | 200 | 2.9996ms | 127.0.0.1 | GET /get-html-str/32
[GIN] 2018/06/10 - 21:02:36 | 200 | 0s | 127.0.0.1 | GET /get-labels
[GIN] 2018/06/10 - 21:02:38 | 200 | 3.9964ms | 127.0.0.1 | GET /get-posts
[GIN] 2018/06/10 - 21:02:41 | 200 | 998.1µs | 127.0.0.1 | GET /get-html-str/32
[GIN] 2018/06/10 - 21:02:43 | 200 | 999.8µs | 127.0.0.1 | GET /get-posts
[GIN] 2018/06/11 - 11:44:36 | 200 | 387.7766ms | ::1 | GET /remove?name=一个博客的名称
[GIN] 2018/06/11 - 11:45:16 | 200 | 133.9227ms | ::1 | POST /upload
22 changes: 22 additions & 0 deletions models/post.go
@@ -1,6 +1,7 @@
package models

import (
"database/sql"
"log"

db "github.com/zachrey/blog/database"
Expand Down Expand Up @@ -28,6 +29,20 @@ func GetPostByID(Id int64) *MPost {
return &post
}

// GetPostByTitle 根据标题获取post
func GetPostByTitle(title string) *MPost {
var post MPost
has, err := db.ORM.Table("posts").Where("title=?", title).Get(&post)
if err != nil {
log.Println("ERROR:", err)
return nil
}
if has == false {
return nil
}
return &post
}

// GetPosts 获取所有的文章
func GetPosts() *[]MPost {
var post []MPost
Expand All @@ -48,3 +63,10 @@ func InsertPost(title, fileName string, textAmount int64, ch chan int64) {
db.ORM.Table("posts").Insert(newPost)
ch <- newPost.Id
}

// RemovePostByID 根据ID删除post
func RemovePostByID(ID int64) (sql.Result, error) {
sql := "DELETE FROM posts WHERE id=?"
affacted, err := db.ORM.Sql(sql, ID).Execute()
return affacted, err
}
8 changes: 8 additions & 0 deletions models/post_category.go
@@ -1,6 +1,7 @@
package models

import (
"database/sql"
"log"

db "github.com/zachrey/blog/database"
Expand Down Expand Up @@ -43,3 +44,10 @@ func GetPostsByPCId(categoryId int64) *[]MCategoryAndPost {
}
return &posts
}

// RemovePCByPostID 根据postid删除对于的记录
func RemovePCByPostID(postID int64) (sql.Result, error) {
sql := "DELETE FROM post_category WHERE post_id=?"
affacted, err := db.ORM.Sql(sql, postID).Execute()
return affacted, err
}
8 changes: 8 additions & 0 deletions models/post_label.go
@@ -1,6 +1,7 @@
package models

import (
"database/sql"
"log"

db "github.com/zachrey/blog/database"
Expand Down Expand Up @@ -43,3 +44,10 @@ func GetPostsByPLId(labelId int64) *[]LabelAndPost {
}
return &posts
}

// RemoveByPostID 根据postid删除对于的记录
func RemovePLByPostID(postID int64) (sql.Result, error) {
sql := "DELETE FROM post_label WHERE post_id=?"
affacted, err := db.ORM.Sql(sql, postID).Execute()
return affacted, err
}
6 changes: 1 addition & 5 deletions posts/b128b7694e4de962e0088bcb1bcb254f.md
Expand Up @@ -4,8 +4,4 @@ label: 标签1, 标签2
---------------------------------------------

# 标题
**it's golang**`A`

```js
console.log(111)
```
**it's golang**
1 change: 1 addition & 0 deletions routers/router.go
Expand Up @@ -26,6 +26,7 @@ func loadRouters(router *gin.Engine) {
})

router.POST("/upload", ctrs.UpLoadFile)
router.GET("/remove", ctrs.RemoveFile)

router.GET("/get-html-str/:postid", ctrs.GetHtmlStr)

Expand Down

0 comments on commit 32b8f98

Please sign in to comment.