mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 19:49:16 +00:00
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
|
package databag
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"net/http"
|
||
|
"github.com/gorilla/mux"
|
||
|
"gorm.io/gorm"
|
||
|
"databag/internal/store"
|
||
|
)
|
||
|
|
||
|
func GetChannelTopicAssets(w http.ResponseWriter, r *http.Request) {
|
||
|
|
||
|
// scan parameters
|
||
|
params := mux.Vars(r)
|
||
|
topicId := params["topicId"]
|
||
|
|
||
|
channelSlot, guid, err, code := getChannelSlot(r, true)
|
||
|
if err != nil {
|
||
|
ErrResponse(w, code, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 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
|
||
|
}
|
||
|
|
||
|
// only creator can list assets
|
||
|
if topicSlot.Topic.Guid != guid {
|
||
|
ErrResponse(w, http.StatusUnauthorized, errors.New("permission denied to asset list"))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 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)
|
||
|
}
|