mirror of
https://github.com/balzack/databag.git
synced 2025-03-13 00:50:03 +00:00
split out labeldata and groupdata to optimize article get
This commit is contained in:
parent
f94f7a2e7d
commit
83e1afc25e
@ -40,7 +40,6 @@ func AddArticle(w http.ResponseWriter, r *http.Request) {
|
||||
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
||||
|
||||
articleData := &store.ArticleData{
|
||||
DataRevision: 1,
|
||||
Status: APP_ARTICLEUNCONFIRMED,
|
||||
TagUpdated: time.Now().Unix(),
|
||||
TagRevision: 1,
|
||||
|
@ -24,12 +24,19 @@ func AddGroup(w http.ResponseWriter, r *http.Request) {
|
||||
var group *store.Group
|
||||
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
||||
|
||||
data := &store.GroupData{
|
||||
Data: subject.Data,
|
||||
}
|
||||
if res := tx.Save(data).Error; res != nil {
|
||||
return res
|
||||
}
|
||||
|
||||
group = &store.Group{
|
||||
GroupId: uuid.New().String(),
|
||||
AccountID: account.ID,
|
||||
GroupDataID: data.ID,
|
||||
Revision: 1,
|
||||
DataType: subject.DataType,
|
||||
Data: subject.Data,
|
||||
}
|
||||
if res := tx.Save(group).Error; res != nil {
|
||||
return res
|
||||
|
@ -19,7 +19,7 @@ func RemoveGroup(w http.ResponseWriter, r *http.Request) {
|
||||
groupId := params["groupId"]
|
||||
|
||||
var group store.Group
|
||||
if err := store.DB.Where("account_id = ? AND group_id = ?", account.ID, groupId).First(&group).Error; err != nil {
|
||||
if err := store.DB.Preload("GroupData").Where("account_id = ? AND group_id = ?", account.ID, groupId).First(&group).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
ErrResponse(w, http.StatusNotFound, err)
|
||||
} else {
|
||||
@ -29,6 +29,9 @@ func RemoveGroup(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
||||
if res := tx.Delete(&group.GroupData).Error; res != nil {
|
||||
return res
|
||||
}
|
||||
if res := tx.Delete(&group).Error; res != nil {
|
||||
return res
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ func UpdateGroup(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// load specified group
|
||||
var group store.Group
|
||||
if err := store.DB.Where("account_id = ? AND group_id = ?", account.ID, groupId).First(&group).Error; err != nil {
|
||||
if err := store.DB.Preload("GroupData").Where("account_id = ? AND group_id = ?", account.ID, groupId).First(&group).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
} else {
|
||||
@ -37,9 +37,12 @@ func UpdateGroup(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// update specified group
|
||||
group.DataType = subject.DataType
|
||||
group.Data = subject.Data
|
||||
group.GroupData.Data = subject.Data
|
||||
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
||||
if res := store.DB.Save(group).Error; res != nil {
|
||||
if res := store.DB.Save(&group.GroupData).Error; res != nil {
|
||||
return res
|
||||
}
|
||||
if res := store.DB.Save(&group).Error; res != nil {
|
||||
return res
|
||||
}
|
||||
if res := store.DB.Model(&account).Update("group_revision", account.GroupRevision + 1).Error; res != nil {
|
||||
|
@ -43,7 +43,7 @@ func getGroupModel(group *store.Group) *Group {
|
||||
GroupId: group.GroupId,
|
||||
Revision: group.Revision,
|
||||
DataType: group.DataType,
|
||||
Data: group.Data,
|
||||
Data: group.GroupData.Data,
|
||||
Created: group.Created,
|
||||
Updated: group.Updated,
|
||||
}
|
||||
|
@ -100,27 +100,39 @@ type Group struct {
|
||||
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
|
||||
GroupId string `gorm:"not null;index:group,unqiue"`
|
||||
AccountID uint `gorm:"not null;index:group,unique"`
|
||||
GroupDataID uint `gorm:"not null;index:groupdata"`
|
||||
LabelID uint `gorm:"not null;index:direct"`
|
||||
Revision int64 `gorm:"not null"`
|
||||
DataType string `gorm:"index"`
|
||||
Data string
|
||||
Created int64 `gorm:"autoCreateTime"`
|
||||
Updated int64 `gorm:"autoUpdateTime"`
|
||||
Cards []Card `gorm:"many2many:card_groups"`
|
||||
Account Account
|
||||
GroupData GroupData
|
||||
}
|
||||
|
||||
type GroupData struct {
|
||||
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
|
||||
Data string
|
||||
}
|
||||
|
||||
type Label struct {
|
||||
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
|
||||
LabelId string `gorm:"not null;index:label,unique"`
|
||||
AccountID uint `gorm:"not null;index:label,unique"`
|
||||
LabelDataID uint `gorm:"not null;index:labeldata"`
|
||||
Revision int64 `gorm:"not null"`
|
||||
DataType string `gorm:"index"`
|
||||
Data string
|
||||
Created int64 `gorm:"autoCreateTime"`
|
||||
Updated int64 `gorm:"autoUpdateTime"`
|
||||
Groups []Group `gorm:"many2many:label_groups;"`
|
||||
Account Account
|
||||
LabelData LabelData
|
||||
}
|
||||
|
||||
type LabelData struct {
|
||||
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
|
||||
Data string
|
||||
}
|
||||
|
||||
type Card struct {
|
||||
@ -178,7 +190,6 @@ type Article struct {
|
||||
|
||||
type ArticleData struct {
|
||||
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
|
||||
DataRevision int64 `gorm:"not null"`
|
||||
DataType string `gorm:"index"`
|
||||
Data string
|
||||
Status string `gorm:"not null;index"`
|
||||
|
Loading…
Reference in New Issue
Block a user