package v1 import ( "context" "encoding/json" "errors" "net/http" "git.jamestombleson.com/jtom38/newsbot-api/internal/database" "git.jamestombleson.com/jtom38/newsbot-api/internal/domain/models" "github.com/google/uuid" "github.com/labstack/echo/v4" ) type ListSubscriptions struct { ApiStatusModel Payload []models.SubscriptionDto `json:"payload"` } type GetSubscription struct { ApiStatusModel 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 // @Tags Subscription // @Router /subscriptions [get] // @Success 200 {object} ListSubscriptions "ok" // @Failure 400 {object} ApiError "Unable to reach SQL." // @Failure 500 {object} ApiError "Failed to process data from SQL." func (s *Handler) ListSubscriptions(c echo.Context) error { payload := ListSubscriptions{ ApiStatusModel: ApiStatusModel{ StatusCode: http.StatusOK, Message: "OK", }, } res, err := s.dto.ListSubscriptions(c.Request().Context(), 50) if err != nil { return s.WriteError(c, err, http.StatusBadRequest) } payload.Payload = res return c.JSON(http.StatusOK, payload) } // ListSubscriptionDetails // @Summary Returns the top 50 entries with full deatils on the source and output. // @Produce application/json // @Tags Subscription // @Router /subscriptions/details [get] // @Success 200 {object} ListSubscriptionDetails "ok" func (s *Handler) ListSubscriptionDetails(c echo.Context) error { payload := ListSubscriptionDetails{ ApiStatusModel: ApiStatusModel{ StatusCode: http.StatusOK, Message: "OK", }, } res, err := s.dto.ListSubscriptionDetails(c.Request().Context(), 50) if err != nil { return s.WriteError(c, err, http.StatusInternalServerError) } payload.Payload = res return c.JSON(http.StatusOK, payload) } // GetSubscriptionsByDiscordId // @Summary Returns the top 100 entries from the queue to be processed. // @Produce application/json // @Param id query string true "id" // @Tags Subscription // @Router /subscriptions/by/discordId [get] // @Success 200 {object} ListSubscriptions "ok" // @Failure 400 {object} ApiError "Unable to reach SQL or Data problems" // @Failure 500 {object} ApiError "Data problems" func (s *Handler) GetSubscriptionsByDiscordId(c echo.Context) error { p := ListSubscriptions{ ApiStatusModel: ApiStatusModel{ StatusCode: http.StatusOK, Message: "OK", }, } id := c.QueryParam("id") if id == "" { return s.WriteError(c, errors.New(ErrIdValueMissing), http.StatusBadRequest) } uuid, err := uuid.Parse(id) if err != nil { 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, http.StatusNoContent) } p.Payload = res return c.JSON(http.StatusOK, p) } // GetSubscriptionsBySourceId // @Summary Returns the top 100 entries from the queue to be processed. // @Produce application/json // @Param id query string true "id" // @Tags Subscription // @Router /subscriptions/by/SourceId [get] // @Success 200 {object} ListSubscriptions "ok" func (s *Handler) GetSubscriptionsBySourceId(c echo.Context) error { p := ListSubscriptions{ ApiStatusModel: ApiStatusModel{ StatusCode: http.StatusOK, Message: "OK", }, } _id := c.QueryParam("id") if _id == "" { return s.WriteError(c, errors.New(ErrIdValueMissing), http.StatusBadRequest) } uuid, err := uuid.Parse(_id) if err != nil { return s.WriteError(c, err, http.StatusBadRequest) } res, err := s.dto.ListSubscriptionsBySourceId(context.Background(), uuid) if err != nil { return s.WriteError(c, err, http.StatusNoContent) } p.Payload = res return c.JSON(http.StatusOK, p) } // NewDiscordWebHookSubscription // @Summary Creates a new subscription to link a post from a Source to a DiscordWebHook. // @Param discordWebHookId query string true "discordWebHookId" // @Param sourceId query string true "sourceId" // @Tags Subscription // @Router /subscriptions/discord/webhook/new [post] func (s *Handler) newDiscordWebHookSubscription(c echo.Context) error { // Extract the values given discordWebHookId := c.QueryParam("discordWebHookId") sourceId := c.QueryParam("sourceId") // Check to make we didn't get a null if discordWebHookId == "" { return s.WriteError(c, errors.New("invalid discordWebHooksId given"), http.StatusBadRequest) } if sourceId == "" { 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, http.StatusBadRequest) } uSource, err := uuid.Parse(sourceId) if err != nil { return s.WriteError(c, err, http.StatusBadRequest) } // Check if the sub already exists _, err = s.Db.QuerySubscriptions(c.Request().Context(), database.QuerySubscriptionsParams{ Discordwebhookid: uHook, Sourceid: uSource, }) if err == nil { return s.WriteError(c, errors.New("a subscription already exists between these two entities"), http.StatusBadRequest) } // Does not exist, so make it. params := database.CreateSubscriptionParams{ ID: uuid.New(), Discordwebhookid: uHook, Sourceid: uSource, } err = s.Db.CreateSubscription(context.Background(), params) if err != nil { return s.WriteError(c, err, http.StatusInternalServerError) } bJson, err := json.Marshal(¶ms) if err != nil { return s.WriteError(c, err, http.StatusInternalServerError) } return c.JSON(http.StatusOK, bJson) } // DeleteDiscordWebHookSubscription // @Summary Removes a Discord WebHook Subscription based on the Subscription ID. // @Param id query string true "id" // @Tags Subscription // @Router /subscriptions/discord/webhook/delete [delete] func (s *Handler) DeleteDiscordWebHookSubscription(c echo.Context) error { var ErrMissingSubscriptionID string = "the request was missing a 'Id'" id := c.QueryParam("id") if id == "" { return s.WriteError(c, errors.New(ErrMissingSubscriptionID), http.StatusBadRequest) } uid, err := uuid.Parse(id) if err != nil { return s.WriteError(c, err, http.StatusBadRequest) } err = s.Db.DeleteSubscription(context.Background(), uid) if err != nil { return s.WriteError(c, err, http.StatusInternalServerError) } return c.JSON(http.StatusOK, nil) }