moved groups into slot model

This commit is contained in:
Roland Osborne 2022-02-02 23:48:17 -08:00
parent a736d0da05
commit 5932f177aa
13 changed files with 111 additions and 56 deletions

View File

@ -23,11 +23,17 @@ func AddArticle(w http.ResponseWriter, r *http.Request) {
return
}
var groups []store.Group
if err := store.DB.Where("group_id IN ?", articleAccess.Groups).Find(&groups).Error; err != nil {
var groupSlots []store.GroupSlot
if err := store.DB.Preload("Group").Where("group_slot_id IN ?", articleAccess.Groups).Find(&groupSlots).Error; err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
var groups []store.Group
for _, slot := range groupSlots {
if slot.Group != nil {
groups = append(groups, *slot.Group)
}
}
var labelSlots []store.LabelSlot
if err := store.DB.Preload("Label").Where("label_slot_id IN ?", articleAccess.Labels).Find(&labelSlots).Error; err != nil {

View File

@ -1,6 +1,7 @@
package databag
import (
"errors"
"net/http"
"gorm.io/gorm"
"github.com/google/uuid"
@ -21,7 +22,8 @@ func AddGroup(w http.ResponseWriter, r *http.Request) {
return
}
var group *store.Group
slot := &store.GroupSlot{}
group := &store.Group{}
err = store.DB.Transaction(func(tx *gorm.DB) error {
data := &store.GroupData{
@ -31,17 +33,26 @@ func AddGroup(w http.ResponseWriter, r *http.Request) {
return res
}
group = &store.Group{
GroupId: uuid.New().String(),
AccountID: account.ID,
GroupDataID: data.ID,
Revision: 1,
DataType: subject.DataType,
}
group.GroupDataID = data.ID
group.DataType = subject.DataType
if res := tx.Save(group).Error; res != nil {
return res
}
if res := tx.Where("account_id = ? AND group_id = 0", account.ID).First(slot).Error; res != nil {
if errors.Is(res, gorm.ErrRecordNotFound) {
slot.GroupSlotId = uuid.New().String()
slot.AccountID = account.ID
} else {
return res
}
}
slot.GroupID = group.ID
slot.Revision = account.GroupRevision + 1
slot.Group = group
if res := tx.Save(slot).Error; res != nil {
return res
}
if res := tx.Model(&account).Update("group_revision", account.GroupRevision + 1).Error; res != nil {
return res
}
@ -53,7 +64,7 @@ func AddGroup(w http.ResponseWriter, r *http.Request) {
}
SetStatus(account)
WriteResponse(w, getGroupModel(group))
WriteResponse(w, getGroupModel(slot))
}

View File

@ -99,7 +99,7 @@ func isShared(slot *store.ArticleSlot, guid string) bool {
}
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
return store.DB.Preload("Article.Groups.GroupSlot").Preload("Article.Labels.Groups.GroupSlot").Where("account_id = ? AND revision > ?", account.ID, revision).Find(articles).Error
}
func getContactArticles(card *store.Card, revision int64, articles *[]store.ArticleSlot) error {

View File

@ -14,7 +14,7 @@ func GetCards(w http.ResponseWriter, r *http.Request) {
}
var slots []store.CardSlot
if err := store.DB.Preload("Card").Where("account_id = ?", account.ID).Find(&slots).Error; err != nil {
if err := store.DB.Preload("Card.Groups.GroupSlot").Where("account_id = ?", account.ID).Find(&slots).Error; err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}

View File

@ -13,15 +13,15 @@ func GetGroups(w http.ResponseWriter, r *http.Request) {
return
}
var storeGroups []store.Group
if err := store.DB.Where("account_id = ?", account.ID).Find(&storeGroups).Error; err != nil {
var slots []store.GroupSlot
if err := store.DB.Where("account_id = ?", account.ID).Find(&slots).Error; err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
var groups []*Group
for _, group := range storeGroups {
groups = append(groups, getGroupModel(&group))
for _, slot := range slots {
groups = append(groups, getGroupModel(&slot))
}
WriteResponse(w, groups)
}

View File

@ -18,8 +18,8 @@ func RemoveGroup(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
groupId := params["groupId"]
var group store.Group
if err := store.DB.Preload("GroupData").Where("account_id = ? AND group_id = ?", account.ID, groupId).First(&group).Error; err != nil {
var slot store.GroupSlot
if err := store.DB.Preload("Group.GroupData").Where("account_id = ? AND group_slot_id = ?", account.ID, groupId).First(&slot).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusNotFound, err)
} else {
@ -29,10 +29,16 @@ 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 {
if res := tx.Delete(&slot.Group.GroupData).Error; res != nil {
return res
}
if res := tx.Delete(&group).Error; res != nil {
if res := tx.Delete(&slot.Group).Error; res != nil {
return res
}
slot.GroupID = 0
slot.Group = nil
slot.Revision = account.GroupRevision + 1
if res := tx.Save(&slot).Error; res != nil {
return res
}
if res := tx.Model(&account).Updates(store.Account{ViewRevision: account.ViewRevision + 1, GroupRevision: account.GroupRevision + 1}).Error; res != nil {

View File

@ -36,8 +36,8 @@ func SetCardGroup(w http.ResponseWriter, r *http.Request) {
}
// load referenced group
var group store.Group
if err := store.DB.Where("account_id = ? AND group_id = ?", account.ID, groupId).First(&group).Error; err != nil {
var groupSlot store.GroupSlot
if err := store.DB.Preload("Group").Where("account_id = ? AND group_slot_id = ?", account.ID, groupId).First(&groupSlot).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err)
} else {
@ -45,9 +45,13 @@ func SetCardGroup(w http.ResponseWriter, r *http.Request) {
}
return
}
if groupSlot.Group == nil {
ErrResponse(w, http.StatusNotFound, errors.New("referenced deleted group"))
return
}
// save and update revision
slot.Card.Groups = append(slot.Card.Groups, group)
slot.Card.Groups = append(slot.Card.Groups, *groupSlot.Group)
slot.Card.ViewRevision += 1
slot.Revision = account.CardRevision + 1
err = store.DB.Transaction(func(tx *gorm.DB) error {

View File

@ -25,8 +25,8 @@ func UpdateGroup(w http.ResponseWriter, r *http.Request) {
}
// load specified group
var group store.Group
if err := store.DB.Preload("GroupData").Where("account_id = ? AND group_id = ?", account.ID, groupId).First(&group).Error; err != nil {
var slot store.GroupSlot
if err := store.DB.Preload("Group.GroupData").Where("account_id = ? AND group_slot_id = ?", account.ID, groupId).First(&slot).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err)
} else {
@ -34,18 +34,26 @@ func UpdateGroup(w http.ResponseWriter, r *http.Request) {
}
return
}
if slot.Group == nil {
ErrResponse(w, http.StatusNotFound, errors.New("referenced deleted group"))
return
}
// update specified group
group.DataType = subject.DataType
group.GroupData.Data = subject.Data
slot.Revision = account.GroupRevision + 1
slot.Group.DataType = subject.DataType
slot.Group.GroupData.Data = subject.Data
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := store.DB.Save(&group.GroupData).Error; res != nil {
if res := tx.Save(&slot.Group.GroupData).Error; res != nil {
return res
}
if res := store.DB.Save(&group).Error; res != nil {
if res := tx.Save(&slot.Group).Error; res != nil {
return res
}
if res := store.DB.Model(&account).Update("group_revision", account.GroupRevision + 1).Error; res != nil {
if res := tx.Save(&slot).Error; res != nil {
return res
}
if res := tx.Model(&account).Update("group_revision", account.GroupRevision + 1).Error; res != nil {
return res
}
return nil
@ -56,7 +64,7 @@ func UpdateGroup(w http.ResponseWriter, r *http.Request) {
}
SetStatus(account)
WriteResponse(w, getGroupModel(&group))
WriteResponse(w, getGroupModel(&slot))
}

View File

@ -15,7 +15,7 @@ func getCardModel(slot *store.CardSlot) *Card {
// populate group id list
var groups []string;
for _, group := range slot.Card.Groups {
groups = append(groups, group.GroupId)
groups = append(groups, group.GroupSlot.GroupSlotId)
}
return &Card{
@ -43,14 +43,21 @@ func getCardModel(slot *store.CardSlot) *Card {
}
}
func getGroupModel(group *store.Group) *Group {
func getGroupModel(slot *store.GroupSlot) *Group {
if slot.Group == nil {
return &Group{
GroupId: slot.GroupSlotId,
}
}
return &Group{
GroupId: group.GroupId,
Revision: group.Revision,
DataType: group.DataType,
Data: group.GroupData.Data,
Created: group.Created,
Updated: group.Updated,
GroupId: slot.GroupSlotId,
GroupData: &GroupData {
DataType: slot.Group.DataType,
Data: slot.Group.GroupData.Data,
Created: slot.Group.Created,
Updated: slot.Group.Updated,
},
}
}
@ -65,7 +72,7 @@ func getArticleModel(slot *store.ArticleSlot, contact bool, shared bool) *Articl
var groups []string;
if !contact {
for _, group := range slot.Article.Groups {
groups = append(groups, group.GroupId)
groups = append(groups, group.GroupSlot.GroupSlotId)
}
}

View File

@ -143,7 +143,10 @@ type DialogueInsights struct {
type Group struct {
GroupId string `json:"groupId"`
Revision int64 `json:"revision"`
GroupData *GroupData `json:"groupData"`
}
type GroupData struct {
DataType string `json:"dataType"`
Data string `json:"data"`
Created int64 `json:"created"`
@ -164,16 +167,15 @@ type Insight struct {
type Label struct {
LabelId string `json:"labelId"`
LabelRevision int64 `json:"labelRevision"`
Type_ string `json:"type"`
Data string `json:"data"`
Created int64 `json:"created"`
Groups []string `json:"groups,omitempty"`
LabelData *LabelData `json:"labelData"`
}
type LabelsLabelIdBody struct {
Type_ string `json:"type"`
type LabelData struct {
DataType string `json:"type"`
Data string `json:"data"`
Created int64 `json:"created"`
Updated int64 `json:"updated"`
Groups []string `json:"groups,omitempty"`
}
type NodeConfig struct {

View File

@ -8,6 +8,8 @@ func AutoMigrate(db *gorm.DB) {
db.AutoMigrate(&App{});
db.AutoMigrate(&Account{});
db.AutoMigrate(&AccountToken{});
db.AutoMigrate(&GroupSlot{});
db.AutoMigrate(&GroupData{});
db.AutoMigrate(&Group{});
db.AutoMigrate(&LabelSlot{});
db.AutoMigrate(&LabelData{});
@ -99,19 +101,25 @@ type App struct {
Account Account `gorm:"references:Guid"`
}
type GroupSlot struct {
ID uint
GroupSlotId string `gorm:"not null;index:groupslot,unique"`
AccountID uint `gorm:"not null;index:groupslot,unique"`
Revision int64 `gorm:"not null"`
GroupID uint `gorm:"not null;default:0"`
Group *Group
Account Account
}
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"`
Created int64 `gorm:"autoCreateTime"`
Updated int64 `gorm:"autoUpdateTime"`
Cards []Card `gorm:"many2many:card_groups"`
Account Account
GroupData GroupData
GroupSlot GroupSlot
}
type GroupData struct {

View File

@ -17,6 +17,8 @@ func TestAddArticle(t *testing.T) {
set, err = AddTestGroup("addarticle")
assert.NoError(t, err)
PrintMsg(set)
// initial revision
rev = GetTestRevision(set.B.Revisions)

View File

@ -123,7 +123,7 @@ func TestGroupContact(t *testing.T) {
SetBearerAuth(r, a)
UpdateGroup(w, r)
assert.NoError(t, ReadResponse(w, &group))
assert.Equal(t, group.DataType, "imagroupEDIT")
assert.Equal(t, group.GroupData.DataType, "imagroupEDIT")
// receive revision
err = StatusRevision(wsA, &revision)
@ -172,5 +172,6 @@ func TestGroupContact(t *testing.T) {
SetBearerAuth(r, a)
GetGroups(w, r)
assert.NoError(t, ReadResponse(w, &groups))
assert.Equal(t, 0, len(groups))
assert.Equal(t, 1, len(groups))
assert.Nil(t, groups[0].GroupData)
}