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
|
required: false
|
||||||
schema:
|
schema:
|
||||||
type: string
|
type: string
|
||||||
|
- name: types
|
||||||
|
in: query
|
||||||
|
description: return only articles of specified types
|
||||||
|
required: false
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
responses:
|
responses:
|
||||||
'200':
|
'200':
|
||||||
description: successful operation
|
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
|
package databag
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@ -23,7 +22,6 @@ func AddGroup(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
slot := &store.GroupSlot{}
|
slot := &store.GroupSlot{}
|
||||||
group := &store.Group{}
|
|
||||||
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
||||||
|
|
||||||
data := &store.GroupData{
|
data := &store.GroupData{
|
||||||
@ -33,20 +31,15 @@ func AddGroup(w http.ResponseWriter, r *http.Request) {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
group := &store.Group{}
|
||||||
group.GroupDataID = data.ID
|
group.GroupDataID = data.ID
|
||||||
group.DataType = subject.DataType
|
group.DataType = subject.DataType
|
||||||
if res := tx.Save(group).Error; res != nil {
|
if res := tx.Save(group).Error; res != nil {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
if res := tx.Where("account_id = ? AND group_id = 0", account.ID).First(slot).Error; res != nil {
|
slot.GroupSlotId = uuid.New().String()
|
||||||
if errors.Is(res, gorm.ErrRecordNotFound) {
|
slot.AccountID = account.ID
|
||||||
slot.GroupSlotId = uuid.New().String()
|
|
||||||
slot.AccountID = account.ID
|
|
||||||
} else {
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
}
|
|
||||||
slot.GroupID = group.ID
|
slot.GroupID = group.ID
|
||||||
slot.Revision = account.GroupRevision + 1
|
slot.Revision = account.GroupRevision + 1
|
||||||
slot.Group = group
|
slot.Group = group
|
||||||
|
@ -13,11 +13,6 @@ import (
|
|||||||
"net/http"
|
"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) {
|
func ClearArticleGroup(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package databag
|
package databag
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"errors"
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"encoding/json"
|
||||||
"databag/internal/store"
|
"databag/internal/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -12,6 +14,8 @@ func GetArticles(w http.ResponseWriter, r *http.Request) {
|
|||||||
var articleRevision int64
|
var articleRevision int64
|
||||||
var viewRevisionSet bool
|
var viewRevisionSet bool
|
||||||
var viewRevision int64
|
var viewRevision int64
|
||||||
|
var typesSet bool
|
||||||
|
var types []string
|
||||||
|
|
||||||
article := r.FormValue("articleRevision")
|
article := r.FormValue("articleRevision")
|
||||||
if article != "" {
|
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
|
var response []*Article
|
||||||
tokenType := r.Header.Get("TokenType")
|
tokenType := r.Header.Get("TokenType")
|
||||||
if tokenType == APP_TOKENAPP {
|
if tokenType == APP_TOKENAPP {
|
||||||
@ -45,19 +60,21 @@ func GetArticles(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
var slots []store.ArticleSlot
|
var slots []store.ArticleSlot
|
||||||
if articleRevisionSet {
|
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)
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} 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)
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, slot := range slots {
|
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))
|
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 {
|
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))
|
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)
|
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) {
|
func TestShareAttribute(t *testing.T) {
|
||||||
var articles []Article
|
var articles *[]Article
|
||||||
|
var subject *Subject
|
||||||
|
var article *Article
|
||||||
|
|
||||||
// setup testing group
|
// setup testing group
|
||||||
set, err := AddTestGroup("shareattribute")
|
set, err := AddTestGroup("shareattribute")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// add a new attribute
|
||||||
|
articles = &[]Article{}
|
||||||
assert.NoError(t, ApiTestMsg(GetArticles, "GET", "/attribute/articles",
|
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)
|
PrintMsg(articles)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user