2022-03-02 21:19:16 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
2022-07-22 19:28:14 +00:00
|
|
|
"databag/internal/store"
|
|
|
|
"errors"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"net/http"
|
2022-03-02 21:19:16 +00:00
|
|
|
)
|
|
|
|
|
2022-07-29 07:21:32 +00:00
|
|
|
//GetChannelTopicAssets retries list of assets associated with topic
|
2022-03-02 21:19:16 +00:00
|
|
|
func GetChannelTopicAssets(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
// scan parameters
|
|
|
|
params := mux.Vars(r)
|
|
|
|
topicID := params["topicID"]
|
2022-03-02 21:19:16 +00:00
|
|
|
|
2022-07-27 05:43:39 +00:00
|
|
|
channelSlot, guid, code, err := getChannelSlot(r, true)
|
2022-07-22 19:28:14 +00:00
|
|
|
if err != nil {
|
|
|
|
ErrResponse(w, code, err)
|
|
|
|
return
|
|
|
|
}
|
2022-03-02 21:19:16 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
// load topic
|
|
|
|
var topicSlot store.TopicSlot
|
|
|
|
if err = store.DB.Preload("Topic.Assets").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
|
|
|
|
}
|
2022-03-02 21:19:16 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
// only creator can list assets
|
|
|
|
if topicSlot.Topic.GUID != guid {
|
|
|
|
ErrResponse(w, http.StatusUnauthorized, errors.New("permission denied to asset list"))
|
|
|
|
return
|
|
|
|
}
|
2022-03-02 21:19:16 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
// return list of assets
|
|
|
|
assets := []Asset{}
|
|
|
|
for _, asset := range topicSlot.Topic.Assets {
|
|
|
|
assets = append(assets, Asset{AssetID: asset.AssetID, Status: asset.Status})
|
|
|
|
}
|
|
|
|
WriteResponse(w, &assets)
|
2022-03-02 21:19:16 +00:00
|
|
|
}
|