Update link's clicked counter when clicked
This commit is contained in:
parent
446d357e26
commit
d34cb39e9f
2
assets/main.min.js
vendored
2
assets/main.min.js
vendored
File diff suppressed because one or more lines are too long
@ -320,6 +320,7 @@ select id, owner_id, created_at, url, name, clicked, logo_url, shared
|
|||||||
from links
|
from links
|
||||||
where owner_id = ?
|
where owner_id = ?
|
||||||
or shared = true
|
or shared = true
|
||||||
|
order by clicked desc
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) GetAllLinksForOwner(ctx context.Context, ownerID int64) ([]Link, error) {
|
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
|
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
|
||||||
|
}
|
||||||
|
19
handlers.go
19
handlers.go
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"image/png"
|
"image/png"
|
||||||
@ -385,3 +386,21 @@ func linkDELETE(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
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)
|
||||||
|
}
|
||||||
|
1
main.go
1
main.go
@ -149,6 +149,7 @@ func main() {
|
|||||||
r.Use(render.SetContentType(render.ContentTypeJSON))
|
r.Use(render.SetContentType(render.ContentTypeJSON))
|
||||||
r.Get("/", linksGET)
|
r.Get("/", linksGET)
|
||||||
r.Delete("/{linkID:[0-9]+}", linkDELETE)
|
r.Delete("/{linkID:[0-9]+}", linkDELETE)
|
||||||
|
r.Get("/{linkID:[0-9]+}", linkGET)
|
||||||
r.Post("/", linksPOST)
|
r.Post("/", linksPOST)
|
||||||
})
|
})
|
||||||
router.Route("/watches", func(r chi.Router) {
|
router.Route("/watches", func(r chi.Router) {
|
||||||
|
@ -30,7 +30,8 @@ where id = ?
|
|||||||
select *
|
select *
|
||||||
from links
|
from links
|
||||||
where owner_id = ?
|
where owner_id = ?
|
||||||
or shared = true;
|
or shared = true
|
||||||
|
order by clicked desc;
|
||||||
|
|
||||||
-- name: GetAllLinks :many
|
-- name: GetAllLinks :many
|
||||||
select *
|
select *
|
||||||
@ -97,3 +98,9 @@ where owner_id = ?;
|
|||||||
-- name: AddPullRequestIgnore :one
|
-- name: AddPullRequestIgnore :one
|
||||||
insert into pull_request_ignores (owner_id, number, repo)
|
insert into pull_request_ignores (owner_id, number, repo)
|
||||||
values (?, ?, ?) returning *;
|
values (?, ?, ?) returning *;
|
||||||
|
|
||||||
|
-- name: IncrementLink :one
|
||||||
|
update links set
|
||||||
|
clicked = clicked + 1
|
||||||
|
where id = ?
|
||||||
|
and owner_id = ? returning * ;
|
||||||
|
65
src/Main.elm
65
src/Main.elm
@ -1,6 +1,7 @@
|
|||||||
module Main exposing (..)
|
module Main exposing (..)
|
||||||
|
|
||||||
import Browser
|
import Browser
|
||||||
|
import Browser.Navigation exposing (load)
|
||||||
import Html exposing (..)
|
import Html exposing (..)
|
||||||
import Html.Attributes
|
import Html.Attributes
|
||||||
exposing
|
exposing
|
||||||
@ -37,7 +38,9 @@ type Msg
|
|||||||
= AddedLink (Result Http.Error ())
|
= AddedLink (Result Http.Error ())
|
||||||
| AddedWatch (Result Http.Error ())
|
| AddedWatch (Result Http.Error ())
|
||||||
| DeletedLink (Result Http.Error ())
|
| DeletedLink (Result Http.Error ())
|
||||||
|
| ClickedLink (Result Http.Error String)
|
||||||
| DeleteLink Int
|
| DeleteLink Int
|
||||||
|
| IncrementLink Int
|
||||||
| DeletedWatch (Result Http.Error ())
|
| DeletedWatch (Result Http.Error ())
|
||||||
| DeleteWatch Int
|
| DeleteWatch Int
|
||||||
| DeletedIgnore (Result Http.Error ())
|
| 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 : Int -> Cmd Msg
|
||||||
deleteIgnore ignoreId =
|
deleteIgnore ignoreId =
|
||||||
Http.request
|
Http.request
|
||||||
@ -227,6 +243,9 @@ update msg model =
|
|||||||
DeleteLink linkId ->
|
DeleteLink linkId ->
|
||||||
( model, deleteLink linkId )
|
( model, deleteLink linkId )
|
||||||
|
|
||||||
|
IncrementLink linkId ->
|
||||||
|
( model, incrementLink linkId )
|
||||||
|
|
||||||
DeleteWatch watchId ->
|
DeleteWatch watchId ->
|
||||||
( model, deleteWatch watchId )
|
( model, deleteWatch watchId )
|
||||||
|
|
||||||
@ -251,6 +270,12 @@ update msg model =
|
|||||||
AddedLink (Ok _) ->
|
AddedLink (Ok _) ->
|
||||||
( { model | newlink = initialModel.newlink }, getLinks )
|
( { model | newlink = initialModel.newlink }, getLinks )
|
||||||
|
|
||||||
|
ClickedLink (Ok newUrl) ->
|
||||||
|
( model, load newUrl )
|
||||||
|
|
||||||
|
ClickedLink (Err _) ->
|
||||||
|
( { model | status = Errored "Server error incrementing link!" }, Cmd.none )
|
||||||
|
|
||||||
DeletedLink (Ok _) ->
|
DeletedLink (Ok _) ->
|
||||||
( model, getLinks )
|
( 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 -> Html Msg
|
||||||
viewWatches model =
|
viewWatches model =
|
||||||
div []
|
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 : Watches.Watch -> Html Msg
|
||||||
viewWatch watch =
|
viewWatch watch =
|
||||||
case watch.results of
|
case watch.results of
|
||||||
|
Loading…
Reference in New Issue
Block a user