defining article slot instead of internal articldata

This commit is contained in:
Roland Osborne 2022-02-02 13:44:41 -08:00
parent dfcbb3c49a
commit 84285194a7
10 changed files with 56 additions and 66 deletions

View File

@ -4283,8 +4283,6 @@ components:
type: object
required:
- articleId
- articleBlockId
- revision
- dataType
- data
- created
@ -4297,11 +4295,6 @@ components:
properties:
articleId:
type: string
articleBlockId:
type: string
revision:
type: integer
format: int64
dataType:
type: string
data:

View File

@ -36,10 +36,10 @@ func AddArticle(w http.ResponseWriter, r *http.Request) {
}
// save data and apply transaction
var article *store.Article
var slot *store.ArticleSlot
err = store.DB.Transaction(func(tx *gorm.DB) error {
articleData := &store.ArticleData{
article := &store.Article{
Status: APP_ARTICLEUNCONFIRMED,
TagUpdated: time.Now().Unix(),
TagRevision: 1,
@ -47,30 +47,30 @@ func AddArticle(w http.ResponseWriter, r *http.Request) {
Groups: groups,
Labels: labels,
};
if res := store.DB.Save(articleData).Error; res != nil {
if res := store.DB.Save(article).Error; res != nil {
return res;
}
if res := store.DB.Where("article_data_id = 0 AND account_id = ?", account.ID).First(&article).Error; res != nil {
if res := store.DB.Where("article_id = 0 AND account_id = ?", account.ID).First(&slot).Error; res != nil {
if errors.Is(res, gorm.ErrRecordNotFound) {
article = &store.Article{
ArticleId: uuid.New().String(),
slot = &store.ArticleSlot{
ArticleSlotId: uuid.New().String(),
AccountID: account.ID,
Revision: 1,
ArticleDataID: articleData.ID,
ArticleData: articleData,
ArticleID: article.ID,
Article: article,
}
if ret := store.DB.Save(article).Error; ret != nil {
if ret := store.DB.Save(slot).Error; ret != nil {
return ret;
}
} else {
return res
}
}
if ret := store.DB.Model(article).Update("article_data_id", articleData.ID).Error; ret != nil {
if ret := store.DB.Model(slot).Update("article_id", article.ID).Error; ret != nil {
return ret;
}
if ret := store.DB.Preload("ArticleData.Labels.Groups").Where("id = ?", article.ID).First(article).Error; ret != nil {
if ret := store.DB.Preload("Article.Labels.Groups").Where("id = ?", article.ID).First(slot).Error; ret != nil {
return ret;
}
if ret := tx.Model(&account).Update("content_revision", account.ContentRevision + 1).Error; ret != nil {
@ -85,7 +85,7 @@ func AddArticle(w http.ResponseWriter, r *http.Request) {
SetContentNotification(account)
SetStatus(account)
WriteResponse(w, getArticleModel(article, false, true))
WriteResponse(w, getArticleModel(slot, false, true))
}

View File

@ -79,10 +79,10 @@ func AddCard(w http.ResponseWriter, r *http.Request) {
// save contact card
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := store.DB.Save(&card).Error; res != nil {
if res := tx.Save(&card).Error; res != nil {
return res
}
if res := store.DB.Model(&account).Update("card_revision", account.CardRevision + 1).Error; res != nil {
if res := tx.Model(&account).Update("card_revision", account.CardRevision + 1).Error; res != nil {
return res
}
return nil

View File

@ -37,7 +37,7 @@ func GetArticles(w http.ResponseWriter, r *http.Request) {
return
}
var articles []store.Article
var articles []store.ArticleSlot
if err := getAccountArticles(account, contentRevision, &articles); err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
@ -58,7 +58,7 @@ func GetArticles(w http.ResponseWriter, r *http.Request) {
contentRevision = 0
}
var articles []store.Article
var articles []store.ArticleSlot
if err := getContactArticles(card, contentRevision, &articles); err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
@ -75,18 +75,18 @@ func GetArticles(w http.ResponseWriter, r *http.Request) {
}
// better if this filtering was done in gorm or sql
func isShared(article *store.Article, guid string) bool {
if article.ArticleData == nil {
func isShared(slot *store.ArticleSlot, guid string) bool {
if slot.Article == nil {
return false
}
for _, group := range article.ArticleData.Groups {
for _, group := range slot.Article.Groups {
for _, card := range group.Cards {
if card.Guid == guid {
return true
}
}
}
for _, label := range article.ArticleData.Labels {
for _, label := range slot.Article.Labels {
for _, group := range label.Groups {
for _, card := range group.Cards {
if card.Guid == guid {
@ -98,12 +98,12 @@ func isShared(article *store.Article, guid string) bool {
return false
}
func getAccountArticles(account *store.Account, revision int64, articles *[]store.Article) error {
return store.DB.Preload("ArticleData.Groups").Preload("ArticleData.Labels.Groups").Where("account_id = ? AND revision > ?", account.ID, revision).Find(articles).Error
func getAccountArticles(account *store.Account, revision int64, articles *[]store.ArticleSlot) error {
return store.DB.Preload("Article.Groups").Preload("Article.Labels.Groups").Where("account_id = ? AND revision > ?", account.ID, revision).Find(articles).Error
}
func getContactArticles(card *store.Card, revision int64, articles *[]store.Article) error {
return store.DB.Preload("ArticleData.Groups.Cards").Preload("ArticleData.Labels.Groups.Cards").Where("account_id = ? AND revision > ?", card.Account.ID, revision).Find(articles).Error
func getContactArticles(card *store.Card, revision int64, articles *[]store.ArticleSlot) error {
return store.DB.Preload("Article.Groups.Cards").Preload("Article.Labels.Groups.Cards").Where("account_id = ? AND revision > ?", card.Account.ID, revision).Find(articles).Error
}

View File

@ -20,19 +20,19 @@ func RemoveArticle(w http.ResponseWriter, r *http.Request) {
articleId := params["articleId"]
err = store.DB.Transaction(func(tx *gorm.DB) error {
var article store.Article
if res := store.DB.Preload("ArticleData").Where("account_id = ? AND article_id = ?", account.ID, articleId).First(&article).Error; res != nil {
var slot store.ArticleSlot
if res := store.DB.Preload("Article").Where("account_id = ? AND article_slot_id = ?", account.ID, articleId).First(&slot).Error; res != nil {
return res
}
if article.ArticleData == nil {
if slot.Article == nil {
return nil
}
if res := tx.Delete(article.ArticleData).Error; res != nil {
if res := tx.Delete(slot.Article).Error; res != nil {
return res
}
article.ArticleDataID = 0
article.ArticleData = nil
if res := tx.Save(&article).Error; res != nil {
slot.ArticleID = 0
slot.Article = nil
if res := tx.Save(&slot).Error; res != nil {
return res
}
if res := tx.Model(&account).Update("content_revision", account.ContentRevision).Error; res != nil {

View File

@ -67,10 +67,10 @@ func SetCardStatus(w http.ResponseWriter, r *http.Request) {
// save and update contact revision
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := store.DB.Save(&card).Error; res != nil {
if res := tx.Save(&card).Error; res != nil {
return res
}
if res := store.DB.Model(&account).Update("card_revision", account.CardRevision + 1).Error; res != nil {
if res := tx.Model(&account).Update("card_revision", account.CardRevision + 1).Error; res != nil {
return res
}
return nil

View File

@ -109,10 +109,10 @@ func SetOpenMessage(w http.ResponseWriter, r *http.Request) {
// save contact card
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := store.DB.Save(&card).Error; res != nil {
if res := tx.Save(&card).Error; res != nil {
return res
}
if res := store.DB.Model(&account).Update("card_revision", account.CardRevision + 1).Error; res != nil {
if res := tx.Model(&account).Update("card_revision", account.CardRevision + 1).Error; res != nil {
return res
}
return nil

View File

@ -49,41 +49,39 @@ func getGroupModel(group *store.Group) *Group {
}
}
func getArticleModel(article *store.Article, contact bool, shared bool) *Article {
func getArticleModel(slot *store.ArticleSlot, contact bool, shared bool) *Article {
if !shared || article.ArticleData == nil {
if !shared || slot.Article == nil {
return &Article{
ArticleId: article.ArticleId,
Revision: article.Revision,
ArticleId: slot.ArticleSlotId,
}
} else {
var groups []string;
if !contact {
for _, group := range article.ArticleData.Groups {
for _, group := range slot.Article.Groups {
groups = append(groups, group.GroupId)
}
}
var labels []string;
for _, label := range article.ArticleData.Labels {
for _, label := range slot.Article.Labels {
labels = append(labels, label.LabelId)
}
return &Article{
ArticleId: article.ArticleId,
Revision: article.Revision,
ArticleId: slot.ArticleSlotId,
ArticleData: &ArticleData{
DataType: article.ArticleData.DataType,
Data: article.ArticleData.Data,
Status: article.ArticleData.Status,
DataType: slot.Article.DataType,
Data: slot.Article.Data,
Status: slot.Article.Status,
Labels: labels,
Groups: groups,
TagCount: article.ArticleData.TagCount,
Created: article.ArticleData.Created,
Updated: article.ArticleData.Updated,
TagUpdated: article.ArticleData.TagUpdated,
TagRevision: article.ArticleData.TagRevision,
TagCount: slot.Article.TagCount,
Created: slot.Article.Created,
Updated: slot.Article.Updated,
TagUpdated: slot.Article.TagUpdated,
TagRevision: slot.Article.TagRevision,
},
}
}

View File

@ -45,7 +45,6 @@ type Subject struct {
type Article struct {
ArticleId string `json:"article_id"`
Revision int64 `json:"revision"`
ArticleData *ArticleData `json:"articleData"`
}

View File

@ -12,8 +12,8 @@ func AutoMigrate(db *gorm.DB) {
db.AutoMigrate(&Label{});
db.AutoMigrate(&Card{});
db.AutoMigrate(&Asset{});
db.AutoMigrate(&ArticleSlot{});
db.AutoMigrate(&Article{});
db.AutoMigrate(&ArticleData{});
db.AutoMigrate(&ArticleAsset{});
db.AutoMigrate(&ArticleTag{});
db.AutoMigrate(&Dialogue{});
@ -178,17 +178,17 @@ type Asset struct {
Account Account
}
type Article struct {
type ArticleSlot struct {
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
ArticleId string `gorm:"not null;index:article,unique"`
AccountID uint `gorm:"not null;index:article,unique"`
ArticleSlotId string `gorm:"not null;index:articleslot,unique"`
AccountID uint `gorm:"not null;index:articleslot,unique"`
Revision int64 `gorm:"not null"`
ArticleDataID uint `gorm:"not null;default:0"`
ArticleData *ArticleData
ArticleID uint `gorm:"not null;default:0"`
Article *Article
Account Account
}
type ArticleData struct {
type Article struct {
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
DataType string `gorm:"index"`
Data string