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

554 lines
16 KiB
Go

package v1
import (
"fmt"
"net/http"
"strconv"
"strings"
"git.jamestombleson.com/jtom38/newsbot-api/domain"
"git.jamestombleson.com/jtom38/newsbot-api/internal/dtoconv"
"github.com/labstack/echo/v4"
)
// ListSources
// @Summary Lists the top 50 records
// @Param page query string false "page number"
// @Produce application/json
// @Tags Source
// @Router /v1/sources [get]
// @Success 200 {object} domain.SourcesResponse "ok"
// @Failure 400 {object} domain.BaseResponse "Unable to reach SQL or Data problems"
// @Security Bearer
func (s *Handler) listSources(c echo.Context) error {
_, err := s.ValidateJwtToken(c, domain.ScopeSourceRead)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
resp := domain.SourcesResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
page, err := strconv.Atoi(c.QueryParam("page"))
if err != nil {
page = 0
}
// Default way of showing all sources
items, err := s.repo.Sources.List(c.Request().Context(), page, 25)
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
resp.Payload = dtoconv.SourcesToDto(items)
return c.JSON(http.StatusOK, resp)
}
// ListSourcesBySource
// @Summary Lists the top 50 records based on the name given. Example: reddit
// @Param source query string true "Source Name"
// @Param page query string false "page number"
// @Produce application/json
// @Tags Source
// @Router /v1/sources/by/source [get]
// @Success 200 {object} domain.SourcesResponse "ok"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
// @Security Bearer
func (s *Handler) listSourcesBySource(c echo.Context) error {
_, err := s.ValidateJwtToken(c, domain.ScopeSourceRead)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
resp := domain.SourcesResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
source := c.QueryParam("source")
if source == "" {
return 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
items, err := s.repo.Sources.ListBySource(c.Request().Context(), page, 25, source)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
Message: err.Error(),
})
}
resp.Payload = dtoconv.SourcesToDto(items)
return c.JSON(http.StatusOK, resp)
}
// GetSource
// @Summary Returns a single entity by ID
// @Param id path int true "uuid"
// @Produce application/json
// @Tags Source
// @Router /v1/sources/{id} [get]
// @Success 200 {object} domain.SourcesResponse "ok"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
// @Security Bearer
func (s *Handler) getSource(c echo.Context) error {
_, err := s.ValidateJwtToken(c, domain.ScopeSourceRead)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
resp := domain.SourcesResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
id, err := strconv.Atoi(c.Param("ID"))
if err != nil {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
Message: ErrUnableToParseId,
})
}
item, err := s.repo.Sources.GetById(c.Request().Context(), int64(id))
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
var dto []domain.SourceDto
dto = append(dto, dtoconv.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 /v1/sources/by/sourceAndName [get]
// @Success 200 {object} domain.SourcesResponse "ok"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
// @Security Bearer
func (s *Handler) GetSourceBySourceAndName(c echo.Context) error {
_, err := s.ValidateJwtToken(c, domain.ScopeSourceRead)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
resp := domain.SourcesResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
var param domain.GetSourceBySourceAndNameParamRequest
err = c.Bind(&param)
if err != nil {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
Message: err.Error(),
})
}
item, err := s.repo.Sources.GetBySourceAndName(c.Request().Context(), param.Source, param.Name)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
var dto []domain.SourceDto
dto = append(dto, dtoconv.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 /v1/sources/new/reddit [post]
// @Success 200 {object} domain.SourcesResponse "ok"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
// @Security Bearer
func (s *Handler) newRedditSource(c echo.Context) error {
_, err := s.ValidateJwtToken(c, domain.ScopeSourceCreate)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
resp := domain.SourcesResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
var param domain.NewSourceParamRequest
err = c.Bind(&param)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
if param.Url == "" {
return s.WriteMessage(c, "url is missing", http.StatusBadRequest)
}
if !strings.Contains(param.Url, "reddit.com") {
return s.WriteMessage(c, "invalid url", http.StatusBadRequest)
}
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 {
return s.WriteError(c, err, http.StatusInternalServerError)
}
if rows != 1 {
return s.WriteMessage(c, ErrFailedToCreateRecord, http.StatusInternalServerError)
}
item, err := s.repo.Sources.GetBySourceAndName(c.Request().Context(), domain.SourceCollectorReddit, param.Name)
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
var dto []domain.SourceDto
dto = append(dto, dtoconv.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 /v1/sources/new/youtube [post]
// @Security Bearer
func (s *Handler) newYoutubeSource(c echo.Context) error {
// Validate the jwt
_, err := s.ValidateJwtToken(c, domain.ScopeSourceCreate)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
var param domain.NewSourceParamRequest
err = c.Bind(&param)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
if param.Url == "" {
return s.WriteMessage(c, "url is missing a value", http.StatusBadRequest)
}
if !strings.Contains(param.Url, "youtube.com") {
return s.WriteMessage(c, "invalid url", http.StatusBadRequest)
}
resp := domain.SourcesResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
item, err := s.repo.Sources.GetBySourceAndName(c.Request().Context(), domain.SourceCollectorYoutube, param.Name)
if err == nil {
var dto []domain.SourceDto
dto = append(dto, dtoconv.SourceToDto(item))
resp.Payload = dto
return c.JSON(http.StatusOK, resp)
}
tags := fmt.Sprintf("twitch, %v", param.Name)
rows, err := s.repo.Sources.Create(c.Request().Context(), domain.SourceCollectorYoutube, param.Name, param.Url, tags, true)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}
if rows != 1 {
return s.WriteMessage(c, ErrFailedToCreateRecord, http.StatusInternalServerError)
}
item, err = s.repo.Sources.GetBySourceAndName(c.Request().Context(), domain.SourceCollectorYoutube, param.Name)
if err == nil {
var dto []domain.SourceDto
dto = append(dto, dtoconv.SourceToDto(item))
resp.Payload = dto
return c.JSON(http.StatusOK, resp)
}
return c.JSON(http.StatusOK, resp)
}
// NewTwitchSource
// @Summary Creates a new twitch source to monitor.
// @Param name query string true "name"
// @Tags Source
// @Router /v1/sources/new/twitch [post]
// @Security Bearer
func (s *Handler) newTwitchSource(c echo.Context) error {
_, err := s.ValidateJwtToken(c, domain.ScopeSourceCreate)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
var param domain.NewSourceParamRequest
err = c.Bind(&param)
if err != nil {
return c.JSON(http.StatusBadRequest, domain.BaseResponse{
Message: err.Error(),
})
}
resp := domain.SourcesResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
tags := fmt.Sprintf("twitch, %v", param.Name)
url := fmt.Sprintf("https://twitch.tv/%v", param.Name)
// Check if the record already exists
item, err := s.repo.Sources.GetBySourceAndName(c.Request().Context(), domain.SourceCollectorTwitch, param.Name)
if err == nil {
var dto []domain.SourceDto
dto = append(dto, dtoconv.SourceToDto(item))
resp.Payload = dto
return c.JSON(http.StatusOK, resp)
}
rows, err := s.repo.Sources.Create(c.Request().Context(), domain.SourceCollectorTwitch, param.Name, url, tags, true)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.BaseResponse{
Message: err.Error(),
})
}
if rows != 1 {
return s.WriteMessage(c, ErrFailedToCreateRecord, http.StatusInternalServerError)
}
item, _ = s.repo.Sources.GetBySourceAndName(c.Request().Context(), domain.SourceCollectorTwitch, param.Name)
var dto []domain.SourceDto
dto = append(dto, dtoconv.SourceToDto(item))
resp.Payload = dto
return c.JSON(http.StatusOK, resp)
}
// 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 /v1/sources/new/rss [post]
// @Success 200 {object} domain.SourcesResponse "ok"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
// @Security Bearer
func (s *Handler) newRssSource(c echo.Context) error {
_, err := s.ValidateJwtToken(c, domain.ScopeSourceCreate)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
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 {
return s.WriteError(c, err, http.StatusInternalServerError)
}
if rows != 1 {
return s.WriteMessage(c, ErrFailedToCreateRecord, http.StatusInternalServerError)
}
item, err := s.repo.Sources.GetBySourceAndName(c.Request().Context(), domain.SourceCollectorRss, param.Name)
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
var dto []domain.SourceDto
dto = append(dto, dtoconv.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 int true "id"
// @Tags Source
// @Router /v1/sources/{id} [POST]
// @Success 200 {object} domain.SourcesResponse "ok"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
// @Security Bearer
func (s *Handler) deleteSources(c echo.Context) error {
_, err := s.ValidateJwtToken(c, domain.ScopeAll)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
id, err := strconv.Atoi(c.Param("ID"))
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
// Check to make sure we can find the record
_, err = s.repo.Sources.GetById(c.Request().Context(), int64(id))
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
// Delete the record
rows, err := s.repo.Sources.SoftDelete(c.Request().Context(), int64(id))
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
if rows != 1 {
return s.WriteMessage(c, ErrFailedToUpdateRecord, http.StatusInternalServerError)
}
// pull the record with its updated value
item, err := s.repo.Sources.GetById(c.Request().Context(), int64(id))
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
var items []domain.SourceDto
items = append(items, dtoconv.SourceToDto(item))
return c.JSON(http.StatusOK, domain.SourcesResponse{
BaseResponse: domain.BaseResponse{
Message: "OK",
},
Payload: items,
})
}
// DisableSource
// @Summary Disables a source from processing.
// @Param id path int true "id"
// @Tags Source
// @Router /v1/sources/{id}/disable [post]
// @Success 200 {object} domain.SourcesResponse "ok"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
// @Security Bearer
func (s *Handler) disableSource(c echo.Context) error {
_, err := s.ValidateJwtToken(c, domain.ScopeAll)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
resp := domain.SourcesResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
id, err := strconv.Atoi(c.Param("ID"))
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
// Check to make sure we can find the record
_, err = s.repo.Sources.GetById(c.Request().Context(), int64(id))
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
_, err = s.repo.Sources.Disable(c.Request().Context(), int64(id))
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
item, err := s.repo.Sources.GetById(c.Request().Context(), int64(id))
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
var dto []domain.SourceDto
dto = append(dto, dtoconv.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 /v1/sources/{id}/enable [post]
// @Success 200 {object} domain.SourcesResponse "ok"
// @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse
// @Security Bearer
func (s *Handler) enableSource(c echo.Context) error {
_, err := s.ValidateJwtToken(c, domain.ScopeAll)
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
resp := domain.SourcesResponse{
BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess,
},
}
id, err := strconv.Atoi(c.Param("ID"))
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
// Check to make sure we can find the record
_, err = s.repo.Sources.GetById(c.Request().Context(), int64(id))
if err != nil {
return s.WriteError(c, err, http.StatusBadRequest)
}
_, err = s.repo.Sources.Enable(c.Request().Context(), int64(id))
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
item, err := s.repo.Sources.GetById(c.Request().Context(), int64(id))
if err != nil {
return s.WriteError(c, err, http.StatusInternalServerError)
}
var dto []domain.SourceDto
dto = append(dto, dtoconv.SourceToDto(item))
resp.Payload = dto
return c.JSON(http.StatusOK, resp)
}