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

495 lines
12 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
"context"
"encoding/json"
"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 ListSources struct {
ApiStatusModel
Payload []models.SourceDto `json:"payload"`
}
type GetSource struct {
ApiStatusModel
Payload models.SourceDto `json:"payload"`
}
// ListSources
// @Summary Lists the top 50 records
// @Produce application/json
// @Tags Source
// @Router /sources [get]
// @Success 200 {object} ListSources "ok"
// @Failure 400 {object} domain.BaseResponse "Unable to reach SQL or Data problems"
2024-04-23 22:18:07 -07:00
func (s *Handler) listSources(c echo.Context) error {
//TODO Add top?
/*
top := chi.URLParam(r, "top")
topInt, err := strconv.ParseInt(top, 0, 32)
if err != nil {
panic(err)
}
res, err := s.Db.ListSources(r.Context(), int32(topInt))
*/
p := ListSources{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
Message: "OK",
},
}
// Default way of showing all sources
2024-04-23 22:18:07 -07:00
items, err := s.dto.ListSources(c.Request().Context(), 50)
if err != nil {
s.WriteError(c, err, http.StatusInternalServerError)
}
p.Payload = items
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, p)
}
// ListSourcesBySource
// @Summary Lists the top 50 records based on the name given. Example: reddit
// @Param source query string true "Source Name"
// @Produce application/json
// @Tags Source
// @Router /sources/by/source [get]
// @Success 200 {object} ListSources "ok"
// @Failure 400 {object} ApiError "Unable to query SQL."
// @Failure 500 {object} ApiError "Problems with data."
2024-04-23 22:18:07 -07:00
func (s *Handler) listSourcesBySource(c echo.Context) error {
//TODO Add top?
/*
top := chi.URLParam(r, "top")
topInt, err := strconv.ParseInt(top, 0, 32)
if err != nil {
panic(err)
}
res, err := s.Db.ListSources(r.Context(), int32(topInt))
*/
p := ListSources{
ApiStatusModel: ApiStatusModel{
StatusCode: http.StatusOK,
Message: "OK",
},
}
2024-04-23 22:18:07 -07:00
source := c.QueryParam("source")
// Shows the list by Sources.source
2024-04-23 22:18:07 -07:00
res, err := s.dto.ListSourcesBySource(c.Request().Context(), source)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
p.Payload = res
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, p)
}
// GetSource
// @Summary Returns a single entity by ID
// @Param id path string true "uuid"
// @Produce application/json
// @Tags Source
// @Router /sources/{id} [get]
// @Success 200 {object} GetSource "ok"
// @Failure 204 {object} ApiError "No record found."
// @Failure 400 {object} ApiError "Unable to query SQL."
// @Failure 500 {object} ApiError "Failed to process data from SQL."
2024-04-23 22:18:07 -07:00
func (s *Handler) getSources(c echo.Context) error {
payload := GetSource{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
2024-04-23 22:18:07 -07:00
id := c.Param("ID")
uuid, err := uuid.Parse(id)
if err != nil {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: ErrUnableToParseId,
})
}
2024-04-23 22:18:07 -07:00
res, err := s.dto.GetSourceById(c.Request().Context(), uuid)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: ErrNoRecordFound,
})
}
payload.Payload = res
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, payload)
}
// GetSourceByNameAndSource
// @Summary Returns a single entity by ID
// @Param name query string true "dadjokes"
// @Param source query string true "reddit"
// @Produce application/json
// @Tags Source
// @Router /sources/by/sourceAndName [get]
// @Success 200 {object} GetSource "ok"
// @Failure 204 {object} ApiError "No record found."
// @Failure 400 {object} ApiError "Unable to query SQL."
// @Failure 500 {object} ApiError "Failed to process data from SQL."
2024-04-23 22:18:07 -07:00
func (s *Handler) GetSourceBySourceAndName(c echo.Context) error {
p := GetSource{
ApiStatusModel: ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
},
}
2024-04-23 22:18:07 -07:00
var param domain.GetSourceBySourceAndNameParamRequest
err := c.Bind(&param)
if err != nil {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
2024-04-23 22:18:07 -07:00
//name := c.QueryParam("name")
//if name == "" {
// s.WriteError(w, "Parameter 'name' was missing in the query.", http.StatusInternalServerError)
// return c.JSON(http.bad)
//}
2024-04-23 22:18:07 -07:00
//source := query["source"][0]
//if source == "" {
// s.WriteError(w, "The parameter 'source' was missing in the query.", http.StatusInternalServerError)
// return
//}
item, err := s.dto.GetSourceByNameAndSource(c.Request().Context(), param.Name, param.Source)
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusInternalServerError, err.Error())
}
p.Payload = item
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, p)
}
// NewRedditSource
// @Summary Creates a new reddit source to monitor.
// @Param name query string true "name"
// @Param url query string true "url"
// @Tags Source
// @Router /sources/new/reddit [post]
2024-04-23 22:18:07 -07:00
func (s *Handler) newRedditSource(c echo.Context) error {
var param domain.NewSourceParamRequest
err := c.Bind(&param)
if err != nil {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
//query := r.URL.Query()
//_name := query["name"][0]
//_url := query["url"][0]
//_tags := query["tags"][0]
2024-04-23 22:18:07 -07:00
if param.Url == "" {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: "Url is missing a value",
})
}
2024-04-23 22:18:07 -07:00
if !strings.Contains(param.Url, "reddit.com") {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: "Invalid URL given",
})
}
/*
var tags string
if _tags == "" {
tags = fmt.Sprintf("twitch, %v", _name)
} else {
}
*/
2024-04-23 22:18:07 -07:00
tags := fmt.Sprintf("twitch, %v", param.Name)
params := database.CreateSourceParams{
ID: uuid.New(),
Site: "reddit",
2024-04-23 22:18:07 -07:00
Name: param.Name,
Source: "reddit",
Type: "feed",
Enabled: true,
2024-04-23 22:18:07 -07:00
Url: param.Url,
Tags: tags,
}
2024-04-23 22:18:07 -07:00
err = s.Db.CreateSource(c.Request().Context(), params)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
//s.WriteJson(w, &params)
bJson, err := json.Marshal(&params)
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 c.JSON(http.StatusOK, bJson)
}
// NewYoutubeSource
// @Summary Creates a new youtube source to monitor.
// @Param name query string true "name"
// @Param url query string true "url"
// @Tags Source
// @Router /sources/new/youtube [post]
2024-04-23 22:18:07 -07:00
func (s *Handler) newYoutubeSource(c echo.Context) error {
var param domain.NewSourceParamRequest
err := c.Bind(&param)
if err != nil {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
//query := r.URL.Query()
//_name := query["name"][0]
//_url := query["url"][0]
////_tags := query["tags"][0]
2024-04-23 22:18:07 -07:00
if param.Url == "" {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: "url is missing a value",
})
}
2024-04-23 22:18:07 -07:00
if !strings.Contains(param.Url, "youtube.com") {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: "Invalid URL",
})
}
/*
if _tags == "" {
tags = fmt.Sprintf("twitch, %v", _name)
} else {
}
*/
2024-04-23 22:18:07 -07:00
tags := fmt.Sprintf("twitch, %v", param.Name)
params := database.CreateSourceParams{
ID: uuid.New(),
Site: "youtube",
2024-04-23 22:18:07 -07:00
Name: param.Name,
Source: "youtube",
Type: "feed",
Enabled: true,
2024-04-23 22:18:07 -07:00
Url: param.Url,
Tags: tags,
}
2024-04-23 22:18:07 -07:00
err = s.Db.CreateSource(context.Background(), params)
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusInternalServerError, err.Error())
}
bJson, err := json.Marshal(&params)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, bJson)
}
// NewTwitchSource
// @Summary Creates a new twitch source to monitor.
// @Param name query string true "name"
// @Tags Source
// @Router /sources/new/twitch [post]
2024-04-23 22:18:07 -07:00
func (s *Handler) newTwitchSource(c echo.Context) error {
var param domain.NewSourceParamRequest
err := c.Bind(&param)
if err != nil {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
2024-04-23 22:18:07 -07:00
//query := r.URL.Query()
//_name := query["name"][0]
2024-04-23 22:18:07 -07:00
tags := fmt.Sprintf("twitch, %v", param.Name)
_url := fmt.Sprintf("https://twitch.tv/%v", param.Name)
params := database.CreateSourceParams{
ID: uuid.New(),
Site: "twitch",
2024-04-23 22:18:07 -07:00
Name: param.Name,
Source: "twitch",
Type: "api",
Enabled: true,
Url: _url,
Tags: tags,
}
2024-04-23 22:18:07 -07:00
err = s.Db.CreateSource(c.Request().Context(), params)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
bJson, err := json.Marshal(&params)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, bJson)
}
// DeleteSource
// @Summary Marks a source as deleted based on its ID value.
// @Param id path string true "id"
// @Tags Source
// @Router /sources/{id} [POST]
2024-04-23 22:18:07 -07:00
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.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
// Check to make sure we can find the record
2024-04-23 22:18:07 -07:00
_, err = s.Db.GetSourceByID(c.Request().Context(), uuid)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
// Delete the record
2024-04-23 22:18:07 -07:00
err = s.Db.DeleteSource(c.Request().Context(), uuid)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
p := ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
}
b, err := json.Marshal(p)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, b)
}
// DisableSource
// @Summary Disables a source from processing.
// @Param id path string true "id"
// @Tags Source
// @Router /sources/{id}/disable [post]
2024-04-23 22:18:07 -07:00
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.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
// Check to make sure we can find the record
2024-04-23 22:18:07 -07:00
_, err = s.Db.GetSourceByID(c.Request().Context(), uuid)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
2024-04-23 22:18:07 -07:00
err = s.Db.DisableSource(context.Background(), uuid)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
p := ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
}
b, err := json.Marshal(p)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, b)
}
// EnableSource
// @Summary Enables a source to continue processing.
// @Param id path string true "id"
// @Tags Source
// @Router /sources/{id}/enable [post]
2024-04-23 22:18:07 -07:00
func (s *Handler) enableSource(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, err.Error())
}
// Check to make sure we can find the record
2024-04-23 22:18:07 -07:00
_, err = s.Db.GetSourceByID(c.Request().Context(), uuid)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
2024-04-23 22:18:07 -07:00
err = s.Db.EnableSource(c.Request().Context(), uuid)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
p := ApiStatusModel{
Message: "OK",
StatusCode: http.StatusOK,
}
b, err := json.Marshal(p)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusOK, b)
}