2022-01-28 07:01:17 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
2022-01-28 21:39:31 +00:00
|
|
|
"errors"
|
2022-01-28 07:01:17 +00:00
|
|
|
"net/http"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"databag/internal/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func AddArticle(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
account, code, err := BearerAppToken(r, false)
|
|
|
|
if err != nil {
|
|
|
|
ErrResponse(w, code, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var articleAccess ArticleAccess
|
|
|
|
if err := ParseRequest(r, w, &articleAccess); err != nil {
|
|
|
|
ErrResponse(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var groups []store.Group
|
|
|
|
if err := store.DB.Where("group_id IN ?", articleAccess.Groups).Find(&groups).Error; err != nil {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var labels []store.Label
|
|
|
|
if err := store.DB.Where("label_id IN ?", articleAccess.Labels).Find(&labels).Error; err != nil {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-28 21:39:31 +00:00
|
|
|
var articleBlock store.ArticleBlock
|
|
|
|
if err := store.DB.Preload("Articles").Where("account_id = ?", account.ID).Last(&articleBlock).Error; err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
if err := addArticleBlock(account, &articleBlock); err != nil {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(articleBlock.Articles) >= APP_ARTICLEBLOCKSIZE {
|
|
|
|
if err := addArticleBlock(account, &articleBlock); err != nil {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 07:01:17 +00:00
|
|
|
article := &store.Article{
|
|
|
|
ArticleId: uuid.New().String(),
|
|
|
|
AccountID: account.ID,
|
|
|
|
Revision: 1,
|
|
|
|
Status: APP_ARTICLEUNCONFIRMED,
|
|
|
|
Expires: 0,
|
|
|
|
TagUpdated: 0,
|
|
|
|
TagRevision: 0,
|
|
|
|
Groups: groups,
|
|
|
|
Labels: labels,
|
|
|
|
}
|
|
|
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
2022-01-28 21:39:31 +00:00
|
|
|
if res := tx.Save(article).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
if res := tx.Model(&articleBlock).Update("revision", account.ContentRevision + 1).Error; res != nil {
|
2022-01-28 07:01:17 +00:00
|
|
|
return res
|
|
|
|
}
|
2022-01-28 21:39:31 +00:00
|
|
|
if res := tx.Model(&account).Update("content_revision", account.ContentRevision + 1).Error; res != nil {
|
2022-01-28 07:01:17 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-28 21:39:31 +00:00
|
|
|
articleEntry := &ArticleEntry{
|
|
|
|
BlockId: articleBlock.ArticleBlockId,
|
|
|
|
Article: getArticleModel(article, 0),
|
|
|
|
}
|
|
|
|
|
2022-01-28 07:01:17 +00:00
|
|
|
SetStatus(account)
|
|
|
|
SetContentNotification(account)
|
2022-01-28 21:39:31 +00:00
|
|
|
WriteResponse(w, articleEntry)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addArticleBlock(account *store.Account, articleBlock *store.ArticleBlock) error {
|
|
|
|
articleBlock.ArticleBlockId = uuid.New().String()
|
|
|
|
articleBlock.AccountID = account.ID
|
|
|
|
articleBlock.Revision = account.ContentRevision
|
|
|
|
if err := store.DB.Save(articleBlock).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2022-01-28 07:01:17 +00:00
|
|
|
}
|
2022-01-28 21:39:31 +00:00
|
|
|
|