working on adding more info to cards and sources

This commit is contained in:
James Tombleson 2024-06-02 19:55:05 -07:00
parent c873eaef3e
commit 921f6c4d44
19 changed files with 251 additions and 37 deletions

View File

@ -9,11 +9,13 @@ const (
type ApiClient struct {
Articles Articles
Users Users
Sources Sources
}
func New(serverAddress string) ApiClient {
return ApiClient{
Articles: newArticleService(serverAddress),
Users: newUserService(serverAddress),
Sources: newSourceService(serverAddress),
}
}

77
apiclient/sources.go Normal file
View File

@ -0,0 +1,77 @@
package apiclient
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"git.jamestombleson.com/jtom38/newsbot-api/domain"
)
const (
SourcesBaseRoute = "api/v1/sources"
)
type Sources interface {
ListAll(jwt string, page int) (domain.SourcesResponse, error)
GetById(jwt string, id int64) (domain.SourcesResponse, error)
}
type sourceClient struct {
serverAddress string
client http.Client
}
func newSourceService(serverAddress string) sourceClient {
return sourceClient{
serverAddress: serverAddress,
client: http.Client{},
}
}
func (c sourceClient) ListAll(jwt string, page int) (domain.SourcesResponse, error) {
var bind domain.SourcesResponse
endpoint := fmt.Sprintf("%s/%s?page=%U", c.serverAddress, SourcesBaseRoute, page)
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
return bind, err
}
req.Header.Set(HeaderContentType, ApplicationJson)
req.Header.Set(HeaderAuthorization, fmt.Sprintf("Bearer %s", jwt))
resp, err := c.client.Do(req)
if err != nil {
return bind, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&bind)
if err != nil {
return bind, err
}
if (resp.StatusCode != 200) {
return bind, errors.New(bind.Message)
}
return bind, nil
}
func (c sourceClient) GetById(jwt string, id int64) (domain.SourcesResponse, error) {
bind := domain.SourcesResponse{}
endpoint := fmt.Sprintf("%s/%s/%d", c.serverAddress, SourcesBaseRoute, id)
statusCode, err := Get(c.client, endpoint, jwt, &bind)
if err != nil {
return bind, err
}
if (statusCode != 200) {
return bind, errors.New(bind.Message)
}
return bind, nil
}

View File

@ -54,7 +54,7 @@ func PostUrlAuthorized(client http.Client, endpoint, jwtToken string, t any) err
if err != nil {
return err
}
return nil
}
@ -75,6 +75,29 @@ func PostBodyUrl(client http.Client, endpoint string, body any, t any) error {
if err != nil {
return err
}
return nil
}
func Get(client http.Client, endpoint, jwt string, t any) (int, error) {
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
return -1, err
}
req.Header.Set(HeaderContentType, ApplicationJson)
req.Header.Set(HeaderAuthorization, fmt.Sprintf("Bearer %s", jwt))
resp, err := client.Do(req)
if err != nil {
return -1, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&t)
if err != nil {
return -1, err
}
return resp.StatusCode, nil
}

3
go.mod
View File

@ -3,11 +3,10 @@ module git.jamestombleson.com/jtom38/newsbot-portal
go 1.22.1
require (
git.jamestombleson.com/jtom38/newsbot-api v0.0.0-20240510021003-4e9a17209f02
git.jamestombleson.com/jtom38/newsbot-api v0.0.0-20240603002809-9237369e5a76
github.com/a-h/templ v0.2.680
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/joho/godotenv v1.5.1
github.com/labstack/echo-jwt/v4 v4.2.0
github.com/labstack/echo/v4 v4.12.0
)

6
go.sum
View File

