From dfd44714c06d49edfb751720031e18b7907ed71d Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 28 Apr 2024 11:41:34 -0700 Subject: [PATCH] minor error updates and will soon be pulled apart --- internal/handler/v1/discordwebhooks.go | 26 +++++----- internal/handler/v1/queue.go | 2 +- internal/handler/v1/settings.go | 2 +- internal/handler/v1/sources.go | 69 +++++++++++--------------- internal/handler/v1/subscriptions.go | 37 +++++++------- 5 files changed, 63 insertions(+), 73 deletions(-) diff --git a/internal/handler/v1/discordwebhooks.go b/internal/handler/v1/discordwebhooks.go index 38cc14d..93ed20a 100644 --- a/internal/handler/v1/discordwebhooks.go +++ b/internal/handler/v1/discordwebhooks.go @@ -60,21 +60,21 @@ func (s *Handler) GetDiscordWebHooksById(c echo.Context) error { _id := c.Param("ID") if _id == "" { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: ErrIdValueMissing, }) } uuid, err := uuid.Parse(_id) if err != nil { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: ErrUnableToParseId, }) } res, err := s.dto.GetDiscordWebhook(c.Request().Context(), uuid) if err != nil { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: ErrNoRecordFound, }) } @@ -100,21 +100,21 @@ func (s *Handler) GetDiscordWebHooksByServerAndChannel(c echo.Context) error { _server := c.QueryParam("server") if _server == "" { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: ErrIdValueMissing, }) } _channel := c.QueryParam("channel") if _channel == "" { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: fmt.Sprintf("%s channel", ErrParameterMissing), }) } res, err := s.dto.GetDiscordWebHookByServerAndChannel(c.Request().Context(), _server, _channel) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ + return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } @@ -136,22 +136,22 @@ func (s *Handler) NewDiscordWebHook(c echo.Context) error { _channel := c.QueryParam("channel") if _url == "" { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: "url is missing a value", }) } if !strings.Contains(_url, "discord.com/api/webhooks") { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: "invalid url", }) } if _server == "" { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: "server is missing", }) } if _channel == "" { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: "channel is missing", }) } @@ -176,7 +176,7 @@ func (s *Handler) disableDiscordWebHook(c echo.Context) error { id := c.Param("ID") uuid, err := uuid.Parse(id) if err != nil { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: err.Error(), }) } @@ -189,7 +189,7 @@ func (s *Handler) disableDiscordWebHook(c echo.Context) error { err = s.Db.DisableDiscordWebHook(c.Request().Context(), uuid) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ + return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } @@ -205,7 +205,7 @@ func (s *Handler) enableDiscordWebHook(c echo.Context) error { id := c.Param("ID") uuid, err := uuid.Parse(id) if err != nil { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: err.Error(), }) } diff --git a/internal/handler/v1/queue.go b/internal/handler/v1/queue.go index 1f8ad59..deadb89 100644 --- a/internal/handler/v1/queue.go +++ b/internal/handler/v1/queue.go @@ -30,7 +30,7 @@ func (s *Handler) ListDiscordWebhookQueue(c echo.Context) error { // Get the raw resp from sql res, err := s.dto.ListDiscordWebhookQueueDetails(c.Request().Context(), 50) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ + return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } diff --git a/internal/handler/v1/settings.go b/internal/handler/v1/settings.go index 96b472d..114500d 100644 --- a/internal/handler/v1/settings.go +++ b/internal/handler/v1/settings.go @@ -20,7 +20,7 @@ func (s *Handler) getSettings(c echo.Context) error { uuid, err := uuid.Parse(id) if err != nil { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: err.Error(), }) } diff --git a/internal/handler/v1/sources.go b/internal/handler/v1/sources.go index cadd521..c725ddf 100644 --- a/internal/handler/v1/sources.go +++ b/internal/handler/v1/sources.go @@ -10,19 +10,10 @@ import ( "git.jamestombleson.com/jtom38/newsbot-api/internal/database" "git.jamestombleson.com/jtom38/newsbot-api/internal/domain" "git.jamestombleson.com/jtom38/newsbot-api/internal/domain/models" - "github.com/go-chi/chi/v5" "github.com/google/uuid" "github.com/labstack/echo/v4" ) -func (s *Handler) GetSourcesRouter() http.Handler { - r := chi.NewRouter() - - - - return r -} - type ListSources struct { ApiStatusModel Payload []models.SourceDto `json:"payload"` @@ -38,8 +29,8 @@ type GetSource struct { // @Produce application/json // @Tags Source // @Router /sources [get] -// @Success 200 {object} ListSources "ok" -// @Failure 400 {object} ApiError "Unable to reach SQL or Data problems" +// @Success 200 {object} ListSources "ok" +// @Failure 400 {object} domain.BaseResponse "Unable to reach SQL or Data problems" func (s *Handler) listSources(c echo.Context) error { //TODO Add top? /* @@ -61,9 +52,7 @@ func (s *Handler) listSources(c echo.Context) error { // Default way of showing all sources items, err := s.dto.ListSources(c.Request().Context(), 50) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ - Message: err.Error(), - }) + s.WriteError(c, err, http.StatusInternalServerError) } p.Payload = items @@ -102,7 +91,7 @@ func (s *Handler) listSourcesBySource(c echo.Context) error { // Shows the list by Sources.source res, err := s.dto.ListSourcesBySource(c.Request().Context(), source) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ + return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } @@ -132,14 +121,14 @@ func (s *Handler) getSources(c echo.Context) error { id := c.Param("ID") uuid, err := uuid.Parse(id) if err != nil { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: ErrUnableToParseId, }) } res, err := s.dto.GetSourceById(c.Request().Context(), uuid) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ + return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: ErrNoRecordFound, }) } @@ -170,7 +159,7 @@ func (s *Handler) GetSourceBySourceAndName(c echo.Context) error { var param domain.GetSourceBySourceAndNameParamRequest err := c.Bind(¶m) if err != nil { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: err.Error(), }) } @@ -206,7 +195,7 @@ func (s *Handler) newRedditSource(c echo.Context) error { var param domain.NewSourceParamRequest err := c.Bind(¶m) if err != nil { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: err.Error(), }) } @@ -216,12 +205,12 @@ func (s *Handler) newRedditSource(c echo.Context) error { //_tags := query["tags"][0] if param.Url == "" { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: "Url is missing a value", }) } if !strings.Contains(param.Url, "reddit.com") { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: "Invalid URL given", }) } @@ -248,7 +237,7 @@ func (s *Handler) newRedditSource(c echo.Context) error { } err = s.Db.CreateSource(c.Request().Context(), params) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ + return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } @@ -272,7 +261,7 @@ func (s *Handler) newYoutubeSource(c echo.Context) error { var param domain.NewSourceParamRequest err := c.Bind(¶m) if err != nil { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: err.Error(), }) } @@ -283,12 +272,12 @@ func (s *Handler) newYoutubeSource(c echo.Context) error { ////_tags := query["tags"][0] if param.Url == "" { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: "url is missing a value", }) } if !strings.Contains(param.Url, "youtube.com") { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: "Invalid URL", }) } @@ -318,7 +307,7 @@ func (s *Handler) newYoutubeSource(c echo.Context) error { bJson, err := json.Marshal(¶ms) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ + return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } @@ -335,7 +324,7 @@ func (s *Handler) newTwitchSource(c echo.Context) error { var param domain.NewSourceParamRequest err := c.Bind(¶m) if err != nil { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: err.Error(), }) } @@ -358,14 +347,14 @@ func (s *Handler) newTwitchSource(c echo.Context) error { } err = s.Db.CreateSource(c.Request().Context(), params) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ + return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } bJson, err := json.Marshal(¶ms) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ + return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } @@ -382,7 +371,7 @@ func (s *Handler) deleteSources(c echo.Context) error { id := c.Param("ID") uuid, err := uuid.Parse(id) if err != nil { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: err.Error(), }) } @@ -390,7 +379,7 @@ func (s *Handler) deleteSources(c echo.Context) error { // Check to make sure we can find the record _, err = s.Db.GetSourceByID(c.Request().Context(), uuid) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ + return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } @@ -398,7 +387,7 @@ func (s *Handler) deleteSources(c echo.Context) error { // Delete the record err = s.Db.DeleteSource(c.Request().Context(), uuid) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ + return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } @@ -410,7 +399,7 @@ func (s *Handler) deleteSources(c echo.Context) error { b, err := json.Marshal(p) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ + return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } @@ -427,7 +416,7 @@ func (s *Handler) disableSource(c echo.Context) error { id := c.Param("ID") uuid, err := uuid.Parse(id) if err != nil { - return c.JSON(http.StatusBadRequest, domain.ErrorResponse{ + return c.JSON(http.StatusBadRequest, domain.BaseResponse{ Message: err.Error(), }) } @@ -435,14 +424,14 @@ func (s *Handler) disableSource(c echo.Context) error { // Check to make sure we can find the record _, err = s.Db.GetSourceByID(c.Request().Context(), uuid) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ + return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } err = s.Db.DisableSource(context.Background(), uuid) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ + return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } @@ -454,7 +443,7 @@ func (s *Handler) disableSource(c echo.Context) error { b, err := json.Marshal(p) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ + return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } @@ -477,14 +466,14 @@ func (s *Handler) enableSource(c echo.Context) error { // Check to make sure we can find the record _, err = s.Db.GetSourceByID(c.Request().Context(), uuid) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ + return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } err = s.Db.EnableSource(c.Request().Context(), uuid) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ + return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } @@ -496,7 +485,7 @@ func (s *Handler) enableSource(c echo.Context) error { b, err := json.Marshal(p) if err != nil { - return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ + return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ Message: err.Error(), }) } diff --git a/internal/handler/v1/subscriptions.go b/internal/handler/v1/subscriptions.go index 7af0ccc..eff2512 100644 --- a/internal/handler/v1/subscriptions.go +++ b/internal/handler/v1/subscriptions.go @@ -3,6 +3,7 @@ package v1 import ( "context" "encoding/json" + "errors" "net/http" "git.jamestombleson.com/jtom38/newsbot-api/internal/database" @@ -44,7 +45,7 @@ func (s *Handler) ListSubscriptions(c echo.Context) error { res, err := s.dto.ListSubscriptions(c.Request().Context(), 50) if err != nil { - return s.WriteError(c, err.Error(), http.StatusBadRequest) + return s.WriteError(c, err, http.StatusBadRequest) } payload.Payload = res @@ -67,7 +68,7 @@ func (s *Handler) ListSubscriptionDetails(c echo.Context) error { res, err := s.dto.ListSubscriptionDetails(c.Request().Context(), 50) if err != nil { - return s.WriteError(c, err.Error(), http.StatusInternalServerError) + return s.WriteError(c, err, http.StatusInternalServerError) } payload.Payload = res @@ -93,18 +94,18 @@ func (s *Handler) GetSubscriptionsByDiscordId(c echo.Context) error { id := c.QueryParam("id") if id == "" { - return s.WriteError(c, ErrIdValueMissing, http.StatusBadRequest) + return s.WriteError(c, errors.New(ErrIdValueMissing), http.StatusBadRequest) } uuid, err := uuid.Parse(id) if err != nil { - return s.WriteError(c, ErrValueNotUuid, http.StatusBadRequest) + return s.WriteError(c, errors.New(ErrValueNotUuid), http.StatusBadRequest) } res, err := s.dto.ListSubscriptionsByDiscordWebhookId(context.Background(), uuid) if err != nil { - return s.WriteError(c, err.Error(), http.StatusNoContent) + return s.WriteError(c, err, http.StatusNoContent) } p.Payload = res @@ -128,17 +129,17 @@ func (s *Handler) GetSubscriptionsBySourceId(c echo.Context) error { _id := c.QueryParam("id") if _id == "" { - return s.WriteError(c, ErrIdValueMissing, http.StatusBadRequest) + return s.WriteError(c, errors.New(ErrIdValueMissing), http.StatusBadRequest) } uuid, err := uuid.Parse(_id) if err != nil { - return s.WriteError(c, err.Error(), http.StatusBadRequest) + return s.WriteError(c, err, http.StatusBadRequest) } res, err := s.dto.ListSubscriptionsBySourceId(context.Background(), uuid) if err != nil { - return s.WriteError(c, err.Error(), http.StatusNoContent) + return s.WriteError(c, err, http.StatusNoContent) } p.Payload = res @@ -158,20 +159,20 @@ func (s *Handler) newDiscordWebHookSubscription(c echo.Context) error { // Check to make we didn't get a null if discordWebHookId == "" { - return s.WriteError(c, "invalid discordWebHooksId given", http.StatusBadRequest) + return s.WriteError(c, errors.New("invalid discordWebHooksId given"), http.StatusBadRequest) } if sourceId == "" { - return s.WriteError(c, "invalid sourceID given", http.StatusBadRequest) + return s.WriteError(c, errors.New("invalid sourceID given"), http.StatusBadRequest) } // Validate they are UUID values uHook, err := uuid.Parse(discordWebHookId) if err != nil { - return s.WriteError(c, err.Error(), http.StatusBadRequest) + return s.WriteError(c, err, http.StatusBadRequest) } uSource, err := uuid.Parse(sourceId) if err != nil { - return s.WriteError(c, err.Error(), http.StatusBadRequest) + return s.WriteError(c, err, http.StatusBadRequest) } // Check if the sub already exists @@ -180,7 +181,7 @@ func (s *Handler) newDiscordWebHookSubscription(c echo.Context) error { Sourceid: uSource, }) if err == nil { - return s.WriteError(c, "a subscription already exists between these two entities", http.StatusBadRequest) + return s.WriteError(c, errors.New("a subscription already exists between these two entities"), http.StatusBadRequest) } // Does not exist, so make it. @@ -191,12 +192,12 @@ func (s *Handler) newDiscordWebHookSubscription(c echo.Context) error { } err = s.Db.CreateSubscription(context.Background(), params) if err != nil { - return s.WriteError(c, err.Error(), http.StatusInternalServerError) + return s.WriteError(c, err, http.StatusInternalServerError) } bJson, err := json.Marshal(¶ms) if err != nil { - return s.WriteError(c, err.Error(), http.StatusInternalServerError) + return s.WriteError(c, err, http.StatusInternalServerError) } return c.JSON(http.StatusOK, bJson) @@ -212,17 +213,17 @@ func (s *Handler) DeleteDiscordWebHookSubscription(c echo.Context) error { id := c.QueryParam("id") if id == "" { - return s.WriteError(c, ErrMissingSubscriptionID, http.StatusBadRequest) + return s.WriteError(c, errors.New(ErrMissingSubscriptionID), http.StatusBadRequest) } uid, err := uuid.Parse(id) if err != nil { - return s.WriteError(c, err.Error(), http.StatusBadRequest) + return s.WriteError(c, err, http.StatusBadRequest) } err = s.Db.DeleteSubscription(context.Background(), uid) if err != nil { - return s.WriteError(c, err.Error(), http.StatusInternalServerError) + return s.WriteError(c, err, http.StatusInternalServerError) } return c.JSON(http.StatusOK, nil)