Skip to content

Commit

Permalink
fix #53
Browse files Browse the repository at this point in the history
  • Loading branch information
phachon committed Jun 2, 2019
1 parent 90c9f45 commit 9ad1813
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
17 changes: 17 additions & 0 deletions app/models/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ func (d *Document) GetDocumentByNameParentIdAndSpaceId(name string, parentId str
return
}

// get document by name and spaceId
func (d *Document) GetDocumentByParentIdAndSpaceId(parentId string, spaceId string, docType int) (document map[string]string, err error) {
db := G.DB()
var rs *mysql.ResultSet
rs, err = db.Query(db.AR().From(Table_Document_Name).Where(map[string]interface{}{
"space_id": spaceId,
"parent_id": parentId,
"type": docType,
"is_delete": Document_Delete_False,
}).Limit(0, 1))
if err != nil {
return
}
document = rs.Row()
return
}

// delete document by document_id
func (d *Document) DeleteDBAndFile(documentId string, userId string, pageFile string, docType string) (err error) {
db := G.DB()
Expand Down
56 changes: 56 additions & 0 deletions app/models/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,62 @@ func (s *Space) Update(spaceId string, spaceValue map[string]interface{}) (id in
return
}

// update space db and file name by space_id
func (s *Space) UpdateDBAndSpaceFileName(spaceId string, spaceValue map[string]interface{}, oldName string) (id int64, err error) {
// begin update
db := G.DB()
tx, err := db.Begin(db.Config)
if err != nil {
return
}
var rs *mysql.ResultSet

// get real old space name (v0.1.2 #53 bug)
defaultDocument, err := DocumentModel.GetDocumentByParentIdAndSpaceId("0", spaceId, Document_Type_Dir)
if err != nil {
return
}
if oldName != defaultDocument["name"] {
oldName = defaultDocument["name"]
}

// update space db
spaceValue["update_time"] = time.Now().Unix()
rs, err = db.ExecTx(db.AR().Update(Table_Space_Name, spaceValue, map[string]interface{}{
"space_id": spaceId,
"is_delete": Space_Delete_False,
}),tx)
if err != nil {
tx.Rollback()
return
}
id = rs.LastInsertId

documentValue := map[string]interface{}{
"name": spaceValue["name"],
"update_time": time.Now().Unix(),
}
// update space document name
_, err = db.ExecTx(db.AR().Update(Table_Document_Name, documentValue, map[string]interface{}{
"space_id": spaceId,
"parent_id": 0,
"type": Document_Type_Dir,
}), tx)
if err != nil {
tx.Rollback()
return
}
// update space name
err = utils.Document.UpdateSpaceName(oldName, spaceValue["name"].(string))
if err != nil {
tx.Rollback()
return
}
err = tx.Commit()

return
}

// get limit spaces by search keyword
func (s *Space) GetSpacesByKeywordAndLimit(keyword string, limit int, number int) (spaces []map[string]string, err error) {

Expand Down
8 changes: 5 additions & 3 deletions app/modules/system/controllers/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,17 @@ func (this *SpaceController) Modify() {
if ok {
this.jsonError("空间名已经存在!")
}
_, err = models.SpaceModel.Update(spaceId, map[string]interface{}{

spaceValue := map[string]interface{}{
"name": name,
"description": description,
"tags": tags,
"visit_level": visitLevel,
"is_share": isShare,
"is_export": isExport,
})

}
// update space document dir name if name update
_, err = models.SpaceModel.UpdateDBAndSpaceFileName(spaceId, spaceValue, space["name"])
if err != nil {
this.ErrorLog("修改空间 " + spaceId + " 失败:" + err.Error())
this.jsonError("修改空间失败")
Expand Down
11 changes: 11 additions & 0 deletions app/utils/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ func (d *document) Update(oldPageFile string, name string, content string, docTy
return nil
}

func (d *document) UpdateSpaceName(oldSpaceName string, newName string) error {

d.lock.Lock()
defer d.lock.Unlock()

spaceOldDir := d.GetAbsPageFileByPageFile(oldSpaceName)
spaceNewDir := d.GetAbsPageFileByPageFile(newName)
err := os.Rename(spaceOldDir, spaceNewDir)
return err
}

// delete document
func (d *document) Delete(path string, docType int) error {
d.lock.Lock()
Expand Down

0 comments on commit 9ad1813

Please sign in to comment.