@ -1,5 +1,5 @@
git.jamestombleson.com/jtom38/newsbot-api v0.0.0-20240510021003-4e9a17209f02 h1:yvu0Fnpw19YUH+AXvMQxJV8mUyEXkvpZqd8HctpwMrI=
git.jamestombleson.com/jtom38/newsbot-api v0.0.0-20240510021003-4e9a17209f02/go.mod h1:A3UdJyQ/IEy3utEwJiC4nbi0ohfgrUNRLTei2iZhLLA=
git.jamestombleson.com/jtom38/newsbot-api v0.0.0-20240603002809-9237369e5a76 h1:B9t5fcfVerMjqnXXPUmYwdmUk76EoEL8x9IRehqg2c4=
git.jamestombleson.com/jtom38/newsbot-api v0.0.0-20240603002809-9237369e5a76/go.mod h1:A3UdJyQ/IEy3utEwJiC4nbi0ohfgrUNRLTei2iZhLLA=
github.com/a-h/templ v0.2.680 h1:TflYFucxp5rmOxAXB9Xy3+QHTk8s8xG9+nCT/cLzjeE=
github.com/a-h/templ v0.2.680/go.mod h1:NQGQOycaPKBxRB14DmAaeIpcGC1AOBPJEMO4ozS7m90=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@ -12,8 +12,6 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/labstack/echo-jwt/v4 v4.2.0 h1:odSISV9JgcSCuhgQSV/6Io3i7nUmfM/QkBeR5GVJj5c=
github.com/labstack/echo-jwt/v4 v4.2.0/go.mod h1:MA2RqdXdEn4/uEglx0HcUOgQSyBaTh5JcaHIan3biwU=
github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0=
github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=

View File

@ -25,9 +25,21 @@ func (h *Handler) ArticlesList(c echo.Context) error {
if err != nil {
return Render(c, http.StatusBadRequest, layout.Error(err))
}
vm := models.ListArticlesViewModel{}
vm := models.ListArticlesViewModel{
Items: resp.Payload,
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))

View File

@ -59,13 +59,15 @@ func NewServer(ctx context.Context, configs config.Configs, apiClient apiclient.
articles := router.Group("/articles")
articles.GET("", s.ArticlesList)
sources := router.Group("/sources")
sources.GET("", s.ListAllSources)
users := router.Group("/users")
users.GET("/login", s.UserLogin)
users.POST("/login", s.UserAfterLogin)
users.GET("/signup", s.UserSignUp)
users.POST("/signup", s.UserAfterSignUp)
//users.Use(echojwt.WithConfig(jwtConfig))
users.GET("/logout", s.UsersLogout)
users.GET("/profile", s.UserProfile)
s.Router = router

View File

@ -0,0 +1,34 @@
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/layout"
"git.jamestombleson.com/jtom38/newsbot-portal/internal/views/sources"
"github.com/labstack/echo/v4"
)
func (h *Handler) ListAllSources(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.Sources.ListAll(userToken.Value, 0)
if err != nil {
return Render(c, http.StatusOK, layout.Error(err))
}
return Render(c, http.StatusOK, sources.ListAll(models.ListAllSourcesViewModel{
Items: resp.Payload,
IsError: resp.IsError,
Message: resp.Message,
}))
}

View File

@ -23,6 +23,10 @@ func (h *Handler) UserAfterLogin(c echo.Context) error {
return Render(c, http.StatusBadRequest, users.AfterLogin(err.Error(), false))
}
if user == "" {
user = "admin"
}
SetCookie(c, domain.CookieToken, resp.Token, "/")
SetCookie(c, domain.CookieRefreshToken, resp.RefreshToken, "/")
SetCookie(c, domain.CookieUser, user, "/")
@ -62,3 +66,11 @@ func (h *Handler) ForceLogout(c echo.Context) error {
h.api.Users.RefreshSessionToken(GetJwtToken(c))
return nil
}
func (h *Handler) UsersLogout(c echo.Context) error {
SetCookie(c, domain.CookieUser, "", "/")
SetCookie(c, domain.CookieRefreshToken, "", "/")
SetCookie(c, domain.CookieToken, "", "/")
return Render(c, http.StatusOK, users.Logout())
}

View File

@ -3,5 +3,10 @@ package models
import "git.jamestombleson.com/jtom38/newsbot-api/domain"
type ListArticlesViewModel struct {
Items []domain.ArticleDto
}
Items []ListArticleSourceModel
}
type ListArticleSourceModel struct {
Article domain.ArticleDto
Source domain.SourceDto
}

View File

@ -0,0 +1,9 @@
package models
import "git.jamestombleson.com/jtom38/newsbot-api/domain"
type ListAllSourcesViewModel struct {
IsError bool
Message string
Items []domain.SourceDto
}

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 5.5 KiB

View File

@ -0,0 +1,4 @@
.pin-bottom {
position: absolute;
bottom: 0;
}

View File

