2022-01-28 07:01:17 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
2022-02-01 18:03:04 +00:00
|
|
|
"time"
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-02 07:55:16 +00:00
|
|
|
var groups []store.Group
|
|
|
|
if err := store.DB.Where("group_id IN ?", articleAccess.Groups).Find(&groups).Error; err != nil {
|
2022-01-28 07:01:17 +00:00
|
|
|
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-02-01 18:03:04 +00:00
|
|
|
// save data and apply transaction
|
|
|
|
var article *store.Article
|
2022-01-28 07:01:17 +00:00
|
|
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
2022-02-01 18:03:04 +00:00
|
|
|
|
|
|
|
articleData := &store.ArticleData{
|
|
|
|
Status: APP_ARTICLEUNCONFIRMED,
|
|
|
|
TagUpdated: time.Now().Unix(),
|
|
|
|
TagRevision: 1,
|
2022-02-02 07:39:25 +00:00
|
|
|
TagCount: 0,
|
2022-02-02 07:55:16 +00:00
|
|
|
Groups: groups,
|
|
|
|
Labels: labels,
|
2022-02-01 18:03:04 +00:00
|
|
|
};
|
|
|
|
if res := store.DB.Save(articleData).Error; res != nil {
|
|
|
|
return res;
|
2022-01-28 21:39:31 +00:00
|
|
|
}
|
2022-02-01 18:03:04 +00:00
|
|
|
|
2022-02-02 07:39:25 +00:00
|
|
|
if res := store.DB.Where("article_data_id = 0 AND account_id = ?", account.ID).First(&article).Error; res != nil {
|
2022-02-01 18:03:04 +00:00
|
|
|
if errors.Is(res, gorm.ErrRecordNotFound) {
|
|
|
|
article = &store.Article{
|
|
|
|
ArticleId: uuid.New().String(),
|
|
|
|
AccountID: account.ID,
|
|
|
|
Revision: 1,
|
|
|
|
ArticleDataID: articleData.ID,
|
|
|
|
ArticleData: articleData,
|
|
|
|
}
|
|
|
|
if ret := store.DB.Save(article).Error; ret != nil {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return res
|
|
|
|
}
|
2022-01-28 07:01:17 +00:00
|
|
|
}
|
2022-02-01 18:03:04 +00:00
|
|
|
if ret := store.DB.Model(article).Update("article_data_id", articleData.ID).Error; ret != nil {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if ret := store.DB.Preload("ArticleData.Labels.Groups").Where("id = ?", article.ID).First(article).Error; ret != nil {
|
|
|
|
return ret;
|
2022-01-28 07:01:17 +00:00
|
|
|
}
|
2022-02-02 07:39:25 +00:00
|
|
|
if ret := tx.Model(&account).Update("content_revision", account.ContentRevision + 1).Error; ret != nil {
|
|
|
|
return ret
|
|
|
|
}
|
2022-01-28 07:01:17 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
SetContentNotification(account)
|
2022-02-01 18:03:04 +00:00
|
|
|
SetStatus(account)
|
2022-02-02 07:39:25 +00:00
|
|
|
WriteResponse(w, getArticleModel(article, false, true))
|
2022-01-28 21:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|