cleaned up get routes

This commit is contained in:
James Tombleson 2023-01-30 22:33:53 -08:00
parent ada453e08a
commit 6629a55157
6 changed files with 111 additions and 204 deletions

View File

@ -3,6 +3,7 @@ package routes
import (
"encoding/json"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
@ -18,6 +19,7 @@ func (s *Server) GetArticleRouter() http.Handler {
r.Get("/details", s.getArticleDetails)
})
r.Get("/by/sourceid", s.GetArticlesBySourceId)
r.Get("/by/page", s.ListArticlesByPage)
return r
}
@ -27,6 +29,16 @@ type ArticlesListResults struct {
Payload []models.ArticleDto `json:"payload"`
}
type ArticleGetResults struct {
ApiStatusModel
Payload models.ArticleDto `json:"payload"`
}
type ArticleDetailsResult struct {
ApiStatusModel
Payload models.ArticleDetailsDto `json:"payload"`
}
// ListArticles
// @Summary Lists the top 50 records
// @Produce application/json
@ -41,27 +53,46 @@ func (s *Server) listArticles(w http.ResponseWriter, r *http.Request) {
},
}
w.Header().Set(HeaderContentType, ApplicationJson)
res, err := s.dto.ListArticles(r.Context(), 50)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
p.Payload = res
s.WriteJson(w, p)
}
// ListArticlesByPage
// @Summary List 50 items based on the requested page
// @Produce application/json
// @Param page query string true "page number"
// @Tags Articles
// @Router /articles/by/page [get]
// @Success 200 {object} ArticlesListResults "OK"
func (s *Server) ListArticlesByPage(w http.ResponseWriter, r *http.Request) {
p := ArticlesListResults {
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
query := r.URL.Query()
page, err := strconv.Atoi(query["page"][0])
if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
}
res, err := s.dto.ListArticlesByPage(r.Context(), int32(page), 25)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
}
type ArticleGetResults struct {
ApiStatusModel
Payload models.ArticleDto `json:"payload"`
s.WriteJson(w, p)
}
// GetArticle
@ -79,8 +110,6 @@ func (s *Server) getArticle(w http.ResponseWriter, r *http.Request) {
},
}
w.Header().Set(HeaderContentType, ApplicationJson)
id := chi.URLParam(r, "ID")
uuid, err := uuid.Parse(id)
if err != nil {
@ -96,18 +125,7 @@ func (s *Server) getArticle(w http.ResponseWriter, r *http.Request) {
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
}
type ArticleDetailsResult struct {
ApiStatusModel
Payload models.ArticleDetailsDto `json:"payload"`
s.WriteJson(w, p)
}
// GetArticleDetails
@ -125,8 +143,6 @@ func (s *Server) getArticleDetails(w http.ResponseWriter, r *http.Request) {
},
}
w.Header().Set(HeaderContentType, ApplicationJson)
id := chi.URLParam(r, "ID")
uuid, err := uuid.Parse(id)
if err != nil {
@ -142,18 +158,7 @@ func (s *Server) getArticleDetails(w http.ResponseWriter, r *http.Request) {
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
}
type ArticlesBySourceIDResults struct {
ApiStatusModel
Payload []models.ArticleDto `json:"payload"`
s.WriteJson(w, p)
}
// TODO add page support
@ -165,7 +170,12 @@ type ArticlesBySourceIDResults struct {
// @Router /articles/by/sourceid [get]
// @Success 200 {object} ArticlesListResults "OK"
func (s *Server) GetArticlesBySourceId(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
p := ArticlesListResults{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
r.URL.Query()
query := r.URL.Query()
@ -182,12 +192,7 @@ func (s *Server) GetArticlesBySourceId(w http.ResponseWriter, r *http.Request) {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
p.Payload = res
bres, err := json.Marshal(res)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
s.WriteJson(w, p)
}

View File

@ -12,6 +12,16 @@ import (
"github.com/jtom38/newsbot/collector/domain/models"
)
type ListDiscordWebhooks struct {
ApiStatusModel
Payload []models.DiscordWebHooksDto `json:"payload"`
}
type GetDiscordWebhook struct {
ApiStatusModel
Payload models.DiscordWebHooksDto `json:"payload"`
}
func (s Server) DiscordWebHookRouter() http.Handler {
r := chi.NewRouter()
@ -28,11 +38,6 @@ func (s Server) DiscordWebHookRouter() http.Handler {
return r
}
type ListDiscordWebhooks struct {
ApiStatusModel
Payload []models.DiscordWebHooksDto `json:"payload"`
}
// ListDiscordWebhooks
// @Summary Returns the top 100 entries from the queue to be processed.
// @Produce application/json
@ -46,27 +51,13 @@ func (s *Server) ListDiscordWebHooks(w http.ResponseWriter, r *http.Request) {
},
}
w.Header().Set(HeaderContentType, ApplicationJson)
res, err := s.dto.ListDiscordWebHooks(r.Context(), 50)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
}
type GetDiscordWebhook struct {
ApiStatusModel
Payload models.DiscordWebHooksDto `json:"payload"`
s.WriteJson(w, p)
}
// GetDiscordWebHook
@ -84,8 +75,6 @@ func (s *Server) GetDiscordWebHooksById(w http.ResponseWriter, r *http.Request)
},
}
w.Header().Set(HeaderContentType, ApplicationJson)
_id := chi.URLParam(r, "ID")
if _id == "" {
s.WriteError(w, "id is missing", http.StatusBadRequest)
@ -104,14 +93,7 @@ func (s *Server) GetDiscordWebHooksById(w http.ResponseWriter, r *http.Request)
return
}
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
s.WriteError(w, "unable to convert to json", http.StatusBadRequest)
return
}
w.Write(bres)
s.WriteJson(w, p)
}
// GetDiscordWebHookByServerAndChannel
@ -130,8 +112,6 @@ func (s *Server) GetDiscordWebHooksByServerAndChannel(w http.ResponseWriter, r *
},
}
w.Header().Set(HeaderContentType, ApplicationJson)
query := r.URL.Query()
_server := query["server"][0]
if _server == "" {
@ -152,14 +132,7 @@ func (s *Server) GetDiscordWebHooksByServerAndChannel(w http.ResponseWriter, r *
}
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
s.WriteError(w, "unable to convert to json", http.StatusInternalServerError)
return
}
w.Write(bres)
s.WriteJson(w, p)
}
// NewDiscordWebHook