@ -3,26 +3,15 @@ package articles
import (
"git.jamestombleson.com/jtom38/newsbot-portal/internal/views/layout"
"git.jamestombleson.com/jtom38/newsbot-portal/internal/models"
"git.jamestombleson.com/jtom38/newsbot-portal/internal/views/bulma"
)
templ List(model models.ListArticlesViewModel) {
@layout.WithTemplate() {
@filterBar()
for _, item := range model.Items {
<div class="card">
<div class="card-image">
<figure class="is-4by3">
<img src={ item.Thumbnail }/>
</figure>
</div>
<div class="card-content">
<div class="media">
<div class="media-content">
<a href={ templ.SafeURL(item.Url) }>{ item.Title }</a>
</div>
</div>
</div>
</div>
@bulma.ArticleCardWithThumbnail(item.Article.Title, item.Article.Thumbnail, item.Article.Url, item.Article.PubDate.String(), item.Source.DisplayName )
}
}
}

View File

@ -0,0 +1,22 @@
package bulma
templ ArticleCardWithThumbnail(title, thumbnailUrl, url, datePosted, sourceName string) {
<div class="card">
<div class="card-image">
<figure class="is-4by3">
<img src={ thumbnailUrl }/>
</figure>
</div>
<div class="card-content">
<div class="media">
<div class="media-content">
<a href={ templ.SafeURL(url) }>{ title }</a>
</div>
</div>
<div class="content">
{ datePosted }<br/>
{ sourceName }
</div>
</div>
</div>
}

View File

@ -1,7 +1,9 @@
package layout
templ footer() {
<!--
<div class="has-text-centered pin-botton">
<strong>Bulma</strong> by <a href="https://jgthms.com">Jeremy Thomas</a>.
</div>
-->
}

View File

