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)
|
2024-05-18 22:38:12 -07:00
|
|
|
if err != nil {
|
2024-07-07 08:01:26 -07:00
|
|
|
return RenderError(c, err)
|
2024-05-18 22:38:12 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-06-02 19:55:05 -07:00
|
|
|
vm := models.ListArticlesViewModel{}
|
2024-05-12 10:46:00 -07:00
|
|
|
|
2024-06-02 19:55:05 -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)
|
2024-06-02 19:55:05 -07:00
|
|
|
if err != nil {
|
2024-07-07 08:01:26 -07:00
|
|
|
return RenderError(c, err)
|
2024-06-02 19:55:05 -07:00
|
|
|
}
|
|
|
|
|
2024-07-07 08:01:26 -07:00
|
|
|
item := models.ListArticleSourceModel{
|
2024-06-02 19:55:05 -07:00
|
|
|
Article: article,
|
2024-07-07 08:01:26 -07:00
|
|
|
Source: source.Payload[0],
|
2024-06-02 19:55:05 -07:00
|
|
|
}
|
|
|
|
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))
|
2024-05-18 22:38:12 -07:00
|
|
|
}
|