diff --git a/apiclient/articles.go b/apiclient/articles.go new file mode 100644 index 0000000..5f54596 --- /dev/null +++ b/apiclient/articles.go @@ -0,0 +1,57 @@ +package apiclient + +import ( + "encoding/json" + "fmt" + "net/http" + + "git.jamestombleson.com/jtom38/newsbot-api/domain" +) + +const ( + ArticlesBaseRoute = "api/v1/articles" +) + +type Articles interface { + List(jwt string, page int) (domain.ArticleResponse, error) +} + +type articlesClient struct { + serverAddress string + client http.Client +} + +func newArticleService(serverAddress string) articlesClient { + return articlesClient{ + serverAddress: serverAddress, + client: http.Client{}, + } +} + +func (c articlesClient) List(jwt string, page int) (domain.ArticleResponse, error) { + endpoint := fmt.Sprintf("%s/%s?page=%U", c.serverAddress, ArticlesBaseRoute, page) + + req, err := http.NewRequest(http.MethodGet, endpoint, nil) + if err != nil { + return domain.ArticleResponse{}, err + } + + //req.Header.Set(HeaderContentType, ApplicationJson) + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", jwt)) + resp, err := c.client.Do(req) + if err != nil { + return domain.ArticleResponse{}, err + } + defer resp.Body.Close() + + var bind domain.ArticleResponse + decoder := json.NewDecoder(resp.Body) + err = decoder.Decode(&bind) + if err != nil { + return domain.ArticleResponse{}, err + } + + return bind, nil + + +} diff --git a/apiclient/client.go b/apiclient/client.go index e403e61..153bc38 100644 --- a/apiclient/client.go +++ b/apiclient/client.go @@ -3,14 +3,17 @@ package apiclient const ( HeaderContentType = "Content-Type" MIMEApplicationForm = "application/x-www-form-urlencoded" + ApplicationJson = "application/json" ) type ApiClient struct { - Users Users + Articles Articles + Users Users } func New(serverAddress string) ApiClient { return ApiClient{ - Users: newUserService(serverAddress), + Articles: newArticleService(serverAddress), + Users: newUserService(serverAddress), } } diff --git a/internal/handlers/articles.go b/internal/handlers/articles.go new file mode 100644 index 0000000..b3edda7 --- /dev/null +++ b/internal/handlers/articles.go @@ -0,0 +1,28 @@ +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" + "github.com/labstack/echo/v4" +) + +func (h *Handler) ArticlesList(c echo.Context) error { + userToken, err := c.Cookie(domain.CookieToken) + if err != nil { + return err + } + + resp, err := h.api.Articles.List(userToken.Value, 0) + if err != nil { + return err + } + + vm := models.ListArticlesViewModel { + Items: resp.Payload, + } + + return Render(c, http.StatusOK, articles.List(vm)) +} \ No newline at end of file diff --git a/internal/models/articles.go b/internal/models/articles.go new file mode 100644 index 0000000..a94a5e3 --- /dev/null +++ b/internal/models/articles.go @@ -0,0 +1,7 @@ +package models + +import "git.jamestombleson.com/jtom38/newsbot-api/domain" + +type ListArticlesViewModel struct { + Items []domain.ArticleDto +} \ No newline at end of file diff --git a/internal/views/articles/list.templ b/internal/views/articles/list.templ new file mode 100644 index 0000000..f26095f --- /dev/null +++ b/internal/views/articles/list.templ @@ -0,0 +1,15 @@ +package articles + +import ( + "git.jamestombleson.com/jtom38/newsbot-portal/internal/views/layout" + "git.jamestombleson.com/jtom38/newsbot-portal/internal/models" +) + +templ List(model models.ListArticlesViewModel) { + @layout.WithTemplate() { + for _, item := range model.Items { + { item.Title } + } +
hi
+ } +} \ No newline at end of file