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

276 lines
7.4 KiB
Go
Raw Normal View History

2024-04-23 07:15:38 -07:00
package v1
import (
2024-04-23 22:18:07 -07:00
"fmt"
"net/http"
"strings"
2024-04-23 07:15:38 -07:00
"git.jamestombleson.com/jtom38/newsbot-api/internal/database"
2024-04-23 22:18:07 -07:00
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain"
2024-04-23 07:15:38 -07:00
"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 ListDiscordWebhooks struct {
ApiStatusModel
Payload []models.DiscordWebHooksDto `json:"payload"`
}
type GetDiscordWebhook struct {
ApiStatusModel
Payload models.DiscordWebHooksDto `json:"payload"`
}
// ListDiscordWebhooks
// @Summary Returns the top 100 entries from the queue to be processed.
// @Produce application/json
// @Tags Discord, Webhook
// @Router /discord/webhooks [get]
2024-04-23 22:18:07 -07:00
func (s *Handler) ListDiscordWebHooks(c echo.Context) error {
p := ListDiscordWebhooks{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
2024-04-23 22:18:07 -07:00
res, err := s.dto.ListDiscordWebHooks(c.Request().Context(), 50)
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusInternalServerError, err)
}
p.Payload = res
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, p)
}
// GetDiscordWebHook
// @Summary Returns the top 100 entries from the queue to be processed.
// @Produce application/json
// @Param id path string true "id"
// @Tags Discord, Webhook
// @Router /discord/webhooks/{id} [get]
// @Success 200 {object} GetDiscordWebhook "OK"
2024-04-23 22:18:07 -07:00
func (s *Handler) GetDiscordWebHooksById(c echo.Context) error {
p := GetDiscordWebhook{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
2024-04-23 22:18:07 -07:00
_id := c.Param("ID")
if _id == "" {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
Message: ErrIdValueMissing,
})
}
uuid, err := uuid.Parse(_id)
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
Message: ErrUnableToParseId,
})
}
2024-04-23 22:18:07 -07:00
res, err := s.dto.GetDiscordWebhook(c.Request().Context(), uuid)
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
Message: ErrNoRecordFound,
})
}
p.Payload = res
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, p)
}
// GetDiscordWebHookByServerAndChannel
// @Summary Returns all the known web hooks based on the Server and Channel given.
// @Produce application/json
// @Param server query string true "Fancy Server"
// @Param channel query string true "memes"
// @Tags Discord, Webhook
// @Router /discord/webhooks/by/serverAndChannel [get]
// @Success 200 {object} ListDiscordWebhooks "OK"
2024-04-23 22:18:07 -07:00
func (s *Handler) GetDiscordWebHooksByServerAndChannel(c echo.Context) error {
p := ListDiscordWebhooks{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
2024-04-23 22:18:07 -07:00
_server := c.QueryParam("server")
if _server == "" {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
Message: ErrIdValueMissing,
})
}
2024-04-23 22:18:07 -07:00
_channel := c.QueryParam("channel")
if _channel == "" {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
Message: fmt.Sprintf("%s channel", ErrParameterMissing),
})
}
2024-04-23 22:18:07 -07:00
res, err := s.dto.GetDiscordWebHookByServerAndChannel(c.Request().Context(), _server, _channel)
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{
Message: err.Error(),
})
}
p.Payload = res
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, p)
}
// NewDiscordWebHook
// @Summary Creates a new record for a discord web hook to post data to.
// @Param url query string true "url"
// @Param server query string true "Server name"
// @Param channel query string true "Channel name"
// @Tags Discord, Webhook
// @Router /discord/webhooks/new [post]
2024-04-23 22:18:07 -07:00
func (s *Handler) NewDiscordWebHook(c echo.Context) error {
_url := c.QueryParam("url")
_server := c.QueryParam("server")
_channel := c.QueryParam("channel")
if _url == "" {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
Message: "url is missing a value",
})
}
if !strings.Contains(_url, "discord.com/api/webhooks") {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
Message: "invalid url",
})
}
if _server == "" {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
Message: "server is missing",
})
}
if _channel == "" {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
Message: "channel is missing",
})
}
params := database.CreateDiscordWebHookParams{
ID: uuid.New(),
Url: _url,
Server: _server,
Channel: _channel,
Enabled: true,
}
2024-04-23 22:18:07 -07:00
s.Db.CreateDiscordWebHook(c.Request().Context(), params)
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, params)
}
// DisableDiscordWebHooks
// @Summary Disables a Webhook from being used.
// @Param id path string true "id"
// @Tags Discord, Webhook
// @Router /discord/webhooks/{ID}/disable [post]
2024-04-23 22:18:07 -07:00
func (s *Handler) disableDiscordWebHook(c echo.Context) error {
id := c.Param("ID")
uuid, err := uuid.Parse(id)
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
Message: err.Error(),
})
}
// Check to make sure we can find the record
2024-04-23 22:18:07 -07:00
_, err = s.Db.GetDiscordWebHooksByID(c.Request().Context(), uuid)
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusInternalServerError, err.Error())
}
2024-04-23 22:18:07 -07:00
err = s.Db.DisableDiscordWebHook(c.Request().Context(), uuid)
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{
Message: err.Error(),
})
}
2024-04-23 22:18:07 -07:00
return nil
}
// EnableDiscordWebHook
// @Summary Enables a source to continue processing.
// @Param id path string true "id"
// @Tags Discord, Webhook
// @Router /discord/webhooks/{ID}/enable [post]
2024-04-23 22:18:07 -07:00
func (s *Handler) enableDiscordWebHook(c echo.Context) error {
id := c.Param("ID")
uuid, err := uuid.Parse(id)
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
Message: err.Error(),
})
}
// Check to make sure we can find the record
2024-04-23 22:18:07 -07:00
_, err = s.Db.GetDiscordWebHooksByID(c.Request().Context(), uuid)
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusInternalServerError, err.Error())
}
2024-04-23 22:18:07 -07:00
err = s.Db.EnableDiscordWebHook(c.Request().Context(), uuid)
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusInternalServerError, err.Error())
}
2024-04-23 22:18:07 -07:00
return nil
}
// DeleteDiscordWebHook
// @Summary Deletes a record by ID.
// @Param id path string true "id"
// @Tags Discord, Webhook
// @Router /discord/webhooks/{ID} [delete]
2024-04-23 22:18:07 -07:00
func (s *Handler) deleteDiscordWebHook(c echo.Context) error {
//var item model.Sources = model.Sources{}
2024-04-23 22:18:07 -07:00
id := c.Param("ID")
uuid, err := uuid.Parse(id)
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusBadRequest, err.Error())
}
// Check to make sure we can find the record
2024-04-23 22:18:07 -07:00
_, err = s.Db.GetDiscordQueueByID(c.Request().Context(), uuid)
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusInternalServerError, err.Error())
}
// Delete the record
2024-04-23 22:18:07 -07:00
err = s.Db.DeleteDiscordWebHooks(c.Request().Context(), uuid)
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusInternalServerError, err.Error())
}
2024-04-23 22:18:07 -07:00
return nil
}
// UpdateDiscordWebHook
// @Summary Updates a valid discord webhook ID based on the body given.
// @Param id path string true "id"
// @Tags Discord, Webhook
// @Router /discord/webhooks/{id} [patch]
2024-04-23 22:18:07 -07:00
func (s *Handler) UpdateDiscordWebHook(c echo.Context) error {
id := c.Param("ID")
uuid, err := uuid.Parse(id)
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusInternalServerError, err.Error())
}
// Check to make sure we can find the record
2024-04-23 22:18:07 -07:00
_, err = s.Db.GetDiscordQueueByID(c.Request().Context(), uuid)
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusInternalServerError, err.Error())
}
2024-04-23 22:18:07 -07:00
return nil
}