删除文档.
()
| 989 | |
| 990 | // 删除文档. |
| 991 | func (this *DocumentController) Delete() { |
| 992 | |
| 993 | identify := this.GetString("identify") |
| 994 | docId, _ := this.GetInt("doc_id", 0) |
| 995 | |
| 996 | bookId := 0 |
| 997 | //如果是超级管理员则忽略权限判断 |
| 998 | if this.Member.IsAdministrator() { |
| 999 | book, err := models.NewBook().FindByFieldFirst("identify", identify) |
| 1000 | if err != nil { |
| 1001 | beego.Error("FindByIdentify => ", err) |
| 1002 | this.JsonResult(6002, "书籍不存在或权限不足") |
| 1003 | } |
| 1004 | bookId = book.BookId |
| 1005 | } else { |
| 1006 | bookResult, err := models.NewBookResult().FindByIdentify(identify, this.Member.MemberId) |
| 1007 | if err != nil || bookResult.RoleId == conf.BookObserver { |
| 1008 | beego.Error("FindByIdentify => ", err) |
| 1009 | this.JsonResult(6002, "书籍不存在或权限不足") |
| 1010 | } |
| 1011 | bookId = bookResult.BookId |
| 1012 | } |
| 1013 | |
| 1014 | if docId <= 0 { |
| 1015 | this.JsonResult(6001, "参数错误") |
| 1016 | } |
| 1017 | |
| 1018 | doc, err := models.NewDocument().Find(docId) |
| 1019 | if err != nil { |
| 1020 | beego.Error("Delete => ", err) |
| 1021 | this.JsonResult(6003, "删除失败") |
| 1022 | } |
| 1023 | |
| 1024 | //如果文档所属书籍错误 |
| 1025 | if doc.BookId != bookId { |
| 1026 | this.JsonResult(6004, "参数错误") |
| 1027 | } |
| 1028 | //递归删除书籍下的文档以及子文档 |
| 1029 | err = doc.RecursiveDocument(doc.DocumentId) |
| 1030 | if err != nil { |
| 1031 | beego.Error(err.Error()) |
| 1032 | this.JsonResult(6005, "删除失败") |
| 1033 | } |
| 1034 | |
| 1035 | //重置文档数量统计 |
| 1036 | models.NewBook().ResetDocumentNumber(doc.BookId) |
| 1037 | |
| 1038 | go func() { |
| 1039 | // 删除文档的索引 |
| 1040 | client := models.NewElasticSearchClient() |
| 1041 | if errDel := client.DeleteIndex(docId, false); errDel != nil && client.On { |
| 1042 | beego.Error(errDel.Error()) |
| 1043 | } |
| 1044 | }() |
| 1045 | |
| 1046 | this.JsonResult(0, "ok") |
| 1047 | } |
| 1048 |
no test coverage detected