mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
testing add articles
This commit is contained in:
parent
4b408c356e
commit
29e84ebbb9
@ -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
|
||||
|
56
net/server/internal/api_addArticle.go
Normal file
56
net/server/internal/api_addArticle.go
Normal file
@ -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))
|
||||
}
|
||||
|
||||
|
@ -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.GroupID = group.ID
|
||||
slot.Revision = account.GroupRevision + 1
|
||||
slot.Group = group
|
||||
|
@ -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)
|
||||
|
@ -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,20 +60,22 @@ 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 {
|
||||
if !typesSet || hasType(types, slot.Article) {
|
||||
response = append(response, getArticleModel(&slot, true))
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Article-Revision", strconv.FormatInt(account.ArticleRevision, 10))
|
||||
|
||||
@ -92,8 +109,10 @@ func GetArticles(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
for _, slot := range slots {
|
||||
if !typesSet || hasType(types, slot.Article) {
|
||||
response = append(response, getArticleModel(&slot, false))
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Article-Revision", strconv.FormatInt(account.ArticleRevision, 10))
|
||||
w.Header().Set("View-Revision", strconv.FormatInt(card.ViewRevision, 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
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user