adding topic update endpoints

This commit is contained in:
Roland Osborne 2022-02-18 22:33:13 -08:00
parent e3fa7170e4
commit 87f6d7caf0
8 changed files with 234 additions and 22 deletions

View File

@ -15,7 +15,7 @@ func AddChannelTopic(w http.ResponseWriter, r *http.Request) {
return
}
channelSlot, guid, err, code := getChannelSlot(r, false)
channelSlot, guid, err, code := getChannelSlot(r, true)
if err != nil {
ErrResponse(w, code, err)
return

View File

@ -48,11 +48,6 @@ func GetChannelTopicTags(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
func GetChannelTopics(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)
@ -73,16 +68,6 @@ func RemoveChannelTopicTag(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
func SetChannelConfirmed(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
func SetChannelTopicSubject(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}
func SetChannelTopicTagSubject(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)

View File

@ -0,0 +1,52 @@
package databag
import (
"strconv"
"net/http"
"databag/internal/store"
)
func GetChannelTopics(w http.ResponseWriter, r *http.Request) {
var revisionSet bool
var revision int64
channelSlot, _, err, code := getChannelSlot(r, false)
if err != nil {
ErrResponse(w, code, err)
return
}
rev := r.FormValue("revision")
if rev != "" {
revisionSet = true
if revision, err = strconv.ParseInt(rev, 10, 64); err != nil {
ErrResponse(w, http.StatusBadRequest, err)
return
}
}
var response []*Topic
if revisionSet {
var slots []store.TopicSlot
if err := store.DB.Preload("Topic").Where("channel_id = ? AND revision > ?", channelSlot.Channel.ID, revision).Find(&slots).Error; err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
for _, slot := range slots {
response = append(response, getTopicRevisionModel(&slot))
}
} else {
var slots []store.TopicSlot
if err := store.DB.Preload("Topic").Where("channel_id = ? AND topic_id != 0", channelSlot.Channel.ID).Find(&slots).Error; err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
for _, slot := range slots {
response = append(response, getTopicModel(&slot))
}
}
w.Header().Set("Topic-Revision", strconv.FormatInt(channelSlot.Revision, 10))
WriteResponse(w, response)
}

View File

@ -0,0 +1,84 @@
package databag
import (
"errors"
"net/http"
"gorm.io/gorm"
"github.com/gorilla/mux"
"databag/internal/store"
)
func SetChannelTopicConfirmed(w http.ResponseWriter, r *http.Request) {
// scan parameters
params := mux.Vars(r)
topicId := params["topicId"]
var status string
if err := ParseRequest(r, w, &status); err != nil {
ErrResponse(w, http.StatusBadRequest, err)
return
}
if !AppTopicStatus(status) {
ErrResponse(w, http.StatusBadRequest, errors.New("unknown status"))
return
}
channelSlot, _, err, code := getChannelSlot(r, true)
if err != nil {
ErrResponse(w, code, err)
return
}
act := &channelSlot.Account
// load topic
var topicSlot store.TopicSlot
if err = store.DB.Where("channel_id = ? AND topic_slot_id = ?", channelSlot.Channel.ID, topicId).First(&topicSlot).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
code = http.StatusNotFound
} else {
code = http.StatusInternalServerError
}
return
}
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Model(topicSlot.Topic).Update("status", status).Error; res != nil {
return res
}
if res := tx.Model(&topicSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil {
return res
}
if res := tx.Model(&channelSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil {
return res
}
if res := tx.Model(act).Update("channel_revision", act.ChannelRevision + 1).Error; res != nil {
return res
}
return nil
})
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
// determine affected contact list
cards := make(map[string]store.Card)
for _, card := range channelSlot.Channel.Cards {
cards[card.Guid] = card
}
for _, group := range channelSlot.Channel.Groups {
for _, card := range group.Cards {
cards[card.Guid] = card
}
}
SetStatus(act)
for _, card := range cards {
SetContactChannelNotification(act, &card)
}
WriteResponse(w, getTopicModel(&topicSlot))
}

View File

@ -0,0 +1,83 @@
package databag
import (
"errors"
"net/http"
"gorm.io/gorm"
"github.com/gorilla/mux"
"databag/internal/store"
)
func SetChannelTopicSubject(w http.ResponseWriter, r *http.Request) {
// scan parameters
params := mux.Vars(r)
topicId := params["topicId"]
var subject Subject
if err := ParseRequest(r, w, &subject); err != nil {
ErrResponse(w, http.StatusBadRequest, err)
return
}
channelSlot, _, err, code := getChannelSlot(r, true)
if err != nil {
ErrResponse(w, code, err)
return
}
act := &channelSlot.Account
// load topic
var topicSlot store.TopicSlot
if err = store.DB.Where("channel_id = ? AND topic_slot_id = ?", channelSlot.Channel.ID, topicId).First(&topicSlot).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
code = http.StatusNotFound
} else {
code = http.StatusInternalServerError
}
return
}
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Model(topicSlot.Topic).Update("data", subject.Data).Error; res != nil {
return res
}
if res := tx.Model(topicSlot.Topic).Update("data_type", subject.DataType).Error; res != nil {
return res
}
if res := tx.Model(&topicSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil {
return res
}
if res := tx.Model(&channelSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil {
return res
}
if res := tx.Model(act).Update("channel_revision", act.ChannelRevision + 1).Error; res != nil {
return res
}
return nil
})
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
// determine affected contact list
cards := make(map[string]store.Card)
for _, card := range channelSlot.Channel.Cards {
cards[card.Guid] = card
}
for _, group := range channelSlot.Channel.Groups {
for _, card := range group.Cards {
cards[card.Guid] = card
}
}
SetStatus(act)
for _, card := range cards {
SetContactChannelNotification(act, &card)
}
WriteResponse(w, getTopicModel(&topicSlot))
}

View File

@ -29,8 +29,6 @@ const APP_TOKENCONTACT = "contact"
const APP_NOTIFYBUFFER = 4096
const APP_TOPICUNCONFIRMED = "unconfirmed"
const APP_TOPICCONFIRMED = "confirmed"
const APP_TOPICINCOMPLETE = "incomplete"
const APP_TOPICERROR = "error"
func AppCardStatus(status string) bool {
if status == APP_CARDPENDING {
@ -50,3 +48,13 @@ func AppCardStatus(status string) bool {
}
return false
}
func AppTopicStatus(status string) bool {
if(status == APP_TOPICCONFIRMED) {
return true
}
if(status == APP_TOPICUNCONFIRMED) {
return true
}
return false
}

View File

@ -213,9 +213,9 @@ func getChannelModel(slot *store.ChannelSlot, showData bool, showList bool) *Cha
}
}
func getTopicRevisionModel(slot *store.TopicSlot, showData bool) *Topic {
func getTopicRevisionModel(slot *store.TopicSlot) *Topic {
if !showData || slot.Topic == nil {
if slot.Topic == nil {
return &Topic{
Id: slot.TopicSlotId,
Revision: slot.Revision,

View File

@ -650,10 +650,10 @@ var routes = Routes{
},
Route{
"SetChannelConfirmed",
"SetChannelTopicConfirmed",
strings.ToUpper("Put"),
"/content/channels/{channelId}/topics/{topicId}/confirmed",
SetChannelConfirmed,
SetChannelTopicConfirmed,
},
Route{