From 579052d7bc8721c5c202a8428e22520f4a780bf8 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Sun, 13 Feb 2022 21:36:29 -0800 Subject: [PATCH] testing article update --- doc/api.oa3 | 4 +- net/server/internal/api_attribute.go | 4 - net/server/internal/api_setArticleSubject.go | 81 +++++++++++++++++++ ..._updateGroup.go => api_setGroupSubject.go} | 2 +- net/server/internal/appValues.go | 9 +-- net/server/internal/models.go | 2 - net/server/internal/routers.go | 6 +- net/server/internal/store/schema.go | 1 - net/server/internal/ucContactSync_test.go | 2 +- net/server/internal/ucShareAttribute_test.go | 10 ++- 10 files changed, 100 insertions(+), 21 deletions(-) create mode 100644 net/server/internal/api_setArticleSubject.go rename net/server/internal/{api_updateGroup.go => api_setGroupSubject.go} (96%) diff --git a/doc/api.oa3 b/doc/api.oa3 index d50cfc48..881c017b 100644 --- a/doc/api.oa3 +++ b/doc/api.oa3 @@ -878,12 +878,12 @@ paths: schema: $ref: '#/components/schemas/Subject' - /alias/groups/{groupId}: + /alias/groups/{groupId}/subject: put: tags: - alias description: Update group description for sharing. Access granted to app tokens of account holder. - operationId: update-group + operationId: set-group-subject security: - bearerAuth: [] parameters: diff --git a/net/server/internal/api_attribute.go b/net/server/internal/api_attribute.go index 94b0cf97..caf3aad6 100644 --- a/net/server/internal/api_attribute.go +++ b/net/server/internal/api_attribute.go @@ -33,8 +33,4 @@ func SetArticleGroup(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } -func SetArticleSubject(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) -} diff --git a/net/server/internal/api_setArticleSubject.go b/net/server/internal/api_setArticleSubject.go new file mode 100644 index 00000000..52de69dd --- /dev/null +++ b/net/server/internal/api_setArticleSubject.go @@ -0,0 +1,81 @@ +package databag + +import ( + "errors" + "net/http" + "gorm.io/gorm" + "github.com/gorilla/mux" + "databag/internal/store" +) + +func SetArticleSubject(w http.ResponseWriter, r *http.Request) { + + account, code, err := BearerAppToken(r, false); + if err != nil { + ErrResponse(w, code, err) + return + } + + // scan parameters + params := mux.Vars(r) + articleId := params["articleId"] + + var subject Subject + if err := ParseRequest(r, w, &subject); err != nil { + ErrResponse(w, http.StatusBadRequest, err) + return + } + + // load referenced article + var slot store.ArticleSlot + if err := store.DB.Preload("Article.Groups.Cards").Where("account_id = ? AND article_slot_id = ?", account.ID, articleId).First(&slot).Error; err != nil { + if !errors.Is(err, gorm.ErrRecordNotFound) { + ErrResponse(w, http.StatusInternalServerError, err) + } else { + ErrResponse(w, http.StatusNotFound, err) + } + return + } + if slot.Article == nil { + ErrResponse(w, http.StatusNotFound, errors.New("article has been deleted")) + return + } + + // determine affected contact list + cards := make(map[string]*store.Card) + for _, group := range slot.Article.Groups { + for _, card := range group.Cards { + cards[card.Guid] = &card + } + } + + // save and update contact revision + err = store.DB.Transaction(func(tx *gorm.DB) error { + if res := tx.Model(&slot.Article).Update("data", subject.Data).Error; res != nil { + return res + } + if res := tx.Model(&slot.Article).Update("data_type", subject.DataType).Error; res != nil { + return res + } + if res := tx.Model(&slot).Update("revision", account.ArticleRevision + 1).Error; res != nil { + return res + } + if res := tx.Model(&account).Update("article_revision", account.ArticleRevision + 1).Error; res != nil { + return res + } + return nil + }) + if err != nil { + ErrResponse(w, http.StatusInternalServerError, err) + return + } + + // notify contacts of content change + SetStatus(account) + for _, card := range cards { + SetContactArticleNotification(account, card) + } + + WriteResponse(w, getArticleModel(&slot, true)); +} + diff --git a/net/server/internal/api_updateGroup.go b/net/server/internal/api_setGroupSubject.go similarity index 96% rename from net/server/internal/api_updateGroup.go rename to net/server/internal/api_setGroupSubject.go index fea8b73c..22a3e8ed 100644 --- a/net/server/internal/api_updateGroup.go +++ b/net/server/internal/api_setGroupSubject.go @@ -8,7 +8,7 @@ import ( "databag/internal/store" ) -func UpdateGroup(w http.ResponseWriter, r *http.Request) { +func SetGroupSubject(w http.ResponseWriter, r *http.Request) { account, code, err := BearerAppToken(r, false); if err != nil { diff --git a/net/server/internal/appValues.go b/net/server/internal/appValues.go index 102181ee..120a7adc 100644 --- a/net/server/internal/appValues.go +++ b/net/server/internal/appValues.go @@ -27,11 +27,10 @@ const APP_NOTIFYVIEW = "view" const APP_TOKENAPP = "app" const APP_TOKENCONTACT = "contact" const APP_NOTIFYBUFFER = 4096 -const APP_ARTICLEUNCONFIRMED = "unconfirmed" -const APP_ARTICLECONFIRMED = "confirmed" -const APP_ARTICLEINCOMPLETE = "incomplete" -const APP_ARTICLEERROR = "error" -const APP_ARTICLEBLOCKSIZE = 128 +const APP_TOPICUNCONFIRMED = "unconfirmed" +const APP_TOPICCONFIRMED = "confirmed" +const APP_TOPICINCOMPLETE = "incomplete" +const APP_TOPICERROR = "error" func AppCardStatus(status string) bool { if status == APP_CARDPENDING { diff --git a/net/server/internal/models.go b/net/server/internal/models.go index 8ec9c11d..64f73314 100644 --- a/net/server/internal/models.go +++ b/net/server/internal/models.go @@ -70,8 +70,6 @@ type ArticleData struct { Updated int64 `json:"updated"` - Status string `json:"status"` - Groups *ArticleGroups `json:"groups,omitempty"` } diff --git a/net/server/internal/routers.go b/net/server/internal/routers.go index 54d9fee5..fba08b96 100644 --- a/net/server/internal/routers.go +++ b/net/server/internal/routers.go @@ -286,10 +286,10 @@ var routes = Routes{ }, Route{ - "UpdateGroup", + "SetGroupSubject", strings.ToUpper("Put"), - "/alias/groups/{groupId}", - UpdateGroup, + "/alias/groups/{groupId}/subject", + SetGroupSubject, }, Route{ diff --git a/net/server/internal/store/schema.go b/net/server/internal/store/schema.go index b9caa06f..f888b61d 100644 --- a/net/server/internal/store/schema.go +++ b/net/server/internal/store/schema.go @@ -176,7 +176,6 @@ type Article struct { ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` DataType string `gorm:"index"` Data string - Status string `gorm:"not null;index"` Created int64 `gorm:"autoCreateTime"` Updated int64 `gorm:"autoUpdateTime"` Groups []Group `gorm:"many2many:article_groups;"` diff --git a/net/server/internal/ucContactSync_test.go b/net/server/internal/ucContactSync_test.go index 5fa47a70..7333938c 100644 --- a/net/server/internal/ucContactSync_test.go +++ b/net/server/internal/ucContactSync_test.go @@ -148,7 +148,7 @@ func TestContactSync(t *testing.T) { // update and delete group param["groupId"] = set.D.C.GroupId subject := &Subject{ DataType: "contactsynctype", Data: "contactsyncdata" } - assert.NoError(t, ApiTestMsg(UpdateGroup, "PUT", "/alias/groups/{groupId}", + assert.NoError(t, ApiTestMsg(SetGroupSubject, "PUT", "/alias/groups/{groupId}", ¶m, subject, APP_TOKENAPP, set.D.Token, nil, nil)) groups = &[]Group{} assert.NoError(t, ApiTestMsg(GetGroups, "GET", "/alias/groups", diff --git a/net/server/internal/ucShareAttribute_test.go b/net/server/internal/ucShareAttribute_test.go index b07beff6..4860b742 100644 --- a/net/server/internal/ucShareAttribute_test.go +++ b/net/server/internal/ucShareAttribute_test.go @@ -9,6 +9,7 @@ func TestShareAttribute(t *testing.T) { var articles *[]Article var subject *Subject var article *Article + param := map[string]string{} // setup testing group set, err := AddTestGroup("shareattribute") @@ -19,7 +20,6 @@ func TestShareAttribute(t *testing.T) { assert.NoError(t, ApiTestMsg(GetArticles, "GET", "/attribute/articles", nil, nil, APP_TOKENAPP, set.A.Token, articles, nil)) assert.Equal(t, 0, len(*articles)) - article = &Article{} subject = &Subject{ Data: "subjectdata", DataType: "subjectdatatype" } assert.NoError(t, ApiTestMsg(AddArticle, "POST", "/attributes/articles", @@ -31,8 +31,14 @@ func TestShareAttribute(t *testing.T) { nil, nil, APP_TOKENAPP, set.A.Token, articles, nil)) assert.Equal(t, 1, len(*articles)) + // update attribute + data := "{ \"nested\" : { \"image\" : \"iVBORw0KGgoAAAANSUhEUgAAAaQAAAGkCAIAAADxLsZiAAAFzElEQVR4nOzWUY3jMBhG0e0qSEqoaIqiaEIoGAxh3gZAldid3nMI+JOiXP3bGOMfwLf7v3oAwAxiBySIHZAgdkCC2AEJYgckiB2QIHZAgtgBCWIHJIgdkCB2QILYAQli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eyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBhWz2Az/Laj9UT4BIuOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgITbGGP1BoDLueyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7IAEsQMSxA5IEDsgQeyABLEDEsQOSBA7IEHsgASxAxLEDkgQOyBB7ICEnwAAAP//DQ4epwV6rzkAAAAASUVORK5CYII=\" } }" + subject = &Subject{ Data: data, DataType: "nestedimage" } + param["articleId"] = article.Id + article = &Article{} + assert.NoError(t, ApiTestMsg(SetArticleSubject, "PUT", "/attribute/articles/{articleId}/subject", + ¶m, subject, APP_TOKENAPP, set.A.Token, article, nil)) PrintMsg(article) - PrintMsg(articles) }