@ -3,12 +3,7 @@ package layout
templ navBar() {
<nav class="navbar" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<a class="navbar-item" href="/">
<svg width="640" height="160" viewBox="0 0 640 160" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M170 132.571V27.5908C170 25.5451 170.915 23.93 172.746 22.7456C174.576 21.5612 176.729 20.969 179.206 20.969H210.377C232.019 20.969 242.84 30.4441 242.84 49.3943C242.84 62.5303 238.264 71.0902 229.112 75.074C234.603 77.2275 238.748 80.2692 241.548 84.1992C244.347 88.1292 245.747 93.8627 245.747 101.4V104.791C245.747 116.743 242.84 125.437 237.026 130.875C231.211 136.312 223.351 139.031 213.445 139.031H179.206C176.514 139.031 174.307 138.385 172.584 137.093C170.861 135.801 170 134.293 170 132.571ZM190.834 120.619H209.085C219.529 120.619 224.751 114.751 224.751 103.015V100.431C224.751 94.401 223.432 90.0404 220.794 87.3486C218.156 84.6568 214.253 83.3109 209.085 83.3109H190.834V120.619ZM190.834 66.8371H208.923C213.122 66.8371 216.326 65.5989 218.533 63.1225C220.74 60.646 221.844 57.2544 221.844 52.9475C221.844 48.7483 220.686 45.4374 218.371 43.0148C216.057 40.5922 212.853 39.3809 208.762 39.3809H190.834V66.8371ZM260.283 103.015V27.4293C260.283 25.2759 261.306 23.6608 263.351 22.5841C265.397 21.5074 267.873 20.969 270.781 20.969C273.688 20.969 276.164 21.5074 278.21 22.5841C280.256 23.6608 281.279 25.2759 281.279 27.4293V103.015C281.279 115.397 287.2 121.588 299.044 121.588C310.888 121.588 316.81 115.397 316.81 103.015V27.4293C316.81 25.2759 317.833 23.6608 319.879 22.5841C321.925 21.5074 324.401 20.969 327.308 20.969C330.215 20.969 332.692 21.5074 334.738 22.5841C336.783 23.6608 337.806 25.2759 337.806 27.4293V103.015C337.806 115.72 334.28 125.061 327.227 131.036C320.175 137.012 310.781 140 299.044 140C287.308 140 277.914 137.039 270.861 131.117C263.809 125.195 260.283 115.828 260.283 103.015ZM356.703 132.409V27.4293C356.703 25.2759 357.725 23.6608 359.771 22.5841C361.817 21.5074 364.293 20.969 367.201 20.969C370.108 20.969 372.584 21.5074 374.63 22.5841C376.676 23.6608 377.699 25.2759 377.699 27.4293V120.619H417.106C419.044 120.619 420.579 121.534 421.709 123.365C422.84 125.195 423.405 127.349 423.405 129.825C423.405 132.301 422.84 134.455 421.709 136.285C420.579 138.116 419.044 139.031 417.106 139.031H365.908C363.432 139.031 361.279 138.439 359.448 137.254C357.618 136.07 356.703 134.455 356.703 132.409ZM434.872 132.409V31.467C434.872 27.9138 435.868 25.2759 437.86 23.5532C439.852 21.8304 442.355 20.969 445.37 20.969C449.354 20.969 452.423 21.6689 454.576 23.0686C456.729 24.4684 459.098 27.4832 461.682 32.1131L481.548 68.2907L501.413 32.1131C503.997 27.4832 506.393 24.4684 508.6 23.0686C510.808 21.6689 513.903 20.969 517.887 20.969C520.902 20.969 523.405 21.8304 525.397 23.5532C527.389 25.2759 528.385 27.9138 528.385 31.467V132.409C528.385 134.455 527.335 136.07 525.236 137.254C523.136 138.439 520.686 139.031 517.887 139.031C514.98 139.031 512.503 138.439 510.458 137.254C508.412 136.07 507.389 134.455 507.389 132.409V62.961L488.493 96.5545C486.985 99.354 484.616 100.754 481.386 100.754C478.264 100.754 475.949 99.354 474.441 96.5545L455.868 61.6689V132.409C455.868 134.455 454.818 136.07 452.719 137.254C450.619 138.439 448.17 139.031 445.37 139.031C442.463 139.031 439.987 138.439 437.941 137.254C435.895 136.07 434.872 134.455 434.872 132.409ZM539.529 130.31C539.529 130.094 539.637 129.556 539.852 128.694L571.023 27.1063C571.669 24.8452 573.257 23.0956 575.787 21.8573C578.318 20.6191 581.198 20 584.428 20C587.658 20 590.565 20.6191 593.149 21.8573C595.734 23.0956 597.349 24.8452 597.995 27.1063L629.166 128.694C629.381 129.556 629.489 130.094 629.489 130.31C629.489 132.678 628.035 134.724 625.128 136.447C622.221 138.17 619.26 139.031 616.245 139.031C612.261 139.031 609.892 137.631 609.139 134.832L603.001 113.351H566.016L559.879 134.832C559.125 137.631 556.756 139.031 552.773 139.031C549.65 139.031 546.662 138.197 543.809 136.528C540.956 134.859 539.529 132.786 539.529 130.31ZM570.377 96.8775H598.479L584.428 47.2948L570.377 96.8775Z" fill="black" class="bd-svg-black"></path>
<path fill-rule="evenodd" clip-rule="evenodd" d="M0 110L10 40L50 0L100 50L70 80L110 120L50 160L0 110Z" fill="#00D1B2"></path>
</svg>
</a>
<a class="navbar-item" href="/">Newsbot</a>
<a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false" data-target="navbarBasicExample">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
@ -19,6 +14,7 @@ templ navBar() {
<div id="navbarBasicExample" class="navbar-menu">
<div class="navbar-start">
<a class="navbar-item" href="/articles">Articles</a>
<a class="navbar-item" href="/sources">Sources</a>
<a class="navbar-item">{ getUsername(ctx) }</a>
</div>
<div class="navbar-end">
@ -30,10 +26,9 @@ templ navBar() {
} else {
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">{ getUsername(ctx) }</a>
<div class="navbar-dropdown">
<a class="navbar-item" href="/users/profile" >Profile</a>
<a class="navbar-item">Logout</a>
<a class="navbar-item" href="/users/profile">Profile</a>
<a class="navbar-item" href="/users/logout">Logout</a>
</div>
</div>
}

View File

@ -0,0 +1,13 @@
package sources
import "git.jamestombleson.com/jtom38/newsbot-portal/internal/views/layout"
import "git.jamestombleson.com/jtom38/newsbot-portal/internal/models"
templ ListAll(model models.ListAllSourcesViewModel) {
@layout.WithTemplate() {
for _, item := range model.Items {
<a href={ templ.SafeURL(item.Url) } target="_blank" rel="noopener noreferrer">{ item.DisplayName } - { item.Source } </a>
<br/>
}
}
}

View File

@ -0,0 +1,10 @@
package users
import "git.jamestombleson.com/jtom38/newsbot-portal/internal/views/layout"
import "git.jamestombleson.com/jtom38/newsbot-portal/internal/views/bulma"
templ Logout() {
@layout.WithTemplate(){
@bulma.Hero("You are out of here!", "Please login again when you are ready.")
}
}