47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/domain"
|
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/models"
|
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/views/articles"
|
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/views/layout"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
func (h *Handler) ArticlesList(c echo.Context) error {
|
|
_, err := ValidateJwt(c, h.config.JwtSecret, h.config.ServerAddress)
|
|
if err != nil {
|
|
return Render(c, http.StatusOK, layout.Error(err))
|
|
}
|
|
|
|
userToken, err := c.Cookie(domain.CookieToken)
|
|
if err != nil {
|
|
return Render(c, http.StatusBadRequest, layout.Error(err))
|
|
}
|
|
|
|
resp, err := h.api.Articles.List(userToken.Value, 0)
|
|
if err != nil {
|
|
return Render(c, http.StatusBadRequest, layout.Error(err))
|
|
}
|
|
|
|
vm := models.ListArticlesViewModel{}
|
|
|
|
for _, article := range resp.Payload {
|
|
source, err := h.api.Sources.GetById(userToken.Value, article.SourceID)
|
|
if err != nil {
|
|
return Render(c, http.StatusBadRequest, layout.Error(err))
|
|
}
|
|
|
|
item := models.ListArticleSourceModel {
|
|
Article: article,
|
|
Source: source.Payload[0],
|
|
}
|
|
vm.Items = append(vm.Items, item)
|
|
|
|
}
|
|
|
|
return Render(c, http.StatusOK, articles.List(vm))
|
|
}
|