sources now sends back a standard message
This commit is contained in:
parent
b0a78c4a1e
commit
025a503aff
@ -3,6 +3,8 @@ package routes
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
//"net/http"
|
//"net/http"
|
||||||
|
|
||||||
@ -12,6 +14,7 @@ import (
|
|||||||
httpSwagger "github.com/swaggo/http-swagger"
|
httpSwagger "github.com/swaggo/http-swagger"
|
||||||
|
|
||||||
"github.com/jtom38/newsbot/collector/database"
|
"github.com/jtom38/newsbot/collector/database"
|
||||||
|
"github.com/jtom38/newsbot/collector/domain/models"
|
||||||
"github.com/jtom38/newsbot/collector/services/config"
|
"github.com/jtom38/newsbot/collector/services/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -69,9 +72,9 @@ func (s *Server) MountRoutes() {
|
|||||||
))
|
))
|
||||||
|
|
||||||
/* Root Routes */
|
/* Root Routes */
|
||||||
s.Router.Get("/api/helloworld", helloWorld)
|
//s.Router.Get("/api/helloworld", helloWorld)
|
||||||
s.Router.Get("/api/hello/{who}", helloWho)
|
//s.Router.Get("/api/hello/{who}", helloWho)
|
||||||
s.Router.Get("/api/ping", ping)
|
//s.Router.Get("/api/ping", ping)
|
||||||
|
|
||||||
/* Article Routes */
|
/* Article Routes */
|
||||||
s.Router.Get("/api/articles", s.listArticles)
|
s.Router.Get("/api/articles", s.listArticles)
|
||||||
@ -80,15 +83,15 @@ func (s *Server) MountRoutes() {
|
|||||||
})
|
})
|
||||||
s.Router.Get("/api/articles/by/sourceid", s.GetArticlesBySourceId)
|
s.Router.Get("/api/articles/by/sourceid", s.GetArticlesBySourceId)
|
||||||
|
|
||||||
/* Discord Queue */
|
/* Queue */
|
||||||
s.Router.Get("/api/discord/queue", s.GetDiscordQueue)
|
s.Router.Mount("/api/queue", s.GetQueueRouter())
|
||||||
|
|
||||||
/* Discord WebHooks */
|
/* Discord WebHooks */
|
||||||
s.Router.Post("/api/discord/webhooks/new", s.NewDiscordWebHook)
|
|
||||||
s.Router.Get("/api/discord/webhooks", s.GetDiscordWebHooks)
|
s.Router.Get("/api/discord/webhooks", s.GetDiscordWebHooks)
|
||||||
|
s.Router.Post("/api/discord/webhooks/new", s.NewDiscordWebHook)
|
||||||
//s.Router.Get("/api/discord/webhooks/byId", s.GetDiscordWebHooksById)
|
//s.Router.Get("/api/discord/webhooks/byId", s.GetDiscordWebHooksById)
|
||||||
s.Router.Get("/api/discord/webhooks/by/serverAndChannel", s.GetDiscordWebHooksByServerAndChannel)
|
s.Router.Get("/api/discord/webhooks/by/serverAndChannel", s.GetDiscordWebHooksByServerAndChannel)
|
||||||
|
|
||||||
s.Router.Route("/api/discord/webhooks/{ID}", func(r chi.Router) {
|
s.Router.Route("/api/discord/webhooks/{ID}", func(r chi.Router) {
|
||||||
r.Get("/", s.GetDiscordWebHooksById)
|
r.Get("/", s.GetDiscordWebHooksById)
|
||||||
r.Delete("/", s.deleteDiscordWebHook)
|
r.Delete("/", s.deleteDiscordWebHook)
|
||||||
@ -99,25 +102,21 @@ func (s *Server) MountRoutes() {
|
|||||||
/* Settings */
|
/* Settings */
|
||||||
s.Router.Get("/api/settings", s.getSettings)
|
s.Router.Get("/api/settings", s.getSettings)
|
||||||
|
|
||||||
/* Source Routes */
|
s.Router.Mount("/api/sources", s.GetSourcesRouter())
|
||||||
s.Router.Get("/api/config/sources", s.listSources)
|
s.Router.Mount("/api/subscriptions", s.GetSubscriptionsRouter())
|
||||||
s.Router.Get("/api/config/sources/by/source", s.listSourcesBySource)
|
}
|
||||||
s.Router.Post("/api/config/sources/new/reddit", s.newRedditSource)
|
|
||||||
s.Router.Post("/api/config/sources/new/youtube", s.newYoutubeSource)
|
func (s *Server) WriteError(w http.ResponseWriter, errMessage string, HttpStatusCode int, Payload interface{}) {
|
||||||
s.Router.Post("/api/config/sources/new/twitch", s.newTwitchSource)
|
e := models.ApiError{
|
||||||
s.Router.Route("/api/config/sources/{ID}", func(r chi.Router) {
|
Message: errMessage,
|
||||||
r.Get("/", s.getSources)
|
StatusCode: http.StatusInternalServerError,
|
||||||
r.Delete("/", s.deleteSources)
|
Payload: nil,
|
||||||
r.Post("/disable", s.disableSource)
|
}
|
||||||
r.Post("/enable", s.enableSource)
|
|
||||||
//r.Post("/delete", )
|
b, err := json.Marshal(e)
|
||||||
})
|
if err != nil {
|
||||||
s.Router.Get("/api/config/sources/by/sourceAndName", s.GetSourceBySourceAndName)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
/* Subscriptions */
|
|
||||||
s.Router.Get("/api/subscriptions", s.ListSubscriptions)
|
w.Write(b)
|
||||||
s.Router.Get("/api/subscriptions/byDiscordId", s.GetSubscriptionsByDiscordId)
|
|
||||||
s.Router.Get("/api/subscriptions/bySourceId", s.GetSubscriptionsBySourceId)
|
|
||||||
s.Router.Post("/api/subscriptions/new/discordwebhook", s.newDiscordWebHookSubscription)
|
|
||||||
s.Router.Delete("/api/subscriptions/discord/webhook/delete", s.DeleteDiscordWebHookSubscription)
|
|
||||||
}
|
}
|
||||||
|
@ -13,11 +13,46 @@ import (
|
|||||||
"github.com/jtom38/newsbot/collector/database"
|
"github.com/jtom38/newsbot/collector/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (s *Server) GetSourcesRouter() http.Handler {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
|
||||||
|
r.Get("/", s.listSources)
|
||||||
|
r.Get("/by/source", s.listSourcesBySource)
|
||||||
|
r.Get("/by/sourceAndName", s.GetSourceBySourceAndName)
|
||||||
|
|
||||||
|
r.Post("/new/reddit", s.newRedditSource)
|
||||||
|
r.Post("/new/youtube", s.newYoutubeSource)
|
||||||
|
r.Post("/new/twitch", s.newTwitchSource)
|
||||||
|
|
||||||
|
r.Route("/{ID}", func(p chi.Router) {
|
||||||
|
p.Get("/", s.getSources)
|
||||||
|
p.Delete("/", s.deleteSources)
|
||||||
|
p.Post("/disable", s.disableSource)
|
||||||
|
p.Post("/enable", s.enableSource)
|
||||||
|
})
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
type ListSourcesResults struct {
|
||||||
|
StatusCode int `json:"status"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Payload []database.SourceDto `json:"payload"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetSourceResult struct {
|
||||||
|
StatusCode int `json:"status"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Payload database.SourceDto `json:"payload"`
|
||||||
|
}
|
||||||
|
|
||||||
// ListSources
|
// ListSources
|
||||||
// @Summary Lists the top 50 records
|
// @Summary Lists the top 50 records
|
||||||
// @Produce application/json
|
// @Produce application/json
|
||||||
// @Tags Config, Source
|
// @Tags Source
|
||||||
// @Router /config/sources [get]
|
// @Router /sources [get]
|
||||||
|
// @Success 200 {object} ListSourcesResults "ok"
|
||||||
|
// @Failure 400 {object} models.ApiError "Unable to reach SQL or Data problems"
|
||||||
func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
|
||||||
//TODO Add top?
|
//TODO Add top?
|
||||||
/*
|
/*
|
||||||
@ -29,10 +64,16 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
|
|||||||
res, err := s.Db.ListSources(*s.ctx, int32(topInt))
|
res, err := s.Db.ListSources(*s.ctx, int32(topInt))
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
result := ListSourcesResults{
|
||||||
|
StatusCode: http.StatusOK,
|
||||||
|
Message: "OK",
|
||||||
|
}
|
||||||
|
|
||||||
// Default way of showing all sources
|
// Default way of showing all sources
|
||||||
res, err := s.Db.ListSources(*s.ctx, 50)
|
res, err := s.Db.ListSources(*s.ctx, 50)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "url is missing a value", http.StatusBadRequest)
|
s.WriteError(w, err.Error(), http.StatusInternalServerError, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,23 +82,26 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
|
|||||||
dto = append(dto, database.ConvertToSourceDto(item))
|
dto = append(dto, database.ConvertToSourceDto(item))
|
||||||
}
|
}
|
||||||
|
|
||||||
bResult, err := json.Marshal(dto)
|
result.Payload = dto
|
||||||
|
|
||||||
|
bResult, err := json.Marshal(result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "unable to convert to json", http.StatusBadRequest)
|
s.WriteError(w, err.Error(), http.StatusInternalServerError, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.Write(bResult)
|
w.Write(bResult)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListSourcesBySource
|
// ListSourcesBySource
|
||||||
// @Summary Lists the top 50 records based on the name given. Example: reddit
|
// @Summary Lists the top 50 records based on the name given. Example: reddit
|
||||||
// @Param source query string true "Source Name"
|
// @Param source query string true "Source Name"
|
||||||
// @Produce application/json
|
// @Produce application/json
|
||||||
// @Tags Config, Source
|
// @Tags Source
|
||||||
// @Router /config/sources/by/source [get]
|
// @Router /sources/by/source [get]
|
||||||
|
// @Success 200 {object} ListSourcesResults "ok"
|
||||||
|
// @Failure 400 {object} models.ApiError "Unable to query SQL."
|
||||||
|
// @Failure 500 {object} models.ApiError "Problems with data."
|
||||||
func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
|
||||||
//TODO Add top?
|
//TODO Add top?
|
||||||
/*
|
/*
|
||||||
@ -68,6 +112,12 @@ func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
res, err := s.Db.ListSources(*s.ctx, int32(topInt))
|
res, err := s.Db.ListSources(*s.ctx, int32(topInt))
|
||||||
*/
|
*/
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
result := ListSourcesResults{
|
||||||
|
StatusCode: http.StatusOK,
|
||||||
|
Message: "OK",
|
||||||
|
}
|
||||||
|
|
||||||
query := r.URL.Query()
|
query := r.URL.Query()
|
||||||
_source := query["source"][0]
|
_source := query["source"][0]
|
||||||
@ -75,16 +125,20 @@ func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
|
|||||||
// Shows the list by Sources.source
|
// Shows the list by Sources.source
|
||||||
res, err := s.Db.ListSourcesBySource(*s.ctx, strings.ToLower(_source))
|
res, err := s.Db.ListSourcesBySource(*s.ctx, strings.ToLower(_source))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "invalid source is missing a value", http.StatusBadRequest)
|
s.WriteError(w, err.Error(), http.StatusBadRequest, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
bResult, err := json.Marshal(res)
|
|
||||||
if err != nil {
|
for _, item := range res {
|
||||||
http.Error(w, "unable to convert to json", http.StatusBadRequest)
|
result.Payload = append(result.Payload, database.ConvertToSourceDto(item))
|
||||||
|
}
|
||||||
|
|
||||||
|
bResult, err := json.Marshal(result)
|
||||||
|
if err != nil {
|
||||||
|
s.WriteError(w, err.Error(), http.StatusInternalServerError, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.Write(bResult)
|
w.Write(bResult)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,29 +146,42 @@ func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
|
|||||||
// @Summary Returns a single entity by ID
|
// @Summary Returns a single entity by ID
|
||||||
// @Param id path string true "uuid"
|
// @Param id path string true "uuid"
|
||||||
// @Produce application/json
|
// @Produce application/json
|
||||||
// @Tags Config, Source
|
// @Tags Source
|
||||||
// @Router /config/sources/{id} [get]
|
// @Router /sources/{id} [get]
|
||||||
|
// @Success 200 {object} GetSourceResult "ok"
|
||||||
|
// @Failure 204 {object} models.ApiError "No record found."
|
||||||
|
// @Failure 400 {object} models.ApiError "Unable to query SQL."
|
||||||
|
// @Failure 500 {object} models.ApiError "Failed to process data from SQL."
|
||||||
func (s *Server) getSources(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) getSources(w http.ResponseWriter, r *http.Request) {
|
||||||
id := chi.URLParam(r, "ID")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
id := chi.URLParam(r, "ID")
|
||||||
uuid, err := uuid.Parse(id)
|
uuid, err := uuid.Parse(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "id is not a uuid", http.StatusBadRequest)
|
s.WriteError(w, err.Error(), http.StatusBadRequest, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := s.Db.GetSourceByID(*s.ctx, uuid)
|
res, err := s.Db.GetSourceByID(*s.ctx, uuid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "invalid id was given", http.StatusBadRequest)
|
s.WriteError(w, err.Error(), http.StatusNoContent, nil)
|
||||||
panic(err)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
bResult, err := json.Marshal(res)
|
dto := database.ConvertToSourceDto(res)
|
||||||
|
|
||||||
|
payload := GetSourceResult{
|
||||||
|
Message: "OK",
|
||||||
|
StatusCode: http.StatusOK,
|
||||||
|
Payload: dto,
|
||||||
|
}
|
||||||
|
|
||||||
|
bResult, err := json.Marshal(payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicln(err)
|
s.WriteError(w, err.Error(), http.StatusInternalServerError, nil)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.Write(bResult)
|
w.Write(bResult)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,8 +190,8 @@ func (s *Server) getSources(w http.ResponseWriter, r *http.Request) {
|
|||||||
// @Param name query string true "dadjokes"
|
// @Param name query string true "dadjokes"
|
||||||
// @Param source query string true "reddit"
|
// @Param source query string true "reddit"
|
||||||
// @Produce application/json
|
// @Produce application/json
|
||||||
// @Tags Config, Source
|
// @Tags Source
|
||||||
// @Router /config/sources/by/sourceAndName [get]
|
// @Router /sources/by/sourceAndName [get]
|
||||||
func (s *Server) GetSourceBySourceAndName(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) GetSourceBySourceAndName(w http.ResponseWriter, r *http.Request) {
|
||||||
query := r.URL.Query()
|
query := r.URL.Query()
|
||||||
|
|
||||||
@ -161,8 +228,8 @@ func (s *Server) GetSourceBySourceAndName(w http.ResponseWriter, r *http.Request
|
|||||||
// @Summary Creates a new reddit source to monitor.
|
// @Summary Creates a new reddit source to monitor.
|
||||||
// @Param name query string true "name"
|
// @Param name query string true "name"
|
||||||
// @Param url query string true "url"
|
// @Param url query string true "url"
|
||||||
// @Tags Config, Source, Reddit
|
// @Tags Source, Reddit
|
||||||
// @Router /config/sources/new/reddit [post]
|
// @Router /sources/new/reddit [post]
|
||||||
func (s *Server) newRedditSource(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) newRedditSource(w http.ResponseWriter, r *http.Request) {
|
||||||
query := r.URL.Query()
|
query := r.URL.Query()
|
||||||
_name := query["name"][0]
|
_name := query["name"][0]
|
||||||
@ -211,8 +278,8 @@ func (s *Server) newRedditSource(w http.ResponseWriter, r *http.Request) {
|
|||||||
// @Summary Creates a new youtube source to monitor.
|
// @Summary Creates a new youtube source to monitor.
|
||||||
// @Param name query string true "name"
|
// @Param name query string true "name"
|
||||||
// @Param url query string true "url"
|
// @Param url query string true "url"
|
||||||
// @Tags Config, Source, YouTube
|
// @Tags Source, YouTube
|
||||||
// @Router /config/sources/new/youtube [post]
|
// @Router /sources/new/youtube [post]
|
||||||
func (s *Server) newYoutubeSource(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) newYoutubeSource(w http.ResponseWriter, r *http.Request) {
|
||||||
query := r.URL.Query()
|
query := r.URL.Query()
|
||||||
_name := query["name"][0]
|
_name := query["name"][0]
|
||||||
@ -259,8 +326,8 @@ func (s *Server) newYoutubeSource(w http.ResponseWriter, r *http.Request) {
|
|||||||
// NewTwitchSource
|
// NewTwitchSource
|
||||||
// @Summary Creates a new twitch source to monitor.
|
// @Summary Creates a new twitch source to monitor.
|
||||||
// @Param name query string true "name"
|
// @Param name query string true "name"
|
||||||
// @Tags Config, Source, Twitch
|
// @Tags Source, Twitch
|
||||||
// @Router /config/sources/new/twitch [post]
|
// @Router /sources/new/twitch [post]
|
||||||
func (s *Server) newTwitchSource(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) newTwitchSource(w http.ResponseWriter, r *http.Request) {
|
||||||
query := r.URL.Query()
|
query := r.URL.Query()
|
||||||
_name := query["name"][0]
|
_name := query["name"][0]
|
||||||
@ -292,7 +359,7 @@ func (s *Server) newTwitchSource(w http.ResponseWriter, r *http.Request) {
|
|||||||
// @Summary Marks a source as deleted based on its ID value.
|
// @Summary Marks a source as deleted based on its ID value.
|
||||||
// @Param id path string true "id"
|
// @Param id path string true "id"
|
||||||
// @Tags Source
|
// @Tags Source
|
||||||
// @Router /config/sources/{id} [POST]
|
// @Router /sources/{id} [POST]
|
||||||
func (s *Server) deleteSources(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) deleteSources(w http.ResponseWriter, r *http.Request) {
|
||||||
//var item model.Sources = model.Sources{}
|
//var item model.Sources = model.Sources{}
|
||||||
|
|
||||||
@ -318,8 +385,8 @@ func (s *Server) deleteSources(w http.ResponseWriter, r *http.Request) {
|
|||||||
// DisableSource
|
// DisableSource
|
||||||
// @Summary Disables a source from processing.
|
// @Summary Disables a source from processing.
|
||||||
// @Param id path string true "id"
|
// @Param id path string true "id"
|
||||||
// @Tags Config, Source
|
// @Tags Source
|
||||||
// @Router /config/sources/{id}/disable [post]
|
// @Router /sources/{id}/disable [post]
|
||||||
func (s *Server) disableSource(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) disableSource(w http.ResponseWriter, r *http.Request) {
|
||||||
id := chi.URLParam(r, "ID")
|
id := chi.URLParam(r, "ID")
|
||||||
uuid, err := uuid.Parse(id)
|
uuid, err := uuid.Parse(id)
|
||||||
@ -342,8 +409,8 @@ func (s *Server) disableSource(w http.ResponseWriter, r *http.Request) {
|
|||||||
// EnableSource
|
// EnableSource
|
||||||
// @Summary Enables a source to continue processing.
|
// @Summary Enables a source to continue processing.
|
||||||
// @Param id path string true "id"
|
// @Param id path string true "id"
|
||||||
// @Tags Config, Source
|
// @Tags Source
|
||||||
// @Router /config/sources/{id}/enable [post]
|
// @Router /sources/{id}/enable [post]
|
||||||
func (s *Server) enableSource(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) enableSource(w http.ResponseWriter, r *http.Request) {
|
||||||
id := chi.URLParam(r, "ID")
|
id := chi.URLParam(r, "ID")
|
||||||
uuid, err := uuid.Parse(id)
|
uuid, err := uuid.Parse(id)
|
||||||
|
Loading…
Reference in New Issue
Block a user