diff --git a/net/server/internal/api_addArticle.go b/net/server/internal/api_addArticle.go index 4010c7dc..3830f152 100644 --- a/net/server/internal/api_addArticle.go +++ b/net/server/internal/api_addArticle.go @@ -25,6 +25,7 @@ func AddArticle(w http.ResponseWriter, r *http.Request) { err = store.DB.Transaction(func(tx *gorm.DB) error { article := &store.Article{} + article.AccountID = account.ID article.Data = subject.Data article.DataType = subject.DataType if res := tx.Save(article).Error; res != nil { diff --git a/net/server/internal/api_addChannel.go b/net/server/internal/api_addChannel.go index 655bd2d0..32452228 100644 --- a/net/server/internal/api_addChannel.go +++ b/net/server/internal/api_addChannel.go @@ -25,6 +25,7 @@ func AddChannel(w http.ResponseWriter, r *http.Request) { err = store.DB.Transaction(func(tx *gorm.DB) error { channel := &store.Channel{} + channel.AccountID = account.ID channel.Data = subject.Data channel.DataType = subject.DataType channel.DetailRevision = account.ChannelRevision + 1 diff --git a/net/server/internal/api_addChannelTopic.go b/net/server/internal/api_addChannelTopic.go index fe828470..7771283b 100644 --- a/net/server/internal/api_addChannelTopic.go +++ b/net/server/internal/api_addChannelTopic.go @@ -36,6 +36,7 @@ func AddChannelTopic(w http.ResponseWriter, r *http.Request) { } topic := &store.Topic{} + topic.AccountID = act.ID topic.ChannelID = channelSlot.Channel.ID topic.TopicSlotID = topicSlot.ID topic.Data = subject.Data diff --git a/net/server/internal/api_addChannelTopicTag.go b/net/server/internal/api_addChannelTopicTag.go index b7194da8..e6b1cc82 100644 --- a/net/server/internal/api_addChannelTopicTag.go +++ b/net/server/internal/api_addChannelTopicTag.go @@ -57,6 +57,7 @@ func AddChannelTopicTag(w http.ResponseWriter, r *http.Request) { } tag := &store.Tag{} + tag.AccountID = act.ID tag.ChannelID = channelSlot.Channel.ID tag.TopicID = topicSlot.Topic.ID tag.TagSlotID = tagSlot.ID diff --git a/net/server/internal/api_addGroup.go b/net/server/internal/api_addGroup.go index ad23e8c7..dd4f7433 100644 --- a/net/server/internal/api_addGroup.go +++ b/net/server/internal/api_addGroup.go @@ -26,12 +26,14 @@ func AddGroup(w http.ResponseWriter, r *http.Request) { data := &store.GroupData{ Data: subject.Data, + AccountID: account.ID, } if res := tx.Save(data).Error; res != nil { return res } group := &store.Group{} + group.AccountID = account.ID group.GroupDataID = data.ID group.DataType = subject.DataType if res := tx.Save(group).Error; res != nil { diff --git a/net/server/internal/api_admin.go b/net/server/internal/api_admin.go index 72ac8684..f24a7683 100644 --- a/net/server/internal/api_admin.go +++ b/net/server/internal/api_admin.go @@ -18,9 +18,5 @@ func ImportAccount(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } -func RemoveNodeAccount(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) -} diff --git a/net/server/internal/api_removeNodeAccount.go b/net/server/internal/api_removeNodeAccount.go new file mode 100644 index 00000000..d597a493 --- /dev/null +++ b/net/server/internal/api_removeNodeAccount.go @@ -0,0 +1,108 @@ +package databag + +import ( + "os" + "errors" + "strconv" + "gorm.io/gorm" + "net/http" + "databag/internal/store" + "github.com/gorilla/mux" +) + +func RemoveNodeAccount(w http.ResponseWriter, r *http.Request) { + + // get referenced account id + params := mux.Vars(r) + accountId, res := strconv.ParseUint(params["accountId"], 10, 32) + if res != nil { + ErrResponse(w, http.StatusBadRequest, res) + return + } + + if err := AdminLogin(r); err != nil { + ErrResponse(w, http.StatusUnauthorized, err) + return + } + + var account store.Account + if err := store.DB.First(&account, accountId).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + ErrResponse(w, http.StatusNotFound, err) + } else { + ErrResponse(w, http.StatusInternalServerError, err) + } + return + } + + err := store.DB.Transaction(func(tx *gorm.DB) error { + if res := tx.Where("account_id = ?", accountId).Delete(&store.Tag{}).Error; res != nil { + return res + } + if res := tx.Where("account_id = ?", accountId).Delete(&store.TagSlot{}).Error; res != nil { + return res + } + if res := tx.Where("account_id = ?", accountId).Delete(&store.Asset{}).Error; res != nil { + return res + } + if res := tx.Where("account_id = ?", accountId).Delete(&store.Topic{}).Error; res != nil { + return res + } + if res := tx.Where("account_id = ?", accountId).Delete(&store.TopicSlot{}).Error; res != nil { + return res + } + if res := tx.Where("account_id = ?", accountId).Delete(&store.ChannelSlot{}).Error; res != nil { + return res + } + if res := tx.Where("account_id = ?", accountId).Delete(&store.Channel{}).Error; res != nil { + return res + } + if res := tx.Where("account_id = ?", accountId).Delete(&store.Article{}).Error; res != nil { + return res + } + if res := tx.Where("account_id = ?", accountId).Delete(&store.ArticleSlot{}).Error; res != nil { + return res + } + if res := tx.Where("account_id = ?", accountId).Delete(&store.CardSlot{}).Error; res != nil { + return res + } + if res := tx.Where("account_id = ?", accountId).Delete(&store.Card{}).Error; res != nil { + return res + } + if res := tx.Where("account_id = ?", accountId).Delete(&store.Group{}).Error; res != nil { + return res + } + if res := tx.Where("account_id = ?", accountId).Delete(&store.GroupData{}).Error; res != nil { + return res + } + if res := tx.Where("account_id = ?", accountId).Delete(&store.Group{}).Error; res != nil { + return res + } + if res := tx.Where("account_id = ?", accountId).Delete(&store.App{}).Error; res != nil { + return res + } + if res := tx.Where("account_id = ?", accountId).Delete(&store.AccountDetail{}).Error; res != nil { + return res + } + if res := tx.Where("account_id = ?", accountId).Delete(&store.Account{}).Error; res != nil { + return res + } + if res := tx.Where("account_id = ?", accountId).Delete(&store.AccountToken{}).Error; res != nil { + return res + } + return nil + }) + if err != nil { + ErrResponse(w, http.StatusInternalServerError, err) + return + } + + // delete asset files + path := getStrConfigValue(CONFIG_ASSETPATH, ".") + "/" + account.Guid + if err = os.RemoveAll(path); err != nil { + ErrMsg(err) + } + + WriteResponse(w, nil) +} + diff --git a/net/server/internal/store/schema.go b/net/server/internal/store/schema.go index d8444cd5..b64fa7af 100644 --- a/net/server/internal/store/schema.go +++ b/net/server/internal/store/schema.go @@ -110,6 +110,7 @@ type GroupSlot struct { type Group struct { ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` GroupDataID uint `gorm:"not null;index:groupdata"` + AccountID uint DataType string `gorm:"index"` Created int64 `gorm:"autoCreateTime"` Updated int64 `gorm:"autoUpdateTime"` @@ -123,6 +124,7 @@ type Group struct { type GroupData struct { ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` Data string + AccountID uint } type CardSlot struct { @@ -177,6 +179,7 @@ type ArticleSlot struct { type Article struct { ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` + AccountID uint DataType string `gorm:"index"` Data string Created int64 `gorm:"autoCreateTime"` @@ -197,6 +200,7 @@ type ChannelSlot struct { type Channel struct { ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` + AccountID uint TopicRevision int64 `gorm:"not null"` DetailRevision int64 `gorm:"not null"` DataType string `gorm:"index"` @@ -223,6 +227,7 @@ type TopicSlot struct { type Topic struct { ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` DetailRevision int64 `gorm:"not null"` + AccountID uint ChannelID uint TopicSlotID uint `gorm:"not null;index:topictopicslot,unique"` Guid string @@ -274,6 +279,7 @@ type TagSlot struct { type Tag struct { ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` TagSlotID uint `gorm:"not null;index:tagtagslot,unique"` + AccountID uint ChannelID uint `gorm:"not null;index:channeltag"` TopicID uint `gorm:"not null;index:topictag"` Guid string `gorm:"not null"`