2024-05-12 10:46:00 -07:00
|
|
|
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"
|
2024-05-18 22:38:12 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/views/layout"
|
2024-05-12 10:46:00 -07:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (h *Handler) ArticlesList(c echo.Context) error {
|
2024-05-18 22:38:12 -07:00
|
|
|
_, err := ValidateJwt(c, h.config.JwtSecret, h.config.ServerAddress)
|
|
|
|
if err != nil {
|
2024-05-26 10:00:44 -07:00
|
|
|
return Render(c, http.StatusOK, layout.Error(err))
|
2024-05-18 22:38:12 -07:00
|
|
|
}
|
|
|
|
|
2024-05-12 10:46:00 -07:00
|
|
|
userToken, err := c.Cookie(domain.CookieToken)
|
|
|
|
if err != nil {
|
2024-05-18 22:38:12 -07:00
|
|
|
return Render(c, http.StatusBadRequest, layout.Error(err))
|
2024-05-12 10:46:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := h.api.Articles.List(userToken.Value, 0)
|
|
|
|
if err != nil {
|
2024-05-18 22:38:12 -07:00
|
|
|
return Render(c, http.StatusBadRequest, layout.Error(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 {
|
|
|
|
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)
|
|
|
|
|
2024-05-12 10:46:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return Render(c, http.StatusOK, articles.List(vm))
|
2024-05-18 22:38:12 -07:00
|
|
|
}
|