From 29e84ebbb967643534a4ee80964b4381b2c24d5e Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Sun, 13 Feb 2022 15:19:41 -0800 Subject: [PATCH] testing add articles --- doc/api.oa3 | 6 +++ net/server/internal/api_addArticle.go | 56 ++++++++++++++++++++ net/server/internal/api_addGroup.go | 13 ++--- net/server/internal/api_attribute.go | 5 -- net/server/internal/api_getArticles.go | 36 +++++++++++-- net/server/internal/ucShareAttribute_test.go | 22 +++++++- 6 files changed, 117 insertions(+), 21 deletions(-) create mode 100644 net/server/internal/api_addArticle.go diff --git a/doc/api.oa3 b/doc/api.oa3 index 4da3e0c6..d50cfc48 100644 --- a/doc/api.oa3 +++ b/doc/api.oa3 @@ -1584,6 +1584,12 @@ paths: required: false schema: type: string + - name: types + in: query + description: return only articles of specified types + required: false + schema: + type: string responses: '200': description: successful operation diff --git a/net/server/internal/api_addArticle.go b/net/server/internal/api_addArticle.go new file mode 100644 index 00000000..17719a1e --- /dev/null +++ b/net/server/internal/api_addArticle.go @@ -0,0 +1,56 @@ +package databag + +import ( + "net/http" + "gorm.io/gorm" + "github.com/google/uuid" + "databag/internal/store" +) + +func AddArticle(w http.ResponseWriter, r *http.Request) { + + account, code, err := BearerAppToken(r, false) + if err != nil { + ErrResponse(w, code, err) + return + } + + var subject Subject + if err := ParseRequest(r, w, &subject); err != nil { + ErrResponse(w, http.StatusBadRequest, err) + return + } + + slot := &store.ArticleSlot{} + err = store.DB.Transaction(func(tx *gorm.DB) error { + + article := &store.Article{} + article.Data = subject.Data + article.DataType = subject.DataType + if res := tx.Save(article).Error; res != nil { + return res + } + + slot.ArticleSlotId = uuid.New().String() + slot.AccountID = account.ID + slot.ArticleID = article.ID + slot.Revision = account.ArticleRevision + 1 + slot.Article = article + if res := tx.Save(slot).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 + } + + SetStatus(account) + WriteResponse(w, getArticleModel(slot, true)) +} + + diff --git a/net/server/internal/api_addGroup.go b/net/server/internal/api_addGroup.go index adcf6aad..ad23e8c7 100644 --- a/net/server/internal/api_addGroup.go +++ b/net/server/internal/api_addGroup.go @@ -1,7 +1,6 @@ package databag import ( - "errors" "net/http" "gorm.io/gorm" "github.com/google/uuid" @@ -23,7 +22,6 @@ func AddGroup(w http.ResponseWriter, r *http.Request) { } slot := &store.GroupSlot{} - group := &store.Group{} err = store.DB.Transaction(func(tx *gorm.DB) error { data := &store.GroupData{ @@ -33,20 +31,15 @@ func AddGroup(w http.ResponseWriter, r *http.Request) { return res } + group := &store.Group{} 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.GroupSlotId = uuid.New().String() + slot.AccountID = account.ID slot.GroupID = group.ID slot.Revision = account.GroupRevision + 1 slot.Group = group diff --git a/net/server/internal/api_attribute.go b/net/server/internal/api_attribute.go index 4745ebd1..94b0cf97 100644 --- a/net/server/internal/api_attribute.go +++ b/net/server/internal/api_attribute.go @@ -13,11 +13,6 @@ import ( "net/http" ) -func AddArticle(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json; charset=UTF-8") - w.WriteHeader(http.StatusOK) -} - func ClearArticleGroup(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_getArticles.go b/net/server/internal/api_getArticles.go index 4059d824..81cee527 100644 --- a/net/server/internal/api_getArticles.go +++ b/net/server/internal/api_getArticles.go @@ -1,9 +1,11 @@ package databag import ( + "strings" "errors" "strconv" "net/http" + "encoding/json" "databag/internal/store" ) @@ -12,6 +14,8 @@ func GetArticles(w http.ResponseWriter, r *http.Request) { var articleRevision int64 var viewRevisionSet bool var viewRevision int64 + var typesSet bool + var types []string article := r.FormValue("articleRevision") if article != "" { @@ -33,6 +37,17 @@ func GetArticles(w http.ResponseWriter, r *http.Request) { } } + schemas := r.FormValue("types") + if schemas != "" { + var err error + typesSet = true + dec := json.NewDecoder(strings.NewReader(schemas)) + if dec.Decode(&types) != nil { + ErrResponse(w, http.StatusBadRequest, err) + return + } + } + var response []*Article tokenType := r.Header.Get("TokenType") if tokenType == APP_TOKENAPP { @@ -45,19 +60,21 @@ func GetArticles(w http.ResponseWriter, r *http.Request) { var slots []store.ArticleSlot if articleRevisionSet { - if err := store.DB.Preload("Article.ArticleData").Where("account_id = ? AND revision > ?", account.ID, articleRevision).Find(&slots).Error; err != nil { + if err := store.DB.Preload("Article").Where("account_id = ? AND revision > ?", account.ID, articleRevision).Find(&slots).Error; err != nil { ErrResponse(w, http.StatusInternalServerError, err) return } } else { - if err := store.DB.Preload("Article.ArticleData").Where("account_id = ? AND article_id != 0", account.ID).Find(&slots).Error; err != nil { + if err := store.DB.Preload("Article").Where("account_id = ? AND article_id != 0", account.ID).Find(&slots).Error; err != nil { ErrResponse(w, http.StatusInternalServerError, err) return } } for _, slot := range slots { - response = append(response, getArticleModel(&slot, true)) + if !typesSet || hasType(types, slot.Article) { + response = append(response, getArticleModel(&slot, true)) + } } w.Header().Set("Article-Revision", strconv.FormatInt(account.ArticleRevision, 10)) @@ -92,7 +109,9 @@ func GetArticles(w http.ResponseWriter, r *http.Request) { } for _, slot := range slots { - response = append(response, getArticleModel(&slot, false)) + if !typesSet || hasType(types, slot.Article) { + response = append(response, getArticleModel(&slot, false)) + } } w.Header().Set("Article-Revision", strconv.FormatInt(account.ArticleRevision, 10)) @@ -106,3 +125,12 @@ func GetArticles(w http.ResponseWriter, r *http.Request) { WriteResponse(w, response) } +func hasType(types []string, article *store.Article) bool { + for _, schema := range types { + if schema == article.DataType { + return true + } + } + return false +} + diff --git a/net/server/internal/ucShareAttribute_test.go b/net/server/internal/ucShareAttribute_test.go index 94ecb26a..b07beff6 100644 --- a/net/server/internal/ucShareAttribute_test.go +++ b/net/server/internal/ucShareAttribute_test.go @@ -6,15 +6,33 @@ import ( ) func TestShareAttribute(t *testing.T) { - var articles []Article + var articles *[]Article + var subject *Subject + var article *Article // setup testing group set, err := AddTestGroup("shareattribute") assert.NoError(t, err) + // add a new attribute + articles = &[]Article{} assert.NoError(t, ApiTestMsg(GetArticles, "GET", "/attribute/articles", - nil, nil, APP_TOKENAPP, set.A.Token, &articles, nil)) + 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", + nil, subject, APP_TOKENAPP, set.A.Token, article, nil)) + assert.Equal(t, "subjectdata", article.Data.Data) + assert.Equal(t, "subjectdatatype", article.Data.DataType) + articles = &[]Article{} + assert.NoError(t, ApiTestMsg(GetArticles, "GET", "/attribute/articles", + nil, nil, APP_TOKENAPP, set.A.Token, articles, nil)) + assert.Equal(t, 1, len(*articles)) + + + PrintMsg(article) PrintMsg(articles) }