2022-02-14 06:47:18 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"databag/internal/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SetArticleGroup(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2022-03-20 21:41:18 +00:00
|
|
|
account, code, err := ParamAgentToken(r, false);
|
2022-02-14 06:47:18 +00:00
|
|
|
if err != nil {
|
|
|
|
ErrResponse(w, code, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// scan parameters
|
|
|
|
params := mux.Vars(r)
|
|
|
|
articleId := params["articleId"]
|
|
|
|
groupId := params["groupId"]
|
|
|
|
|
|
|
|
// load referenced article
|
|
|
|
var articleSlot store.ArticleSlot
|
|
|
|
if err := store.DB.Preload("Article").Where("account_id = ? AND article_slot_id = ?", account.ID, articleId).First(&articleSlot).Error; err != nil {
|
|
|
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
|
|
} else {
|
|
|
|
ErrResponse(w, http.StatusNotFound, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if articleSlot.Article == nil {
|
|
|
|
ErrResponse(w, http.StatusNotFound, errors.New("article has been deleted"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// load referenced group
|
|
|
|
var groupSlot store.GroupSlot
|
|
|
|
if err := store.DB.Preload("Group.Cards").Preload("Group.GroupSlot").Where("account_id = ? AND group_slot_id = ?", account.ID, groupId).First(&groupSlot).Error; err != nil {
|
|
|
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
|
|
} else {
|
|
|
|
ErrResponse(w, http.StatusNotFound, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if groupSlot.Group == nil {
|
|
|
|
ErrResponse(w, http.StatusNotFound, errors.New("group has been deleted"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// save and update contact revision
|
|
|
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
|
if res := tx.Model(&articleSlot.Article).Association("Groups").Append(groupSlot.Group); res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
if res := tx.Model(&articleSlot).Update("revision", account.ArticleRevision + 1).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
if res := tx.Model(&account).Update("article_revision", account.ArticleRevision + 1).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify contacts of content change
|
|
|
|
SetStatus(account)
|
|
|
|
for _, card := range groupSlot.Group.Cards {
|
|
|
|
SetContactArticleNotification(account, &card)
|
|
|
|
}
|
|
|
|
|
2022-02-14 20:55:02 +00:00
|
|
|
WriteResponse(w, getArticleModel(&articleSlot, true, true));
|
2022-02-14 06:47:18 +00:00
|
|
|
}
|
|
|
|
|