chore: initial commit
This commit is contained in:
52
internal/db/sqlc/post_stats.sql.go
Normal file
52
internal/db/sqlc/post_stats.sql.go
Normal file
@@ -0,0 +1,52 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: post_stats.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/netip"
|
||||
)
|
||||
|
||||
const incrementPostStatsView = `-- name: IncrementPostStatsView :exec
|
||||
WITH visitor AS (
|
||||
INSERT INTO post_viewers (
|
||||
post_id,
|
||||
ip,
|
||||
last_viewed_at
|
||||
)
|
||||
VALUES (
|
||||
$1,
|
||||
$2,
|
||||
NOW()
|
||||
)
|
||||
ON CONFLICT (post_id, ip)
|
||||
DO UPDATE
|
||||
SET last_viewed_at = NOW()
|
||||
WHERE post_viewers.last_viewed_at < NOW() - INTERVAL '30 minutes'
|
||||
RETURNING post_id
|
||||
)
|
||||
INSERT INTO post_stats (
|
||||
post_id,
|
||||
view
|
||||
)
|
||||
SELECT
|
||||
post_id,
|
||||
1
|
||||
FROM visitor
|
||||
ON CONFLICT (post_id)
|
||||
DO UPDATE
|
||||
SET view = post_stats.view + 1
|
||||
`
|
||||
|
||||
type IncrementPostStatsViewParams struct {
|
||||
PostID int32 `json:"post_id"`
|
||||
Ip netip.Addr `json:"ip"`
|
||||
}
|
||||
|
||||
func (q *Queries) IncrementPostStatsView(ctx context.Context, arg IncrementPostStatsViewParams) error {
|
||||
_, err := q.db.Exec(ctx, incrementPostStatsView, arg.PostID, arg.Ip)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user