newsbot-portal/internal/handlers/articles.go

41 lines
943 B
Go
Raw Normal View History

2024-05-12 10:46:00 -07:00
package handlers
import (
"net/http"
2024-07-07 08:01:26 -07:00
apidomain "git.jamestombleson.com/jtom38/newsbot-api/domain"
2024-05-12 10:46:00 -07:00
"git.jamestombleson.com/jtom38/newsbot-portal/internal/models"
"git.jamestombleson.com/jtom38/newsbot-portal/internal/views/articles"
"github.com/labstack/echo/v4"
)
func (h *Handler) ArticlesList(c echo.Context) error {
2024-07-07 08:01:26 -07:00
err := HasValidScope(c, apidomain.ScopeArticleRead)
if err != nil {
2024-07-07 08:01:26 -07:00
return RenderError(c, err)
}
2024-07-07 08:01:26 -07:00
resp, err := h.api.Articles.List(GetJwtToken(c), 0)
2024-05-12 10:46:00 -07:00
if err != nil {
2024-07-07 08:01:26 -07:00
return RenderError(c, err)
2024-05-12 10:46:00 -07:00
}
vm := models.ListArticlesViewModel{}
2024-05-12 10:46:00 -07:00
for _, article := range resp.Payload {
2024-07-07 08:01:26 -07:00
source, err := h.api.Sources.GetById(GetJwtToken(c), article.SourceID)
if err != nil {
2024-07-07 08:01:26 -07:00
return RenderError(c, err)
}
2024-07-07 08:01:26 -07:00
item := models.ListArticleSourceModel{
Article: article,
2024-07-07 08:01:26 -07:00
Source: source.Payload[0],
}
vm.Items = append(vm.Items, item)
2024-07-07 08:01:26 -07:00
2024-05-12 10:46:00 -07:00
}
2024-07-13 10:39:05 -07:00
return Render(c, http.StatusOK, articles.ListArticlesTable(vm))
}