Update link's clicked counter when clicked

This commit is contained in:
Aaron Bieber 2024-07-08 18:29:09 -06:00
parent 446d357e26
commit d34cb39e9f
No known key found for this signature in database
6 changed files with 105 additions and 20 deletions

2
assets/main.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -320,6 +320,7 @@ select id, owner_id, created_at, url, name, clicked, logo_url, shared
from links
where owner_id = ?
or shared = true
order by clicked desc
`
func (q *Queries) GetAllLinksForOwner(ctx context.Context, ownerID int64) ([]Link, error) {
@ -555,3 +556,31 @@ func (q *Queries) GetOwner(ctx context.Context, id int64) (Owner, error) {
)
return i, err
}
const incrementLink = `-- name: IncrementLink :one
update links set
clicked = clicked + 1
where id = ?
and owner_id = ? returning id, owner_id, created_at, url, name, clicked, logo_url, shared
`
type IncrementLinkParams struct {
ID int64 `json:"id"`
OwnerID int64 `json:"owner_id"`
}
func (q *Queries) IncrementLink(ctx context.Context, arg IncrementLinkParams) (Link, error) {
row := q.db.QueryRowContext(ctx, incrementLink, arg.ID, arg.OwnerID)
var i Link
err := row.Scan(
&i.ID,
&i.OwnerID,
&i.CreatedAt,
&i.Url,
&i.Name,
&i.Clicked,
&i.LogoUrl,
&i.Shared,
)
return i, err
}

View File

@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"image"
"image/color"
"image/png"
@ -385,3 +386,21 @@ func linkDELETE(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func linkGET(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ownerID, ok := ctx.Value(ownerKey).(int64)
if !ok {
http.Error(w, http.StatusText(http.StatusUnprocessableEntity), http.StatusUnprocessableEntity)
return
}
linkID, err := strconv.Atoi(chi.URLParam(r, "linkID"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
link, err := app.queries.IncrementLink(ctx, data.IncrementLinkParams{ID: int64(linkID), OwnerID: ownerID})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
fmt.Fprintf(w, "%s", link.Url)
}

View File

@ -149,6 +149,7 @@ func main() {
r.Use(render.SetContentType(render.ContentTypeJSON))
r.Get("/", linksGET)
r.Delete("/{linkID:[0-9]+}", linkDELETE)
r.Get("/{linkID:[0-9]+}", linkGET)
r.Post("/", linksPOST)
})
router.Route("/watches", func(r chi.Router) {

View File

@ -30,7 +30,8 @@ where id = ?
select *
from links
where owner_id = ?
or shared = true;
or shared = true
order by clicked desc;
-- name: GetAllLinks :many
select *
@ -97,3 +98,9 @@ where owner_id = ?;
-- name: AddPullRequestIgnore :one
insert into pull_request_ignores (owner_id, number, repo)
values (?, ?, ?) returning *;
-- name: IncrementLink :one
update links set
clicked = clicked + 1
where id = ?
and owner_id = ? returning * ;

View File

@ -1,6 +1,7 @@
module Main exposing (..)
import Browser
import Browser.Navigation exposing (load)
import Html exposing (..)
import Html.Attributes
exposing
@ -37,7 +38,9 @@ type Msg
= AddedLink (Result Http.Error ())
| AddedWatch (Result Http.Error ())
| DeletedLink (Result Http.Error ())
| ClickedLink (Result Http.Error String)
| DeleteLink Int
| IncrementLink Int
| DeletedWatch (Result Http.Error ())
| DeleteWatch Int
| DeletedIgnore (Result Http.Error ())
@ -186,6 +189,19 @@ deleteLink linkId =
}
incrementLink : Int -> Cmd Msg
incrementLink linkId =
Http.request
{ url = "/links/" ++ String.fromInt linkId
, method = "GET"
, timeout = Nothing
, tracker = Nothing
, headers = []
, body = Http.emptyBody
, expect = Http.expectString ClickedLink
}
deleteIgnore : Int -> Cmd Msg
deleteIgnore ignoreId =
Http.request
@ -227,6 +243,9 @@ update msg model =
DeleteLink linkId ->
( model, deleteLink linkId )
IncrementLink linkId ->
( model, incrementLink linkId )
DeleteWatch watchId ->
( model, deleteWatch watchId )
@ -251,6 +270,12 @@ update msg model =
AddedLink (Ok _) ->
( { model | newlink = initialModel.newlink }, getLinks )
ClickedLink (Ok newUrl) ->
( model, load newUrl )
ClickedLink (Err _) ->
( { model | status = Errored "Server error incrementing link!" }, Cmd.none )
DeletedLink (Ok _) ->
( model, getLinks )
@ -678,6 +703,28 @@ viewLinks model =
]
viewLink : Links.Link -> Html Msg
viewLink link =
div []
[ div [ class "icon" ]
[ span [ onClick (DeleteLink link.id) ] [ text "×" ]
, a
[ onClick (IncrementLink link.id)
-- , href link.url
]
[ div
[]
[ header []
[ img [ src ("/icons/" ++ String.fromInt link.id) ] []
]
, text link.name
]
]
]
]
viewWatches : Model -> Html Msg
viewWatches model =
div []
@ -691,24 +738,6 @@ viewWatches model =
]
viewLink : Links.Link -> Html Msg
viewLink link =
div []
[ div [ class "icon" ]
[ span [ onClick (DeleteLink link.id) ] [ text "×" ]
, a [ href link.url ]
[ div
[]
[ header []
[ img [ src ("/icons/" ++ String.fromInt link.id) ] []
]
, text link.name
]
]
]
]
viewWatch : Watches.Watch -> Html Msg
viewWatch watch =
case watch.results of