newsbot-api/internal/handler/v1/subscriptions.go

232 lines
6.6 KiB
Go
Raw Normal View History

2024-04-23 07:15:38 -07:00
package v1
/*
import (
"context"
"encoding/json"
"errors"
"net/http"
2024-04-23 07:15:38 -07:00
"git.jamestombleson.com/jtom38/newsbot-api/internal/database"
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain/models"
"github.com/google/uuid"
2024-04-23 22:18:07 -07:00
"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."
2024-04-23 22:18:07 -07:00
func (s *Handler) ListSubscriptions(c echo.Context) error {
payload := ListSubscriptions{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
Message: "OK",
},
}
2024-04-23 22:18:07 -07:00
res, err := s.dto.ListSubscriptions(c.Request().Context(), 50)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
payload.Payload = res
2024-04-23 22:18:07 -07:00
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"
2024-04-23 22:18:07 -07:00
func (s *Handler) ListSubscriptionDetails(c echo.Context) error {
payload := ListSubscriptionDetails{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
Message: "OK",
},
}
2024-04-23 22:18:07 -07:00
res, err := s.dto.ListSubscriptionDetails(c.Request().Context(), 50)
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
payload.Payload = res
2024-04-23 22:18:07 -07:00
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"
2024-04-23 22:18:07 -07:00
func (s *Handler) GetSubscriptionsByDiscordId(c echo.Context) error {
p := ListSubscriptions{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
Message: "OK",
},
}
2024-04-23 22:18:07 -07:00
id := c.QueryParam("id")
if id == "" {
return s.WriteError(c, errors.New(ErrIdValueMissing), http.StatusBadRequest)
}
2024-04-23 22:18:07 -07:00
uuid, err := uuid.Parse(id)
if err != nil {
return s.WriteError(c, errors.New(ErrValueNotUuid), http.StatusBadRequest)
2024-04-23 22:18:07 -07:00
}
2024-04-23 22:18:07 -07:00
res, err := s.dto.ListSubscriptionsByDiscordWebhookId(context.Background(), uuid)
if err != nil {
return s.WriteError(c, err, http.StatusNoContent)
}
p.Payload = res
2024-04-23 22:18:07 -07:00
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"
2024-04-23 22:18:07 -07:00
func (s *Handler) GetSubscriptionsBySourceId(c echo.Context) error {
p := ListSubscriptions{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
Message: "OK",
},
}
2024-04-23 22:18:07 -07:00
_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)
}
2024-04-23 22:18:07 -07:00
res, err := s.dto.ListSubscriptionsBySourceId(context.Background(), uuid)
if err != nil {
return s.WriteError(c, err, http.StatusNoContent)
}
p.Payload = res
2024-04-23 22:18:07 -07:00
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]
2024-04-23 22:18:07 -07:00
func (s *Handler) newDiscordWebHookSubscription(c echo.Context) error {
// Extract the values given
2024-04-23 22:18:07 -07:00
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
2024-04-23 22:18:07 -07:00
_, 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,
}
2024-04-23 22:18:07 -07:00
err = s.Db.CreateSubscription(context.Background(), params)
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
bJson, err := json.Marshal(&params)
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
2024-04-23 22:18:07 -07:00
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]
2024-04-23 22:18:07 -07:00
func (s *Handler) DeleteDiscordWebHookSubscription(c echo.Context) error {
var ErrMissingSubscriptionID string = "the request was missing a 'Id'"
2024-04-23 22:18:07 -07:00
id := c.QueryParam("id")
if id == "" {
return s.WriteError(c, errors.New(ErrMissingSubscriptionID), http.StatusBadRequest)
}
2024-04-23 22:18:07 -07:00
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)
}
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, nil)
}
*/