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

514 lines
14 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"
2024-04-28 19:29:49 -07:00
"strconv"
"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"
2024-04-28 19:29:49 -07:00
"git.jamestombleson.com/jtom38/newsbot-api/internal/services"
"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
2024-04-28 19:29:49 -07:00
// @Param page query string false "page number"
// @Produce application/json
// @Tags Source
// @Router /sources [get]
2024-04-28 19:29:49 -07:00
// @Success 200 {object} domain.SourcesResponse "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 {
2024-04-28 19:29:49 -07:00
resp := domain.SourcesResponse {
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
2024-04-28 19:29:49 -07:00
page, err := strconv.Atoi(c.QueryParam("page"))
if err != nil {
page = 0
}
// Default way of showing all sources
2024-04-28 19:29:49 -07:00
items, err := s.repo.Sources.List(c.Request().Context(), page, 25)
if err != nil {
s.WriteError(c, err, http.StatusInternalServerError)
}
2024-04-28 19:29:49 -07:00
resp.Payload = services.SourcesToDto(items)
return c.JSON(http.StatusOK, resp)
}
// ListSourcesBySource
// @Summary Lists the top 50 records based on the name given. Example: reddit
2024-04-28 19:29:49 -07:00
// @Param source query string true "Source Name"
// @Param page query string false "page number"
// @Produce application/json
// @Tags Source
// @Router /sources/by/source [get]
2024-04-28 19:29:49 -07:00
// @Success 200 {object} domain.SourcesResponse "ok"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
2024-04-23 22:18:07 -07:00
func (s *Handler) listSourcesBySource(c echo.Context) error {
2024-04-28 19:29:49 -07:00
resp := domain.SourcesResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
2024-04-23 22:18:07 -07:00
source := c.QueryParam("source")
2024-04-28 19:29:49 -07:00
if source == "" {
s.WriteMessage(c, fmt.Sprintf("%s source", ErrParameterMissing), http.StatusBadRequest)
}
page, err := strconv.Atoi(c.QueryParam("page"))
if err != nil {
page = 0
}
// Shows the list by Sources.source
2024-04-28 19:29:49 -07:00
items, err := s.repo.Sources.ListBySource(c.Request().Context(), page, 25, source)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: err.Error(),
})
}
2024-04-28 19:29:49 -07:00
resp.Payload = services.SourcesToDto(items)
return c.JSON(http.StatusOK, resp)
}
// GetSource
// @Summary Returns a single entity by ID
2024-04-28 19:29:49 -07:00
// @Param id path int true "uuid"
// @Produce application/json
// @Tags Source
// @Router /sources/{id} [get]
2024-04-28 19:29:49 -07:00
// @Success 200 {object} domain.SourcesResponse "ok"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
func (s *Handler) getSource(c echo.Context) error {
resp := domain.SourcesResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
2024-04-28 19:29:49 -07:00
id, err := strconv.Atoi(c.Param("ID"))
if err != nil {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
2024-04-23 22:18:07 -07:00
Message: ErrUnableToParseId,
})
}
2024-04-28 19:29:49 -07:00
item, err := s.repo.Sources.GetById(c.Request().Context(), int64(id))
if err != nil {
2024-04-28 19:29:49 -07:00
s.WriteError(c, err, http.StatusInternalServerError)
}
2024-04-28 19:29:49 -07:00
var dto []domain.SourceDto
dto = append(dto, services.SourceToDto(item))
resp.Payload = dto
return c.JSON(http.StatusOK, resp)
}
// 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]
2024-04-28 19:29:49 -07:00
// @Success 200 {object} domain.SourcesResponse "ok"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
2024-04-23 22:18:07 -07:00
func (s *Handler) GetSourceBySourceAndName(c echo.Context) error {
2024-04-28 19:29:49 -07:00
resp := domain.SourcesResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
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-28 19:29:49 -07:00
item, err := s.repo.Sources.GetBySourceAndName(c.Request().Context(), param.Source, param.Name)
if err != nil {
2024-04-23 22:18:07 -07:00
return c.JSON(http.StatusInternalServerError, err.Error())
}
2024-04-28 19:29:49 -07:00
var dto []domain.SourceDto
dto = append(dto, services.SourceToDto(item))
resp.Payload = dto
return c.JSON(http.StatusOK, resp)
}
// 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-28 19:29:49 -07:00
// @Success 200 {object} domain.SourcesResponse "ok"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
2024-04-23 22:18:07 -07:00
func (s *Handler) newRedditSource(c echo.Context) error {
2024-04-28 19:29:49 -07:00
resp := domain.SourcesResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
2024-04-23 22:18:07 -07:00
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
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",
})
}
2024-04-28 19:29:49 -07:00
tags := fmt.Sprintf("twitch, %v, %s", param.Name, param.Tags)
rows, err := s.repo.Sources.Create(c.Request().Context(), domain.SourceCollectorReddit, param.Name, param.Url, tags, true)
if err != nil {
2024-04-28 19:29:49 -07:00
s.WriteError(c, err, http.StatusInternalServerError)
}
2024-04-28 19:29:49 -07:00
if rows != 1 {
s.WriteMessage(c, ErrFailedToCreateRecord, http.StatusInternalServerError)
}
item, err := s.repo.Sources.GetBySourceAndName(c.Request().Context(), domain.SourceCollectorReddit, param.Name)
if err != nil {
2024-04-28 19:29:49 -07:00
s.WriteError(c, err, http.StatusInternalServerError)
}
2024-04-28 19:29:49 -07:00
var dto []domain.SourceDto
dto = append(dto, services.SourceToDto(item))
resp.Payload = dto
return c.JSON(http.StatusOK, resp)
}
// 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)
}
2024-04-28 19:29:49 -07:00
// NewRssSource
// @Summary Creates a new rss source to monitor.
// @Param name query string true "Site Name"
// @Param url query string true "RSS Url"
// @Tags Source
// @Router /sources/new/rss [post]
// @Success 200 {object} domain.SourcesResponse "ok"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
func (s *Handler) newRssSource(c echo.Context) error {
resp := domain.SourcesResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
var param domain.NewSourceParamRequest
err := c.Bind(&param)
if err != nil {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
Message: err.Error(),
})
}
if param.Url == "" {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
Message: "Url is missing a value",
})
}
tags := fmt.Sprintf("rss, %v, %s", param.Name, param.Tags)
rows, err := s.repo.Sources.Create(c.Request().Context(), domain.SourceCollectorRss, param.Name, param.Url, tags, true)
if err != nil {
s.WriteError(c, err, http.StatusInternalServerError)
}
if rows != 1 {
s.WriteMessage(c, ErrFailedToCreateRecord, http.StatusInternalServerError)
}
item, err := s.repo.Sources.GetBySourceAndName(c.Request().Context(), domain.SourceCollectorRss, param.Name)
if err != nil {
s.WriteError(c, err, http.StatusInternalServerError)
}
var dto []domain.SourceDto
dto = append(dto, services.SourceToDto(item))
resp.Payload = dto
return c.JSON(http.StatusOK, resp)
}
// 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.
2024-04-28 19:29:49 -07:00
// @Param id path int true "id"
// @Tags Source
// @Router /sources/{id}/disable [post]
2024-04-28 19:29:49 -07:00
// @Success 200 {object} domain.SourcesResponse "ok"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
2024-04-23 22:18:07 -07:00
func (s *Handler) disableSource(c echo.Context) error {
2024-04-28 19:29:49 -07:00
resp := domain.SourcesResponse {
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
2024-04-28 19:29:49 -07:00
id, err := strconv.Atoi(c.Param("ID"))
if err != nil {
2024-04-28 19:29:49 -07:00
s.WriteError(c, err, http.StatusBadRequest)
}
2024-04-28 19:29:49 -07:00
// Check to make sure we can find the record
_, err = s.repo.Sources.GetById(c.Request().Context(), int64(id))
if err != nil {
2024-04-28 19:29:49 -07:00
s.WriteError(c, err, http.StatusBadRequest)
}
2024-04-28 19:29:49 -07:00
_, err = s.repo.Sources.Disable(c.Request().Context(), int64(id))
if err != nil {
s.WriteError(c, err, http.StatusInternalServerError)
}
2024-04-28 19:29:49 -07:00
item, err := s.repo.Sources.GetById(c.Request().Context(), int64(id))
if err != nil {
2024-04-28 19:29:49 -07:00
s.WriteError(c, err, http.StatusInternalServerError)
}
2024-04-28 19:29:49 -07:00
var dto []domain.SourceDto
dto = append(dto, services.SourceToDto(item))
resp.Payload = dto
return c.JSON(http.StatusOK, resp)
}
// EnableSource
// @Summary Enables a source to continue processing.
// @Param id path string true "id"
// @Tags Source
// @Router /sources/{id}/enable [post]
2024-04-28 19:29:49 -07:00
// @Success 200 {object} domain.SourcesResponse "ok"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
2024-04-23 22:18:07 -07:00
func (s *Handler) enableSource(c echo.Context) error {
2024-04-28 19:29:49 -07:00
resp := domain.SourcesResponse {
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
2024-04-28 19:29:49 -07:00
id, err := strconv.Atoi(c.Param("ID"))
if err != nil {
2024-04-28 19:29:49 -07:00
s.WriteError(c, err, http.StatusBadRequest)
}
2024-04-28 19:29:49 -07:00
// Check to make sure we can find the record
_, err = s.repo.Sources.GetById(c.Request().Context(), int64(id))
if err != nil {
2024-04-28 19:29:49 -07:00
s.WriteError(c, err, http.StatusBadRequest)
}
2024-04-28 19:29:49 -07:00
_, err = s.repo.Sources.Enable(c.Request().Context(), int64(id))
if err != nil {
s.WriteError(c, err, http.StatusInternalServerError)
}
2024-04-28 19:29:49 -07:00
item, err := s.repo.Sources.GetById(c.Request().Context(), int64(id))
if err != nil {
2024-04-28 19:29:49 -07:00
s.WriteError(c, err, http.StatusInternalServerError)
}
2024-04-28 19:29:49 -07:00
var dto []domain.SourceDto
dto = append(dto, services.SourceToDto(item))
resp.Payload = dto
return c.JSON(http.StatusOK, resp)
}