Merge upstream/main
This commit is contained in:
93
internal/db/continue_watching_test.go
Normal file
93
internal/db/continue_watching_test.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func TestGetContinueWatchingCarouselEntriesLimitsNewestFirst(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
sqlDB := newContinueWatchingTestDB(ctx, t)
|
||||
defer closeContinueWatchingTestDB(t, sqlDB)
|
||||
seedContinueWatchingRows(ctx, t, sqlDB, 30)
|
||||
|
||||
rows, err := New(sqlDB).GetContinueWatchingCarouselEntries(ctx, GetContinueWatchingCarouselEntriesParams{
|
||||
UserID: "user-1",
|
||||
Limit: 24,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("GetContinueWatchingCarouselEntries: %v", err)
|
||||
}
|
||||
if len(rows) != 24 {
|
||||
t.Fatalf("len(rows) = %d, want 24", len(rows))
|
||||
}
|
||||
if rows[0].AnimeID != 30 {
|
||||
t.Fatalf("first AnimeID = %d, want 30", rows[0].AnimeID)
|
||||
}
|
||||
if rows[len(rows)-1].AnimeID != 7 {
|
||||
t.Fatalf("last AnimeID = %d, want 7", rows[len(rows)-1].AnimeID)
|
||||
}
|
||||
}
|
||||
|
||||
func newContinueWatchingTestDB(ctx context.Context, t *testing.T) *sql.DB {
|
||||
t.Helper()
|
||||
|
||||
sqlDB, err := sql.Open("sqlite3", ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
|
||||
_, err = sqlDB.ExecContext(ctx, `
|
||||
CREATE TABLE anime (
|
||||
id INTEGER PRIMARY KEY,
|
||||
title_original TEXT NOT NULL,
|
||||
title_english TEXT,
|
||||
title_japanese TEXT,
|
||||
image_url TEXT NOT NULL,
|
||||
duration_seconds REAL
|
||||
);
|
||||
CREATE TABLE continue_watching_entry (
|
||||
id TEXT PRIMARY KEY,
|
||||
user_id TEXT NOT NULL,
|
||||
anime_id INTEGER NOT NULL REFERENCES anime(id),
|
||||
current_episode INTEGER,
|
||||
current_time_seconds REAL NOT NULL DEFAULT 0,
|
||||
duration_seconds REAL,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(user_id, anime_id)
|
||||
);`)
|
||||
if err != nil {
|
||||
closeContinueWatchingTestDB(t, sqlDB)
|
||||
t.Fatalf("create schema: %v", err)
|
||||
}
|
||||
|
||||
return sqlDB
|
||||
}
|
||||
|
||||
func closeContinueWatchingTestDB(t *testing.T, sqlDB *sql.DB) {
|
||||
t.Helper()
|
||||
|
||||
if err := sqlDB.Close(); err != nil {
|
||||
t.Errorf("close sqlite: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func seedContinueWatchingRows(ctx context.Context, t *testing.T, sqlDB *sql.DB, totalRows int) {
|
||||
t.Helper()
|
||||
|
||||
for i := 1; i <= totalRows; i++ {
|
||||
if _, err := sqlDB.ExecContext(ctx, `INSERT INTO anime (id, title_original, image_url) VALUES (?, ?, ?)`, i, fmt.Sprintf("Anime %02d", i), "image.jpg"); err != nil {
|
||||
t.Fatalf("insert anime %d: %v", i, err)
|
||||
}
|
||||
if _, err := sqlDB.ExecContext(ctx, `
|
||||
INSERT INTO continue_watching_entry (id, user_id, anime_id, current_episode, current_time_seconds, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, datetime('2026-01-01', printf('+%d minutes', ?)))`, fmt.Sprintf("cw-%02d", i), "user-1", i, i, 0, i); err != nil {
|
||||
t.Fatalf("insert continue watching %d: %v", i, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,6 +103,38 @@ type JikanCache struct {
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type RecommendationEvent struct {
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
AnimeID sql.NullInt64 `json:"anime_id"`
|
||||
EventType string `json:"event_type"`
|
||||
Source sql.NullString `json:"source"`
|
||||
MetadataJson sql.NullString `json:"metadata_json"`
|
||||
OccurredAt time.Time `json:"occurred_at"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type RecommendationImpression struct {
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
AnimeID int64 `json:"anime_id"`
|
||||
Rail string `json:"rail"`
|
||||
Position int64 `json:"position"`
|
||||
RequestID sql.NullString `json:"request_id"`
|
||||
MetadataJson sql.NullString `json:"metadata_json"`
|
||||
OccurredAt time.Time `json:"occurred_at"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type RecommendationProfileSnapshot struct {
|
||||
UserID string `json:"user_id"`
|
||||
ProfileJson string `json:"profile_json"`
|
||||
SourceWindowStart sql.NullTime `json:"source_window_start"`
|
||||
SourceWindowEnd sql.NullTime `json:"source_window_end"`
|
||||
ComputedAt time.Time `json:"computed_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
@@ -131,13 +163,15 @@ type User struct {
|
||||
}
|
||||
|
||||
type WatchListEntry struct {
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
AnimeID int64 `json:"anime_id"`
|
||||
Status string `json:"status"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
CurrentEpisode sql.NullInt64 `json:"current_episode"`
|
||||
LastEpisodeAt sql.NullTime `json:"last_episode_at"`
|
||||
CurrentTimeSeconds float64 `json:"current_time_seconds"`
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
AnimeID int64 `json:"anime_id"`
|
||||
Status string `json:"status"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
CurrentEpisode sql.NullInt64 `json:"current_episode"`
|
||||
LastEpisodeAt sql.NullTime `json:"last_episode_at"`
|
||||
CurrentTimeSeconds float64 `json:"current_time_seconds"`
|
||||
CompletedAt sql.NullTime `json:"completed_at"`
|
||||
CompletedAtEstimated bool `json:"completed_at_estimated"`
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ type Querier interface {
|
||||
GetAnime(ctx context.Context, id int64) (Anime, error)
|
||||
GetAnimeNeedingRelationSync(ctx context.Context) ([]GetAnimeNeedingRelationSyncRow, error)
|
||||
GetAuditLogsForUser(ctx context.Context, arg GetAuditLogsForUserParams) ([]AuditLog, error)
|
||||
GetContinueWatchingCarouselEntries(ctx context.Context, arg GetContinueWatchingCarouselEntriesParams) ([]GetContinueWatchingEntriesRow, error)
|
||||
GetContinueWatchingEntries(ctx context.Context, userID string) ([]GetContinueWatchingEntriesRow, error)
|
||||
GetContinueWatchingEntry(ctx context.Context, arg GetContinueWatchingEntryParams) (ContinueWatchingEntry, error)
|
||||
GetDueAnimeFetchRetries(ctx context.Context, limit int64) ([]AnimeFetchRetry, error)
|
||||
|
||||
@@ -68,12 +68,32 @@ RETURNING *;
|
||||
SELECT * FROM anime WHERE id = ? LIMIT 1;
|
||||
|
||||
-- name: UpsertWatchListEntry :one
|
||||
INSERT INTO watch_list_entry (id, user_id, anime_id, status, current_episode, current_time_seconds, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
||||
INSERT INTO watch_list_entry (id, user_id, anime_id, status, current_episode, current_time_seconds, completed_at, completed_at_estimated, updated_at)
|
||||
VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(user_id),
|
||||
sqlc.arg(anime_id),
|
||||
sqlc.arg(status),
|
||||
sqlc.arg(current_episode),
|
||||
sqlc.arg(current_time_seconds),
|
||||
CASE WHEN sqlc.arg(status) = 'completed' THEN CURRENT_TIMESTAMP END,
|
||||
0,
|
||||
CURRENT_TIMESTAMP
|
||||
)
|
||||
ON CONFLICT (user_id, anime_id) DO UPDATE SET
|
||||
status = excluded.status,
|
||||
current_episode = excluded.current_episode,
|
||||
current_time_seconds = excluded.current_time_seconds,
|
||||
completed_at = CASE
|
||||
WHEN excluded.status != 'completed' THEN NULL
|
||||
WHEN watch_list_entry.status = 'completed' THEN COALESCE(watch_list_entry.completed_at, CURRENT_TIMESTAMP)
|
||||
ELSE CURRENT_TIMESTAMP
|
||||
END,
|
||||
completed_at_estimated = CASE
|
||||
WHEN excluded.status = 'completed' AND watch_list_entry.status = 'completed'
|
||||
THEN watch_list_entry.completed_at_estimated
|
||||
ELSE 0
|
||||
END,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
RETURNING *;
|
||||
|
||||
@@ -117,6 +137,27 @@ JOIN anime a ON c.anime_id = a.id
|
||||
WHERE c.user_id = ?
|
||||
ORDER BY c.updated_at DESC;
|
||||
|
||||
-- name: GetContinueWatchingCarouselEntries :many
|
||||
SELECT
|
||||
c.id,
|
||||
c.user_id,
|
||||
c.anime_id,
|
||||
c.current_episode,
|
||||
c.current_time_seconds,
|
||||
c.duration_seconds,
|
||||
c.created_at,
|
||||
c.updated_at,
|
||||
a.title_original,
|
||||
a.title_english,
|
||||
a.title_japanese,
|
||||
a.image_url,
|
||||
a.duration_seconds as anime_duration_seconds
|
||||
FROM continue_watching_entry c
|
||||
JOIN anime a ON c.anime_id = a.id
|
||||
WHERE c.user_id = ?
|
||||
ORDER BY c.updated_at DESC
|
||||
LIMIT ?;
|
||||
|
||||
-- name: DeleteContinueWatchingEntry :exec
|
||||
DELETE FROM continue_watching_entry
|
||||
WHERE user_id = ? AND anime_id = ?;
|
||||
@@ -127,7 +168,20 @@ WHERE user_id = ? AND anime_id = ? LIMIT 1;
|
||||
|
||||
-- name: GetUserWatchList :many
|
||||
SELECT
|
||||
e.*,
|
||||
e.id,
|
||||
e.user_id,
|
||||
e.anime_id,
|
||||
e.status,
|
||||
e.created_at,
|
||||
e.updated_at,
|
||||
e.current_episode,
|
||||
e.last_episode_at,
|
||||
e.current_time_seconds,
|
||||
e.completed_at,
|
||||
e.completed_at_estimated,
|
||||
c.current_episode AS playback_current_episode,
|
||||
c.current_time_seconds AS playback_current_time_seconds,
|
||||
c.updated_at AS playback_updated_at,
|
||||
a.title_original,
|
||||
a.title_english,
|
||||
a.title_japanese,
|
||||
@@ -135,6 +189,7 @@ SELECT
|
||||
a.airing
|
||||
FROM watch_list_entry e
|
||||
JOIN anime a ON e.anime_id = a.id
|
||||
LEFT JOIN continue_watching_entry c ON c.user_id = e.user_id AND c.anime_id = e.anime_id
|
||||
WHERE e.user_id = ?
|
||||
ORDER BY e.updated_at DESC;
|
||||
|
||||
|
||||
@@ -380,6 +380,70 @@ func (q *Queries) GetAuditLogsForUser(ctx context.Context, arg GetAuditLogsForUs
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getContinueWatchingCarouselEntries = `-- name: GetContinueWatchingCarouselEntries :many
|
||||
SELECT
|
||||
c.id,
|
||||
c.user_id,
|
||||
c.anime_id,
|
||||
c.current_episode,
|
||||
c.current_time_seconds,
|
||||
c.duration_seconds,
|
||||
c.created_at,
|
||||
c.updated_at,
|
||||
a.title_original,
|
||||
a.title_english,
|
||||
a.title_japanese,
|
||||
a.image_url,
|
||||
a.duration_seconds as anime_duration_seconds
|
||||
FROM continue_watching_entry c
|
||||
JOIN anime a ON c.anime_id = a.id
|
||||
WHERE c.user_id = ?
|
||||
ORDER BY c.updated_at DESC
|
||||
LIMIT ?
|
||||
`
|
||||
|
||||
type GetContinueWatchingCarouselEntriesParams struct {
|
||||
UserID string `json:"user_id"`
|
||||
Limit int64 `json:"limit"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetContinueWatchingCarouselEntries(ctx context.Context, arg GetContinueWatchingCarouselEntriesParams) ([]GetContinueWatchingEntriesRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getContinueWatchingCarouselEntries, arg.UserID, arg.Limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetContinueWatchingEntriesRow
|
||||
for rows.Next() {
|
||||
var i GetContinueWatchingEntriesRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.UserID,
|
||||
&i.AnimeID,
|
||||
&i.CurrentEpisode,
|
||||
&i.CurrentTimeSeconds,
|
||||
&i.DurationSeconds,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.TitleOriginal,
|
||||
&i.TitleEnglish,
|
||||
&i.TitleJapanese,
|
||||
&i.ImageUrl,
|
||||
&i.AnimeDurationSeconds,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getContinueWatchingEntries = `-- name: GetContinueWatchingEntries :many
|
||||
SELECT
|
||||
c.id,
|
||||
@@ -609,6 +673,7 @@ func (q *Queries) GetJikanCacheStale(ctx context.Context, key string) (string, e
|
||||
return data, err
|
||||
}
|
||||
|
||||
|
||||
const getSession = `-- name: GetSession :one
|
||||
SELECT id, user_id, expires_at, created_at FROM session WHERE id = ? LIMIT 1
|
||||
`
|
||||
@@ -794,7 +859,20 @@ func (q *Queries) GetUserByUsername(ctx context.Context, username string) (User,
|
||||
|
||||
const getUserWatchList = `-- name: GetUserWatchList :many
|
||||
SELECT
|
||||
e.id, e.user_id, e.anime_id, e.status, e.created_at, e.updated_at, e.current_episode, e.last_episode_at, e.current_time_seconds,
|
||||
e.id,
|
||||
e.user_id,
|
||||
e.anime_id,
|
||||
e.status,
|
||||
e.created_at,
|
||||
e.updated_at,
|
||||
e.current_episode,
|
||||
e.last_episode_at,
|
||||
e.current_time_seconds,
|
||||
e.completed_at,
|
||||
e.completed_at_estimated,
|
||||
c.current_episode AS playback_current_episode,
|
||||
c.current_time_seconds AS playback_current_time_seconds,
|
||||
c.updated_at AS playback_updated_at,
|
||||
a.title_original,
|
||||
a.title_english,
|
||||
a.title_japanese,
|
||||
@@ -802,25 +880,31 @@ SELECT
|
||||
a.airing
|
||||
FROM watch_list_entry e
|
||||
JOIN anime a ON e.anime_id = a.id
|
||||
LEFT JOIN continue_watching_entry c ON c.user_id = e.user_id AND c.anime_id = e.anime_id
|
||||
WHERE e.user_id = ?
|
||||
ORDER BY e.updated_at DESC
|
||||
`
|
||||
|
||||
type GetUserWatchListRow struct {
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
AnimeID int64 `json:"anime_id"`
|
||||
Status string `json:"status"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
CurrentEpisode sql.NullInt64 `json:"current_episode"`
|
||||
LastEpisodeAt sql.NullTime `json:"last_episode_at"`
|
||||
CurrentTimeSeconds float64 `json:"current_time_seconds"`
|
||||
TitleOriginal string `json:"title_original"`
|
||||
TitleEnglish sql.NullString `json:"title_english"`
|
||||
TitleJapanese sql.NullString `json:"title_japanese"`
|
||||
ImageUrl string `json:"image_url"`
|
||||
Airing sql.NullBool `json:"airing"`
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
AnimeID int64 `json:"anime_id"`
|
||||
Status string `json:"status"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
CurrentEpisode sql.NullInt64 `json:"current_episode"`
|
||||
LastEpisodeAt sql.NullTime `json:"last_episode_at"`
|
||||
CurrentTimeSeconds float64 `json:"current_time_seconds"`
|
||||
CompletedAt sql.NullTime `json:"completed_at"`
|
||||
CompletedAtEstimated bool `json:"completed_at_estimated"`
|
||||
PlaybackCurrentEpisode sql.NullInt64 `json:"playback_current_episode"`
|
||||
PlaybackCurrentTimeSeconds sql.NullFloat64 `json:"playback_current_time_seconds"`
|
||||
PlaybackUpdatedAt sql.NullTime `json:"playback_updated_at"`
|
||||
TitleOriginal string `json:"title_original"`
|
||||
TitleEnglish sql.NullString `json:"title_english"`
|
||||
TitleJapanese sql.NullString `json:"title_japanese"`
|
||||
ImageUrl string `json:"image_url"`
|
||||
Airing sql.NullBool `json:"airing"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetUserWatchList(ctx context.Context, userID string) ([]GetUserWatchListRow, error) {
|
||||
@@ -841,6 +925,11 @@ func (q *Queries) GetUserWatchList(ctx context.Context, userID string) ([]GetUse
|
||||
&i.CurrentEpisode,
|
||||
&i.LastEpisodeAt,
|
||||
&i.CurrentTimeSeconds,
|
||||
&i.CompletedAt,
|
||||
&i.CompletedAtEstimated,
|
||||
&i.PlaybackCurrentEpisode,
|
||||
&i.PlaybackCurrentTimeSeconds,
|
||||
&i.PlaybackUpdatedAt,
|
||||
&i.TitleOriginal,
|
||||
&i.TitleEnglish,
|
||||
&i.TitleJapanese,
|
||||
@@ -861,7 +950,7 @@ func (q *Queries) GetUserWatchList(ctx context.Context, userID string) ([]GetUse
|
||||
}
|
||||
|
||||
const getWatchListEntry = `-- name: GetWatchListEntry :one
|
||||
SELECT id, user_id, anime_id, status, created_at, updated_at, current_episode, last_episode_at, current_time_seconds FROM watch_list_entry
|
||||
SELECT id, user_id, anime_id, status, created_at, updated_at, current_episode, last_episode_at, current_time_seconds, completed_at, completed_at_estimated FROM watch_list_entry
|
||||
WHERE user_id = ? AND anime_id = ? LIMIT 1
|
||||
`
|
||||
|
||||
@@ -883,13 +972,15 @@ func (q *Queries) GetWatchListEntry(ctx context.Context, arg GetWatchListEntryPa
|
||||
&i.CurrentEpisode,
|
||||
&i.LastEpisodeAt,
|
||||
&i.CurrentTimeSeconds,
|
||||
&i.CompletedAt,
|
||||
&i.CompletedAtEstimated,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getWatchingAnime = `-- name: GetWatchingAnime :many
|
||||
SELECT
|
||||
e.id, e.user_id, e.anime_id, e.status, e.created_at, e.updated_at, e.current_episode, e.last_episode_at, e.current_time_seconds,
|
||||
e.id, e.user_id, e.anime_id, e.status, e.created_at, e.updated_at, e.current_episode, e.last_episode_at, e.current_time_seconds, e.completed_at, e.completed_at_estimated,
|
||||
a.title_original,
|
||||
a.title_english,
|
||||
a.title_japanese,
|
||||
@@ -902,20 +993,22 @@ ORDER BY e.updated_at DESC
|
||||
`
|
||||
|
||||
type GetWatchingAnimeRow struct {
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
AnimeID int64 `json:"anime_id"`
|
||||
Status string `json:"status"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
CurrentEpisode sql.NullInt64 `json:"current_episode"`
|
||||
LastEpisodeAt sql.NullTime `json:"last_episode_at"`
|
||||
CurrentTimeSeconds float64 `json:"current_time_seconds"`
|
||||
TitleOriginal string `json:"title_original"`
|
||||
TitleEnglish sql.NullString `json:"title_english"`
|
||||
TitleJapanese sql.NullString `json:"title_japanese"`
|
||||
ImageUrl string `json:"image_url"`
|
||||
Airing sql.NullBool `json:"airing"`
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"user_id"`
|
||||
AnimeID int64 `json:"anime_id"`
|
||||
Status string `json:"status"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
CurrentEpisode sql.NullInt64 `json:"current_episode"`
|
||||
LastEpisodeAt sql.NullTime `json:"last_episode_at"`
|
||||
CurrentTimeSeconds float64 `json:"current_time_seconds"`
|
||||
CompletedAt sql.NullTime `json:"completed_at"`
|
||||
CompletedAtEstimated bool `json:"completed_at_estimated"`
|
||||
TitleOriginal string `json:"title_original"`
|
||||
TitleEnglish sql.NullString `json:"title_english"`
|
||||
TitleJapanese sql.NullString `json:"title_japanese"`
|
||||
ImageUrl string `json:"image_url"`
|
||||
Airing sql.NullBool `json:"airing"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetWatchingAnime(ctx context.Context, userID string) ([]GetWatchingAnimeRow, error) {
|
||||
@@ -936,6 +1029,8 @@ func (q *Queries) GetWatchingAnime(ctx context.Context, userID string) ([]GetWat
|
||||
&i.CurrentEpisode,
|
||||
&i.LastEpisodeAt,
|
||||
&i.CurrentTimeSeconds,
|
||||
&i.CompletedAt,
|
||||
&i.CompletedAtEstimated,
|
||||
&i.TitleOriginal,
|
||||
&i.TitleEnglish,
|
||||
&i.TitleJapanese,
|
||||
@@ -1299,14 +1394,34 @@ func (q *Queries) UpsertEpisodeProviderMapping(ctx context.Context, arg UpsertEp
|
||||
}
|
||||
|
||||
const upsertWatchListEntry = `-- name: UpsertWatchListEntry :one
|
||||
INSERT INTO watch_list_entry (id, user_id, anime_id, status, current_episode, current_time_seconds, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
||||
INSERT INTO watch_list_entry (id, user_id, anime_id, status, current_episode, current_time_seconds, completed_at, completed_at_estimated, updated_at)
|
||||
VALUES (
|
||||
?1,
|
||||
?2,
|
||||
?3,
|
||||
?4,
|
||||
?5,
|
||||
?6,
|
||||
CASE WHEN ?4 = 'completed' THEN CURRENT_TIMESTAMP END,
|
||||
0,
|
||||
CURRENT_TIMESTAMP
|
||||
)
|
||||
ON CONFLICT (user_id, anime_id) DO UPDATE SET
|
||||
status = excluded.status,
|
||||
current_episode = excluded.current_episode,
|
||||
current_time_seconds = excluded.current_time_seconds,
|
||||
completed_at = CASE
|
||||
WHEN excluded.status != 'completed' THEN NULL
|
||||
WHEN watch_list_entry.status = 'completed' THEN COALESCE(watch_list_entry.completed_at, CURRENT_TIMESTAMP)
|
||||
ELSE CURRENT_TIMESTAMP
|
||||
END,
|
||||
completed_at_estimated = CASE
|
||||
WHEN excluded.status = 'completed' AND watch_list_entry.status = 'completed'
|
||||
THEN watch_list_entry.completed_at_estimated
|
||||
ELSE 0
|
||||
END,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
RETURNING id, user_id, anime_id, status, created_at, updated_at, current_episode, last_episode_at, current_time_seconds
|
||||
RETURNING id, user_id, anime_id, status, created_at, updated_at, current_episode, last_episode_at, current_time_seconds, completed_at, completed_at_estimated
|
||||
`
|
||||
|
||||
type UpsertWatchListEntryParams struct {
|
||||
@@ -1338,6 +1453,8 @@ func (q *Queries) UpsertWatchListEntry(ctx context.Context, arg UpsertWatchListE
|
||||
&i.CurrentEpisode,
|
||||
&i.LastEpisodeAt,
|
||||
&i.CurrentTimeSeconds,
|
||||
&i.CompletedAt,
|
||||
&i.CompletedAtEstimated,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user