View File

@ -8,6 +8,11 @@ import (
"github.com/jtom38/newsbot/collector/domain/models"
)
type ListDiscordWebHooksQueueResults struct {
ApiStatusModel
Payload []models.DiscordQueueDetailsDto `json:"payload"`
}
func (s *Server) GetQueueRouter() http.Handler {
r := chi.NewRouter()
@ -16,11 +21,6 @@ func (s *Server) GetQueueRouter() http.Handler {
return r
}
type ListDiscordWebHooksQueueResults struct {
ApiStatusModel
Payload []models.DiscordQueueDetailsDto `json:"payload"`
}
// GetDiscordQueue
// @Summary Returns the top 100 entries from the queue to be processed.
// @Produce application/json
@ -28,8 +28,6 @@ type ListDiscordWebHooksQueueResults struct {
// @Router /queue/discord/webhooks [get]
// @Success 200 {object} ListDiscordWebHooksQueueResults "ok"
func (s *Server) ListDiscordWebhookQueue(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
p := ListDiscordWebHooksQueueResults{
ApiStatusModel: ApiStatusModel{
Message: "OK",
@ -45,13 +43,5 @@ func (s *Server) ListDiscordWebhookQueue(w http.ResponseWriter, r *http.Request)
}
p.Payload = res
// convert to json
b, err := json.Marshal(p)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(b)
s.WriteJson(w, p)
}

View File

@ -112,3 +112,15 @@ func (s *Server) WriteError(w http.ResponseWriter, errMessage string, HttpStatus
w.Write(b)
}
func (s *Server) WriteJson(w http.ResponseWriter, model interface{}) {
w.Header().Set(HeaderContentType, ApplicationJson)
bres, err := json.Marshal(model)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
}

View File

@ -61,8 +61,7 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
res, err := s.Db.ListSources(*s.ctx, int32(topInt))
*/
w.Header().Set(HeaderContentType, ApplicationJson)
result := ListSources{
p := ListSources{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
Message: "OK",
@ -76,15 +75,8 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
return
}
result.Payload = items
bResult, err := json.Marshal(result)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bResult)
p.Payload = items
s.WriteJson(w, p)
}
// ListSourcesBySource
@ -106,9 +98,8 @@ func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
}
res, err := s.Db.ListSources(*s.ctx, int32(topInt))
*/
w.Header().Set(HeaderContentType, ApplicationJson)
result := ListSources{
p := ListSources{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
Message: "OK",
@ -116,24 +107,16 @@ func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
}
query := r.URL.Query()
_source := query["source"][0]
// Shows the list by Sources.source
res, err := s.dto.ListSourcesBySource(r.Context(), _source)
res, err := s.dto.ListSourcesBySource(r.Context(), query["source"][0])
if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
}
result.Payload = res
bResult, err := json.Marshal(result)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bResult)
p.Payload = res
s.WriteJson(w, p)
}
// GetSource
@ -154,10 +137,7 @@ func (s *Server) getSources(w http.ResponseWriter, r *http.Request) {
},
}
w.Header().Set(HeaderContentType, ApplicationJson)
id := chi.URLParam(r, "ID")
uuid, err := uuid.Parse(id)
uuid, err := uuid.Parse(chi.URLParam(r, "ID"))
if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest)
return
@ -170,14 +150,7 @@ func (s *Server) getSources(w http.ResponseWriter, r *http.Request) {
}
payload.Payload = res
bResult, err := json.Marshal(payload)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bResult)
s.WriteJson(w, payload)
}
// GetSourceByNameAndSource
@ -219,15 +192,7 @@ func (s *Server) GetSourceBySourceAndName(w http.ResponseWriter, r *http.Request
}
p.Payload = item
bResult, err := json.Marshal(item)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set(HeaderContentType, ApplicationJson)
w.Write(bResult)
s.WriteJson(w, p)
}
// NewRedditSource
@ -242,8 +207,6 @@ func (s *Server) newRedditSource(w http.ResponseWriter, r *http.Request) {
_url := query["url"][0]
//_tags := query["tags"][0]
w.Header().Set("Content-Type", "application/json")
if _url == "" {
s.WriteError(w, "url is missing a value", http.StatusBadRequest)
return
@ -277,6 +240,7 @@ func (s *Server) newRedditSource(w http.ResponseWriter, r *http.Request) {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
//s.WriteJson(w, &params)
bJson, err := json.Marshal(&params)
if err != nil {

View File

@ -34,6 +34,11 @@ type GetSubscription struct {
Payload models.SubscriptionDto `json:"payload"`
}
type ListSubscriptionDetails struct {
ApiStatusModel
Payload []models.SubscriptionDetailsDto `json:"payload"`
}
// GetSubscriptions
// @Summary Returns the top 100 entries from the queue to be processed.
// @Produce application/json
@ -43,8 +48,6 @@ type GetSubscription struct {
// @Failure 400 {object} ApiError "Unable to reach SQL."
// @Failure 500 {object} ApiError "Failed to process data from SQL."
func (s *Server) ListSubscriptions(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
payload := ListSubscriptions{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
@ -59,19 +62,7 @@ func (s *Server) ListSubscriptions(w http.ResponseWriter, r *http.Request) {
}
payload.Payload = res
bres, err := json.Marshal(payload)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
}
type ListSubscriptionDetails struct {
ApiStatusModel
Payload []models.SubscriptionDetailsDto `json:"payload"`
s.WriteJson(w, payload)
}
// ListSubscriptionDetails
@ -80,8 +71,7 @@ type ListSubscriptionDetails struct {
// @Tags Subscription
// @Router /subscriptions/details [get]
// @Success 200 {object} ListSubscriptionDetails "ok"
func (s Server) ListSubscriptionDetails(w http.ResponseWriter, r *http.Request) {
w.Header().Set(HeaderContentType, ApplicationJson)
func (s *Server) ListSubscriptionDetails(w http.ResponseWriter, r *http.Request) {
payload := ListSubscriptionDetails{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
@ -96,13 +86,7 @@ func (s Server) ListSubscriptionDetails(w http.ResponseWriter, r *http.Request)
}
payload.Payload = res
b, err := json.Marshal(payload)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
}
w.Write(b)
s.WriteJson(w, payload)
}
// GetSubscriptionsByDiscordId
@ -115,8 +99,6 @@ func (s Server) ListSubscriptionDetails(w http.ResponseWriter, r *http.Request)
// @Failure 400 {object} ApiError "Unable to reach SQL or Data problems"
// @Failure 500 {object} ApiError "Data problems"
func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Request) {
w.Header().Set(HeaderContentType, ApplicationJson)
p := ListSubscriptions{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
@ -125,13 +107,12 @@ func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Requ
}
query := r.URL.Query()
_id := query["id"][0]
if _id == "" {
if query["id"][0] == "" {
s.WriteError(w, ErrIdValueMissing, http.StatusBadRequest)
return
}
uuid, err := uuid.Parse(_id)
uuid, err := uuid.Parse(query["id"][0])
if err != nil {
s.WriteError(w, ErrValueNotUuid, http.StatusBadRequest)
return
@ -144,14 +125,7 @@ func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Requ
}
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
s.WriteJson(w, p)
}
// GetSubscriptionsBySourceId
@ -162,8 +136,6 @@ func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Requ
// @Router /subscriptions/by/SourceId [get]
// @Success 200 {object} ListSubscriptions "ok"
func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
p := ListSubscriptions{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
@ -191,14 +163,7 @@ func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Reque
}
p.Payload = res
bres, err := json.Marshal(p)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bres)
s.WriteJson(w, p)
}
// NewDiscordWebHookSubscription
@ -208,8 +173,6 @@ func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Reque
// @Tags Subscription
// @Router /subscriptions/discord/webhook/new [post]
func (s *Server) newDiscordWebHookSubscription(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// Extract the values given
query := r.URL.Query()
discordWebHookId := query["discordWebHookId"][0]