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

276 lines
7.4 KiB
Go

package v1
import (
"fmt"
"net/http"
"strings"
"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/google/uuid"
"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]
func (s *Handler) ListDiscordWebHooks(c echo.Context) error {
p := ListDiscordWebhooks{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
res, err := s.dto.ListDiscordWebHooks(c.Request().Context(), 50)
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
}
p.Payload = res
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"
func (s *Handler) GetDiscordWebHooksById(c echo.Context) error {
p := GetDiscordWebhook{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
_id := c.Param("ID")
if _id == "" {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
Message: ErrIdValueMissing,
})
}
uuid, err := uuid.Parse(_id)
if err != nil {
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.BaseResponse{
Message: ErrNoRecordFound,
})
}
p.Payload = res
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"
func (s *Handler) GetDiscordWebHooksByServerAndChannel(c echo.Context) error {
p := ListDiscordWebhooks{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
_server := c.QueryParam("server")
if _server == "" {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
Message: ErrIdValueMissing,
})
}
_channel := c.QueryParam("channel")
if _channel == "" {
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.BaseResponse{
Message: err.Error(),
})
}
p.Payload = res
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]
func (s *Handler) NewDiscordWebHook(c echo.Context) error {
_url := c.QueryParam("url")
_server := c.QueryParam("server")
_channel := c.QueryParam("channel")
if _url == "" {
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.BaseResponse{
Message: "invalid url",
})
}
if _server == "" {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
Message: "server is missing",
})
}
if _channel == "" {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
Message: "channel is missing",
})
}
params := database.CreateDiscordWebHookParams{
ID: uuid.New(),
Url: _url,
Server: _server,
Channel: _channel,
Enabled: true,
}
s.Db.CreateDiscordWebHook(c.Request().Context(), params)
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]
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.BaseResponse{
Message: err.Error(),
})
}
// Check to make sure we can find the record
_, err = s.Db.GetDiscordWebHooksByID(c.Request().Context(), uuid)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
err = s.Db.DisableDiscordWebHook(c.Request().Context(), uuid)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
Message: err.Error(),
})
}
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]
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.BaseResponse{
Message: err.Error(),
})
}
// Check to make sure we can find the record
_, err = s.Db.GetDiscordWebHooksByID(c.Request().Context(), uuid)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
err = s.Db.EnableDiscordWebHook(c.Request().Context(), uuid)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
return nil
}
// DeleteDiscordWebHook
// @Summary Deletes a record by ID.
// @Param id path string true "id"
// @Tags Discord, Webhook
// @Router /discord/webhooks/{ID} [delete]
func (s *Handler) deleteDiscordWebHook(c echo.Context) error {
//var item model.Sources = model.Sources{}
id := c.Param("ID")
uuid, err := uuid.Parse(id)
if err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
}
// Check to make sure we can find the record
_, err = s.Db.GetDiscordQueueByID(c.Request().Context(), uuid)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
// Delete the record
err = s.Db.DeleteDiscordWebHooks(c.Request().Context(), uuid)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
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]
func (s *Handler) UpdateDiscordWebHook(c echo.Context) error {
id := c.Param("ID")
uuid, err := uuid.Parse(id)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
// Check to make sure we can find the record
_, err = s.Db.GetDiscordQueueByID(c.Request().Context(), uuid)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
return nil
}