newsbot-portal/internal/handlers/sources.go

48 lines
1.3 KiB
Go

package handlers
import (
"net/http"
"git.jamestombleson.com/jtom38/newsbot-portal/internal/models"
"git.jamestombleson.com/jtom38/newsbot-portal/internal/views/sources"
apidomain "git.jamestombleson.com/jtom38/newsbot-api/domain"
"github.com/labstack/echo/v4"
)
func (h *Handler) ListAllSources(c echo.Context) error {
err := HasValidScope(c, apidomain.ScopeSourceRead)
if err != nil {
return RenderError(c, err)
}
resp, err := h.api.Sources.ListAll(GetJwtToken(c), 0)
if err != nil {
return RenderError(c, err)
}
return Render(c, http.StatusOK, sources.ListAll(models.ListAllSourcesViewModel{
Items: resp.Payload,
IsError: resp.IsError,
Message: resp.Message,
}))
}
func (h *Handler) AddSource(c echo.Context) error {
err := HasValidScope(c, apidomain.ScopeSourceCreate)
if err != nil {
return RenderError(c, err)
}
return Render(c, http.StatusOK, sources.Add(models.AddSourcePayloadModel{}))
}
func (h *Handler) AddSourceAfter(c echo.Context) error {
name := c.FormValue("name")
url := c.FormValue("url")
resp, err := h.api.Sources.NewRss(GetJwtToken(c), name, url, "rss")
if err != nil {
return Render(c, http.StatusOK, sources.AddAfter(err.Error(), true))
}
return Render(c, http.StatusOK, sources.AddAfter(resp.Message, false))
}