mirror of
https://github.com/balzack/databag.git
synced 2025-02-14 12:39:17 +00:00
testing label list
This commit is contained in:
parent
d53399e306
commit
1c6358e3d7
@ -83,11 +83,6 @@ func GetArticleTags(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func GetLabels(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func RemoveArticleAsset(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
@ -72,7 +72,7 @@ func GetArticles(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
for _, article := range articles {
|
||||
if isShared(&article, card.Guid) {
|
||||
if isArticleShared(&article, card.Guid) {
|
||||
response = append(response, getArticleModel(&article, true, true))
|
||||
} else if revisionSet {
|
||||
response = append(response, getArticleModel(&article, true, false))
|
||||
@ -89,7 +89,7 @@ func GetArticles(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// better if this filtering was done in gorm or sql
|
||||
func isShared(slot *store.ArticleSlot, guid string) bool {
|
||||
func isArticleShared(slot *store.ArticleSlot, guid string) bool {
|
||||
if slot.Article == nil {
|
||||
return false
|
||||
}
|
||||
|
116
net/server/internal/api_getLabels.go
Normal file
116
net/server/internal/api_getLabels.go
Normal file
@ -0,0 +1,116 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"net/http"
|
||||
"databag/internal/store"
|
||||
)
|
||||
|
||||
func GetLabels(w http.ResponseWriter, r *http.Request) {
|
||||
var res error
|
||||
var viewRevision int64
|
||||
var labelRevision int64
|
||||
var revisionSet bool
|
||||
|
||||
view := r.FormValue("viewRevision")
|
||||
if view != "" {
|
||||
if viewRevision, res = strconv.ParseInt(view, 10, 64); res != nil {
|
||||
ErrResponse(w, http.StatusBadRequest, res)
|
||||
return
|
||||
}
|
||||
}
|
||||
label := r.FormValue("labelRevision")
|
||||
if label != "" {
|
||||
if labelRevision, res = strconv.ParseInt(label, 10, 64); res != nil {
|
||||
ErrResponse(w, http.StatusBadRequest, res)
|
||||
return
|
||||
}
|
||||
revisionSet = true
|
||||
}
|
||||
|
||||
tokenType := r.Header.Get("TokenType")
|
||||
var response []*Label
|
||||
if tokenType == APP_TOKENAPP {
|
||||
|
||||
account, code, err := BearerAppToken(r, false)
|
||||
if err != nil {
|
||||
ErrResponse(w, code, err)
|
||||
return
|
||||
}
|
||||
|
||||
var labels []store.LabelSlot
|
||||
if err := getAccountLabels(account, labelRevision, &labels); err != nil {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, label := range labels {
|
||||
response = append(response, getLabelModel(&label, false, true))
|
||||
}
|
||||
|
||||
w.Header().Set("Label-Revision", strconv.FormatInt(account.LabelRevision, 10))
|
||||
} else if tokenType == APP_TOKENCONTACT {
|
||||
|
||||
card, code, err := BearerContactToken(r, false)
|
||||
if err != nil {
|
||||
ErrResponse(w, code, err)
|
||||
return
|
||||
}
|
||||
|
||||
if viewRevision != card.ViewRevision + card.Account.ViewRevision {
|
||||
if revisionSet {
|
||||
ErrResponse(w, http.StatusGone, errors.New("label view unavailable"))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var labels []store.LabelSlot
|
||||
if err := getContactLabels(card, labelRevision, &labels); err != nil {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, label := range labels {
|
||||
if isLabelShared(&label, card.Guid) {
|
||||
response = append(response, getLabelModel(&label, true, true))
|
||||
} else if revisionSet {
|
||||
response = append(response, getLabelModel(&label, true, false))
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("View-Revision", strconv.FormatInt(card.ViewRevision + card.Account.ViewRevision, 10))
|
||||
w.Header().Set("Label-Revision", strconv.FormatInt(card.Account.LabelRevision, 10))
|
||||
} else {
|
||||
ErrResponse(w, http.StatusBadRequest, errors.New("invalid token type"))
|
||||
}
|
||||
|
||||
WriteResponse(w, response)
|
||||
}
|
||||
|
||||
// better if this filtering was done in gorm or sql
|
||||
func isLabelShared(slot *store.LabelSlot, guid string) bool {
|
||||
if slot.Label == nil {
|
||||
return false
|
||||
}
|
||||
for _, group := range slot.Label.Groups {
|
||||
for _, card := range group.Cards {
|
||||
if card.Guid == guid {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func getAccountLabels(account *store.Account, revision int64, labels *[]store.LabelSlot) error {
|
||||
return store.DB.Preload("Label.Groups.GroupSlot").Where("account_id = ? AND revision > ?", account.ID, revision).Find(labels).Error
|
||||
}
|
||||
|
||||
func getContactLabels(card *store.Card, revision int64, labels *[]store.LabelSlot) error {
|
||||
return store.DB.Preload("Label.Groups.Cards").Where("account_id = ? AND revision > ?", card.Account.ID, revision).Find(labels).Error
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -21,6 +21,8 @@ func TestAddArticle(t *testing.T) {
|
||||
var contentRevision int64
|
||||
var viewRevision int64
|
||||
var labelRevision int64
|
||||
var labels *[]Label
|
||||
var view int64
|
||||
|
||||
// setup testing group
|
||||
set, err = AddTestGroup("addarticle")
|
||||
@ -66,7 +68,6 @@ func TestAddArticle(t *testing.T) {
|
||||
SetBearerAuth(r, set.B.A.Token)
|
||||
GetArticles(w, r)
|
||||
resp := w.Result()
|
||||
var view int64
|
||||
view, err = strconv.ParseInt(resp.Header["View-Revision"][0], 10, 64)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, view, cards[0].CardData.NotifiedView)
|
||||
@ -141,4 +142,26 @@ func TestAddArticle(t *testing.T) {
|
||||
assert.NoError(t, SendEndpointTest(GetArticles, "GET", "/content/articles", nil, nil, APP_TOKENCONTACT, set.B.A.Token, articles))
|
||||
assert.Equal(t, 1, len(*articles))
|
||||
assert.NotEqual(t, article.ArticleId, (*articles)[0].ArticleId)
|
||||
|
||||
labels = &[]Label{}
|
||||
assert.NoError(t, SendEndpointTest(GetLabels, "GET", "/content/labels", nil, nil, APP_TOKENAPP, set.A.Token, labels))
|
||||
assert.Equal(t, 1, len(*labels))
|
||||
|
||||
labels = &[]Label{}
|
||||
assert.NoError(t, SendEndpointTest(GetLabels, "GET", "/content/labels", nil, nil, APP_TOKENCONTACT, set.B.A.Token, labels))
|
||||
assert.Equal(t, 0, len(*labels))
|
||||
|
||||
labels = &[]Label{}
|
||||
assert.NoError(t, SendEndpointTest(GetLabels, "GET", "/content/labels", nil, nil, APP_TOKENCONTACT, set.C.A.Token, labels))
|
||||
assert.Equal(t, 1, len(*labels))
|
||||
|
||||
r, w, ret = NewRequest("GET", "/content/labels", nil)
|
||||
assert.NoError(t, ret)
|
||||
r.Header.Add("TokenType", APP_TOKENCONTACT)
|
||||
SetBearerAuth(r, set.C.A.Token)
|
||||
GetLabels(w, r)
|
||||
resp = w.Result()
|
||||
view, err = strconv.ParseInt(resp.Header["View-Revision"][0], 10, 64)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, cards[0].CardData.NotifiedView, view)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user