mirror of
https://github.com/balzack/databag.git
synced 2025-03-13 00:50:03 +00:00
testing get topic tags
This commit is contained in:
parent
11483178a1
commit
e312a42f7b
@ -51,6 +51,7 @@ func AddChannelTopicTag(w http.ResponseWriter, r *http.Request) {
|
||||
tagSlot.TagSlotId = uuid.New().String()
|
||||
tagSlot.AccountID = act.ID
|
||||
tagSlot.Revision = act.ChannelRevision + 1
|
||||
tagSlot.TopicID = topicSlot.Topic.ID
|
||||
if res := tx.Save(tagSlot).Error; res != nil {
|
||||
return res
|
||||
}
|
||||
|
@ -13,11 +13,6 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetChannelTopicTags(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func RemoveChannel(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
76
net/server/internal/api_getChannelTopicTags.go
Normal file
76
net/server/internal/api_getChannelTopicTags.go
Normal file
@ -0,0 +1,76 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"net/http"
|
||||
"github.com/gorilla/mux"
|
||||
"gorm.io/gorm"
|
||||
"databag/internal/store"
|
||||
)
|
||||
|
||||
func GetChannelTopicTags(w http.ResponseWriter, r *http.Request) {
|
||||
var revisionSet bool
|
||||
var revision int64
|
||||
|
||||
// load channel slot
|
||||
channelSlot, _, err, code := getChannelSlot(r, false)
|
||||
if err != nil {
|
||||
ErrResponse(w, code, err)
|
||||
return
|
||||
}
|
||||
|
||||
// scan parameters
|
||||
params := mux.Vars(r)
|
||||
topicId := params["topicId"]
|
||||
rev := r.FormValue("revision")
|
||||
if rev != "" {
|
||||
revisionSet = true
|
||||
if revision, err = strconv.ParseInt(rev, 10, 64); err != nil {
|
||||
ErrResponse(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// load topic
|
||||
var topicSlot store.TopicSlot
|
||||
if err = store.DB.Preload("Topic").Where("channel_id = ? AND topic_slot_id = ?", channelSlot.Channel.ID, topicId).First(&topicSlot).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
ErrResponse(w, http.StatusNotFound, err)
|
||||
} else {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if topicSlot.Topic == nil {
|
||||
ErrResponse(w, http.StatusNotFound, errors.New("referenced empty topic"))
|
||||
return
|
||||
}
|
||||
|
||||
var response []*Tag
|
||||
if revisionSet {
|
||||
var slots []store.TagSlot
|
||||
if err := store.DB.Preload("Tag").Where("topic_id = ? AND revision > ?", topicSlot.Topic.ID, revision).Find(&slots).Error; err != nil {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
for _, slot := range slots {
|
||||
response = append(response, getTagModel(&slot))
|
||||
}
|
||||
} else {
|
||||
var slots []store.TagSlot
|
||||
if err := store.DB.Preload("Tag").Where("topic_id = ?", topicSlot.Topic.ID).Find(&slots).Error; err != nil {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
for _, slot := range slots {
|
||||
if slot.Tag != nil {
|
||||
response = append(response, getTagModel(&slot))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Tag-Revision", strconv.FormatInt(topicSlot.Revision, 10))
|
||||
WriteResponse(w, response)
|
||||
}
|
||||
|
@ -261,9 +261,11 @@ type TagSlot struct {
|
||||
ID uint
|
||||
TagSlotId string `gorm:"not null;index:tagslot,unique"`
|
||||
AccountID uint `gorm:"not null;index:tagslot,unique"`
|
||||
TopicID uint `gorm:"not null;index:tagtopic"`
|
||||
Revision int64 `gorm:"not null"`
|
||||
Tag *Tag
|
||||
Account Account
|
||||
Topic *Topic
|
||||
}
|
||||
|
||||
type Tag struct {
|
||||
|
@ -104,9 +104,15 @@ func TestTopicShare(t *testing.T) {
|
||||
// add a tag to topic
|
||||
tag := Tag{}
|
||||
subject = &Subject{ DataType: "tagdatatype", Data: "subjectfromA" }
|
||||
assert.NoError(t, ApiTestMsg(AddChannelTopicTag, "POST", "/content/channels/{channelId}/topcis/{topicId}",
|
||||
assert.NoError(t, ApiTestMsg(AddChannelTopicTag, "POST", "/content/channels/{channelId}/topics/{topicId}",
|
||||
¶ms, subject, APP_TOKENAPP, set.A.Token, tag, nil))
|
||||
|
||||
// get tags for topic
|
||||
tags := []Tag{}
|
||||
assert.NoError(t, ApiTestMsg(GetChannelTopicTags, "GET", "/content/channels/{channelId}/topics/{topicId}",
|
||||
¶ms, nil, APP_TOKENCONTACT, set.C.A.Token, &tags, nil))
|
||||
assert.Equal(t, 1, len(tags))
|
||||
|
||||
// get list of assets
|
||||
assets = []Asset{}
|
||||
assert.NoError(t, ApiTestMsg(GetChannelTopicAssets, "GET", "/content/channels/{channelId}/topics/{topicId}",
|
||||
|
Loading…
Reference in New Issue
Block a user