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 type: object
required: required:
- articleId - articleId
- articleBlockId
- revision
- dataType - dataType
- data - data
- created - created
@ -4297,11 +4295,6 @@ components:
properties: properties:
articleId: articleId:
type: string type: string
articleBlockId:
type: string
revision:
type: integer
format: int64
dataType: dataType:
type: string type: string
data: data:

View File

@ -36,10 +36,10 @@ func AddArticle(w http.ResponseWriter, r *http.Request) {
} }
// save data and apply transaction // save data and apply transaction
var article *store.Article var slot *store.ArticleSlot
err = store.DB.Transaction(func(tx *gorm.DB) error { err = store.DB.Transaction(func(tx *gorm.DB) error {
articleData := &store.ArticleData{ article := &store.Article{
Status: APP_ARTICLEUNCONFIRMED, Status: APP_ARTICLEUNCONFIRMED,
TagUpdated: time.Now().Unix(), TagUpdated: time.Now().Unix(),
TagRevision: 1, TagRevision: 1,
@ -47,30 +47,30 @@ func AddArticle(w http.ResponseWriter, r *http.Request) {
Groups: groups, Groups: groups,
Labels: labels, Labels: labels,
}; };
if res := store.DB.Save(articleData).Error; res != nil { if res := store.DB.Save(article).Error; res != nil {
return res; 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) { if errors.Is(res, gorm.ErrRecordNotFound) {
article = &store.Article{ slot = &store.ArticleSlot{
ArticleId: uuid.New().String(), ArticleSlotId: uuid.New().String(),
AccountID: account.ID, AccountID: account.ID,
Revision: 1, Revision: 1,
ArticleDataID: articleData.ID, ArticleID: article.ID,
ArticleData: articleData, Article: article,
} }
if ret := store.DB.Save(article).Error; ret != nil { if ret := store.DB.Save(slot).Error; ret != nil {
return ret; return ret;
} }
} else { } else {
return res 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; 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; return ret;
} }
if ret := tx.Model(&account).Update("content_revision", account.ContentRevision + 1).Error; ret != nil { 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) SetContentNotification(account)
SetStatus(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 // save contact card
err = store.DB.Transaction(func(tx *gorm.DB) error { 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 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 res
} }
return nil return nil

View File

@ -37,7 +37,7 @@ func GetArticles(w http.ResponseWriter, r *http.Request) {
return return
} }
var articles []store.Article var articles []store.ArticleSlot
if err := getAccountArticles(account, contentRevision, &articles); err != nil { if err := getAccountArticles(account, contentRevision, &articles); err != nil {
ErrResponse(w, http.StatusInternalServerError, err) ErrResponse(w, http.StatusInternalServerError, err)
return return
@ -58,7 +58,7 @@ func GetArticles(w http.ResponseWriter, r *http.Request) {
contentRevision = 0 contentRevision = 0
} }
var articles []store.Article var articles []store.ArticleSlot
if err := getContactArticles(card, contentRevision, &articles); err != nil { if err := getContactArticles(card, contentRevision, &articles); err != nil {
ErrResponse(w, http.StatusInternalServerError, err) ErrResponse(w, http.StatusInternalServerError, err)
return return
@ -75,18 +75,18 @@ func GetArticles(w http.ResponseWriter, r *http.Request) {
} }
// better if this filtering was done in gorm or sql // better if this filtering was done in gorm or sql
func isShared(article *store.Article, guid string) bool { func isShared(slot *store.ArticleSlot, guid string) bool {
if article.ArticleData == nil { if slot.Article == nil {
return false return false
} }
for _, group := range article.ArticleData.Groups { for _, group := range slot.Article.Groups {
for _, card := range group.Cards { for _, card := range group.Cards {
if card.Guid == guid { if card.Guid == guid {
return true return true
} }
} }
} }
for _, label := range article.ArticleData.Labels { for _, label := range slot.Article.Labels {
for _, group := range label.Groups { for _, group := range label.Groups {
for _, card := range group.Cards { for _, card := range group.Cards {
if card.Guid == guid { if card.Guid == guid {
@ -98,12 +98,12 @@ func isShared(article *store.Article, guid string) bool {
return false return false
} }
func getAccountArticles(account *store.Account, revision int64, articles *[]store.Article) error { func getAccountArticles(account *store.Account, revision int64, articles *[]store.ArticleSlot) error {
return store.DB.Preload("ArticleData.Groups").Preload("ArticleData.Labels.Groups").Where("account_id = ? AND revision > ?", account.ID, revision).Find(articles).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 { func getContactArticles(card *store.Card, revision int64, articles *[]store.ArticleSlot) 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 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"] articleId := params["articleId"]
err = store.DB.Transaction(func(tx *gorm.DB) error { err = store.DB.Transaction(func(tx *gorm.DB) error {
var article store.Article var slot store.ArticleSlot
if res := store.DB.Preload("ArticleData").Where("account_id = ? AND article_id = ?", account.ID, articleId).First(&article).Error; res != nil { if res := store.DB.Preload("Article").Where("account_id = ? AND article_slot_id = ?", account.ID, articleId).First(&slot).Error; res != nil {
return res return res
} }
if article.ArticleData == nil { if slot.Article == nil {
return nil return nil
} }
if res := tx.Delete(article.ArticleData).Error; res != nil { if res := tx.Delete(slot.Article).Error; res != nil {
return res return res
} }
article.ArticleDataID = 0 slot.ArticleID = 0
article.ArticleData = nil slot.Article = nil
if res := tx.Save(&article).Error; res != nil { if res := tx.Save(&slot).Error; res != nil {
return res return res
} }
if res := tx.Model(&account).Update("content_revision", account.ContentRevision).Error; res != nil { 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 // save and update contact revision
err = store.DB.Transaction(func(tx *gorm.DB) error { 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 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 res
} }
return nil return nil

View File

@ -109,10 +109,10 @@ func SetOpenMessage(w http.ResponseWriter, r *http.Request) {
// save contact card // save contact card
err = store.DB.Transaction(func(tx *gorm.DB) error { 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 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 res
} }
return nil 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{ return &Article{
ArticleId: article.ArticleId, ArticleId: slot.ArticleSlotId,
Revision: article.Revision,
} }
} else { } else {
var groups []string; var groups []string;
if !contact { if !contact {
for _, group := range article.ArticleData.Groups { for _, group := range slot.Article.Groups {
groups = append(groups, group.GroupId) groups = append(groups, group.GroupId)
} }
} }
var labels []string; var labels []string;
for _, label := range article.ArticleData.Labels { for _, label := range slot.Article.Labels {
labels = append(labels, label.LabelId) labels = append(labels, label.LabelId)
} }
return &Article{ return &Article{
ArticleId: article.ArticleId, ArticleId: slot.ArticleSlotId,
Revision: article.Revision,
ArticleData: &ArticleData{ ArticleData: &ArticleData{
DataType: article.ArticleData.DataType, DataType: slot.Article.DataType,
Data: article.ArticleData.Data, Data: slot.Article.Data,
Status: article.ArticleData.Status, Status: slot.Article.Status,
Labels: labels, Labels: labels,
Groups: groups, Groups: groups,
TagCount: article.ArticleData.TagCount, TagCount: slot.Article.TagCount,
Created: article.ArticleData.Created, Created: slot.Article.Created,
Updated: article.ArticleData.Updated, Updated: slot.Article.Updated,
TagUpdated: article.ArticleData.TagUpdated, TagUpdated: slot.Article.TagUpdated,
TagRevision: article.ArticleData.TagRevision, TagRevision: slot.Article.TagRevision,
}, },
} }
} }

View File

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

View File

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