cleaned up swag and updated models to take from the base apistatusmodel. less human errors this way

This commit is contained in:
James Tombleson 2022-12-11 09:47:48 -08:00
parent c74a35292b
commit 3b72a4b329

View File

@ -11,6 +11,7 @@ import (
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/jtom38/newsbot/collector/database" "github.com/jtom38/newsbot/collector/database"
"github.com/jtom38/newsbot/collector/domain/models"
) )
func (s *Server) GetSourcesRouter() http.Handler { func (s *Server) GetSourcesRouter() http.Handler {
@ -35,15 +36,13 @@ func (s *Server) GetSourcesRouter() http.Handler {
} }
type ListSourcesResults struct { type ListSourcesResults struct {
StatusCode int `json:"status"` ApiStatusModel
Message string `json:"message"` Payload []models.SourceDto `json:"payload"`
Payload []database.SourceDto `json:"payload"`
} }
type GetSourceResult struct { type GetSourceResult struct {
StatusCode int `json:"status"` ApiStatusModel
Message string `json:"message"` Payload models.SourceDto `json:"payload"`
Payload database.SourceDto `json:"payload"`
} }
// ListSources // ListSources
@ -52,7 +51,7 @@ type GetSourceResult struct {
// @Tags Source // @Tags Source
// @Router /sources [get] // @Router /sources [get]
// @Success 200 {object} ListSourcesResults "ok" // @Success 200 {object} ListSourcesResults "ok"
// @Failure 400 {object} models.ApiError "Unable to reach SQL or Data problems" // @Failure 400 {object} 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?
/* /*
@ -66,27 +65,29 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
result := ListSourcesResults{ result := ListSourcesResults{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK, StatusCode: http.StatusOK,
Message: "OK", 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 {
s.WriteError(w, err.Error(), http.StatusInternalServerError, nil) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return return
} }
var dto []database.SourceDto var dto []models.SourceDto
for _, item := range res { for _, item := range res {
dto = append(dto, database.ConvertToSourceDto(item)) dto = append(dto, models.ConvertToSourceDto(item))
} }
result.Payload = dto result.Payload = dto
bResult, err := json.Marshal(result) bResult, err := json.Marshal(result)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError, nil) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return return
} }
@ -100,8 +101,8 @@ func (s *Server) listSources(w http.ResponseWriter, r *http.Request) {
// @Tags Source // @Tags Source
// @Router /sources/by/source [get] // @Router /sources/by/source [get]
// @Success 200 {object} ListSourcesResults "ok" // @Success 200 {object} ListSourcesResults "ok"
// @Failure 400 {object} models.ApiError "Unable to query SQL." // @Failure 400 {object} ApiError "Unable to query SQL."
// @Failure 500 {object} models.ApiError "Problems with data." // @Failure 500 {object} 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?
/* /*
@ -115,8 +116,10 @@ func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
result := ListSourcesResults{ result := ListSourcesResults{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK, StatusCode: http.StatusOK,
Message: "OK", Message: "OK",
},
} }
query := r.URL.Query() query := r.URL.Query()
@ -125,17 +128,17 @@ 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 {
s.WriteError(w, err.Error(), http.StatusBadRequest, nil) s.WriteError(w, err.Error(), http.StatusBadRequest)
return return
} }
for _, item := range res { for _, item := range res {
result.Payload = append(result.Payload, database.ConvertToSourceDto(item)) result.Payload = append(result.Payload, models.ConvertToSourceDto(item))
} }
bResult, err := json.Marshal(result) bResult, err := json.Marshal(result)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError, nil) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return return
} }
@ -149,36 +152,38 @@ func (s *Server) listSourcesBySource(w http.ResponseWriter, r *http.Request) {
// @Tags Source // @Tags Source
// @Router /sources/{id} [get] // @Router /sources/{id} [get]
// @Success 200 {object} GetSourceResult "ok" // @Success 200 {object} GetSourceResult "ok"
// @Failure 204 {object} models.ApiError "No record found." // @Failure 204 {object} ApiError "No record found."
// @Failure 400 {object} models.ApiError "Unable to query SQL." // @Failure 400 {object} ApiError "Unable to query SQL."
// @Failure 500 {object} models.ApiError "Failed to process data from SQL." // @Failure 500 {object} 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) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
id := chi.URLParam(r, "ID") id := chi.URLParam(r, "ID")
uuid, err := uuid.Parse(id) uuid, err := uuid.Parse(id)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusBadRequest, nil) s.WriteError(w, err.Error(), http.StatusBadRequest)
return return
} }
res, err := s.Db.GetSourceByID(*s.ctx, uuid) res, err := s.Db.GetSourceByID(*s.ctx, uuid)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusNoContent, nil) s.WriteError(w, err.Error(), http.StatusNoContent)
return return
} }
dto := database.ConvertToSourceDto(res) dto := models.ConvertToSourceDto(res)
payload := GetSourceResult{ payload := GetSourceResult{
ApiStatusModel: ApiStatusModel{
Message: "OK", Message: "OK",
StatusCode: http.StatusOK, StatusCode: http.StatusOK,
},
Payload: dto, Payload: dto,
} }
bResult, err := json.Marshal(payload) bResult, err := json.Marshal(payload)
if err != nil { if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError, nil) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return return
} }
@ -228,7 +233,7 @@ 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 Source, Reddit // @Tags Source
// @Router /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()
@ -236,12 +241,14 @@ func (s *Server) newRedditSource(w http.ResponseWriter, r *http.Request) {
_url := query["url"][0] _url := query["url"][0]
//_tags := query["tags"][0] //_tags := query["tags"][0]
w.Header().Set("Content-Type", "application/json")
if _url == "" { if _url == "" {
http.Error(w, "url is missing a value", http.StatusBadRequest) s.WriteError(w, "url is missing a value", http.StatusBadRequest)
return return
} }
if !strings.Contains(_url, "reddit.com") { if !strings.Contains(_url, "reddit.com") {
http.Error(w, "invalid url", http.StatusBadRequest) s.WriteError(w, "invalid url", http.StatusBadRequest)
return return
} }
@ -264,13 +271,18 @@ func (s *Server) newRedditSource(w http.ResponseWriter, r *http.Request) {
Url: _url, Url: _url,
Tags: tags, Tags: tags,
} }
s.Db.CreateSource(*s.ctx, params) err := s.Db.CreateSource(*s.ctx, params)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
bJson, err := json.Marshal(&params) bJson, err := json.Marshal(&params)
if err != nil { if err != nil {
log.Panicln(err) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
} }
w.Header().Set("Content-Type", "application/json")
w.Write(bJson) w.Write(bJson)
} }
@ -278,7 +290,7 @@ 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 Source, YouTube // @Tags Source
// @Router /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()
@ -286,12 +298,13 @@ func (s *Server) newYoutubeSource(w http.ResponseWriter, r *http.Request) {
_url := query["url"][0] _url := query["url"][0]
//_tags := query["tags"][0] //_tags := query["tags"][0]
w.Header().Set("Content-Type", "application/json")
if _url == "" { if _url == "" {
http.Error(w, "url is missing a value", http.StatusBadRequest) s.WriteError(w, "url is missing a value", http.StatusBadRequest)
return return
} }
if !strings.Contains(_url, "youtube.com") { if !strings.Contains(_url, "youtube.com") {
http.Error(w, "invalid url", http.StatusBadRequest) s.WriteError(w, "invalid url", http.StatusBadRequest)
return return
} }
@ -313,22 +326,29 @@ func (s *Server) newYoutubeSource(w http.ResponseWriter, r *http.Request) {
Url: _url, Url: _url,
Tags: tags, Tags: tags,
} }
s.Db.CreateSource(*s.ctx, params) err := s.Db.CreateSource(*s.ctx, params)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
bJson, err := json.Marshal(&params) bJson, err := json.Marshal(&params)
if err != nil { if err != nil {
log.Panicln(err) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
} }
w.Header().Set("Content-Type", "application/json")
w.Write(bJson) w.Write(bJson)
} }
// 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 Source, Twitch // @Tags Source
// @Router /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) {
w.Header().Set("Content-Type", "application/json")
query := r.URL.Query() query := r.URL.Query()
_name := query["name"][0] _name := query["name"][0]
@ -345,13 +365,18 @@ func (s *Server) newTwitchSource(w http.ResponseWriter, r *http.Request) {
Url: _url, Url: _url,
Tags: tags, Tags: tags,
} }
s.Db.CreateSource(*s.ctx, params) err := s.Db.CreateSource(*s.ctx, params)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
bJson, err := json.Marshal(&params) bJson, err := json.Marshal(&params)
if err != nil { if err != nil {
log.Panicln(err) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
} }
w.Header().Set("Content-Type", "application/json")
w.Write(bJson) w.Write(bJson)
} }
@ -366,20 +391,36 @@ func (s *Server) deleteSources(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)
if err != nil { if err != nil {
log.Panicln(err) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
} }
// Check to make sure we can find the record // Check to make sure we can find the record
_, err = s.Db.GetSourceByID(*s.ctx, uuid) _, err = s.Db.GetSourceByID(*s.ctx, uuid)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
} }
// Delete the record // Delete the record
err = s.Db.DeleteSource(*s.ctx, uuid) err = s.Db.DeleteSource(*s.ctx, uuid)
if err != nil { if err != nil {
log.Panic(err) s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
} }
p := ApiStatusModel {
Message: "OK",
StatusCode: http.StatusOK,
}
b, err := json.Marshal(p)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(b)
} }
// DisableSource // DisableSource
@ -391,19 +432,32 @@ 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)
if err != nil { if err != nil {
log.Panicln(err) s.WriteError(w, err.Error(), http.StatusInternalServerError)
} }
// Check to make sure we can find the record // Check to make sure we can find the record
_, err = s.Db.GetSourceByID(*s.ctx, uuid) _, err = s.Db.GetSourceByID(*s.ctx, uuid)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) s.WriteError(w, err.Error(), http.StatusInternalServerError)
} }
err = s.Db.DisableSource(*s.ctx, uuid) err = s.Db.DisableSource(*s.ctx, uuid)
if err != nil { if err != nil {
log.Panic(err) s.WriteError(w, err.Error(), http.StatusInternalServerError)
} }
p := ApiStatusModel {
Message: "OK",
StatusCode: http.StatusOK,
}
b, err := json.Marshal(p)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(b)
} }
// EnableSource // EnableSource
@ -415,17 +469,30 @@ 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)
if err != nil { if err != nil {
log.Panicln(err) s.WriteError(w, err.Error(), http.StatusInternalServerError)
} }
// Check to make sure we can find the record // Check to make sure we can find the record
_, err = s.Db.GetSourceByID(*s.ctx, uuid) _, err = s.Db.GetSourceByID(*s.ctx, uuid)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) s.WriteError(w, err.Error(), http.StatusInternalServerError)
} }
err = s.Db.EnableSource(*s.ctx, uuid) err = s.Db.EnableSource(*s.ctx, uuid)
if err != nil { if err != nil {
log.Panic(err) s.WriteError(w, err.Error(), http.StatusInternalServerError)
} }
p := ApiStatusModel {
Message: "OK",
StatusCode: http.StatusOK,
}
b, err := json.Marshal(p)
if err != nil {
s.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(b)
} }