refactor: general architectural cleanup and bug fixes

This commit is contained in:
2026-05-13 11:20:49 +02:00
parent 1b88c4597c
commit 345853406c
34 changed files with 274 additions and 102 deletions

View File

@@ -1,7 +1,6 @@
package main package main
import ( import (
"log"
"mal/internal/app" "mal/internal/app"
"github.com/joho/godotenv" "github.com/joho/godotenv"

View File

@@ -25,7 +25,7 @@ type Client struct {
lastReqTime time.Time // rate limiting: last request timestamp lastReqTime time.Time // rate limiting: last request timestamp
} }
func NewClient(db db.Querier) *Client { func NewClient(queries *db.Queries) *Client {
return &Client{ return &Client{
httpClient: &http.Client{ httpClient: &http.Client{
Timeout: 10 * time.Second, Timeout: 10 * time.Second,
@@ -37,7 +37,7 @@ func NewClient(db db.Querier) *Client {
}, },
}, },
baseURL: "https://api.jikan.moe/v4", baseURL: "https://api.jikan.moe/v4",
db: db, db: queries,
retrySignal: make(chan struct{}, 1), retrySignal: make(chan struct{}, 1),
} }
} }

View File

@@ -1,8 +1,6 @@
package jikan package jikan
import ( import (
"mal/internal/db"
"go.uber.org/fx" "go.uber.org/fx"
) )

View File

@@ -1,28 +1,29 @@
package handler package handler
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"mal/internal/domain" "mal/internal/domain"
"mal/internal/server"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type AnimeHandler struct { type AnimeHandler struct {
svc domain.AnimeService svc domain.AnimeService
watchlistSvc domain.WatchlistService
} }
func NewAnimeHandler(svc domain.AnimeService) *AnimeHandler { func NewAnimeHandler(svc domain.AnimeService, watchlistSvc domain.WatchlistService) *AnimeHandler {
return &AnimeHandler{svc: svc} return &AnimeHandler{
svc: svc,
watchlistSvc: watchlistSvc,
}
} }
func (h *AnimeHandler) Register(r *gin.Engine) { func (h *AnimeHandler) Register(r *gin.Engine) {
log.Println("Registering anime routes")
r.GET("/", h.HandleCatalog) r.GET("/", h.HandleCatalog)
r.GET("/api/catalog/airing", h.HandleCatalogAiring) r.GET("/api/catalog/airing", h.HandleCatalogAiring)
r.GET("/api/catalog/popular", h.HandleCatalogPopular) r.GET("/api/catalog/popular", h.HandleCatalogPopular)
@@ -39,8 +40,19 @@ func (h *AnimeHandler) Register(r *gin.Engine) {
} }
func (h *AnimeHandler) HandleCatalog(c *gin.Context) { func (h *AnimeHandler) HandleCatalog(c *gin.Context) {
user, _ := c.Get("User")
watchlistMap := make(map[int]bool)
if u, ok := user.(*domain.User); ok {
entries, _ := h.watchlistSvc.GetWatchlist(c.Request.Context(), u.ID)
for _, e := range entries {
watchlistMap[int(e.AnimeID)] = true
}
}
c.HTML(http.StatusOK, "index.gohtml", gin.H{ c.HTML(http.StatusOK, "index.gohtml", gin.H{
"CurrentPath": "/", "CurrentPath": "/",
"User": user,
"WatchlistMap": watchlistMap,
}) })
} }
@@ -57,21 +69,36 @@ func (h *AnimeHandler) HandleCatalogContinue(c *gin.Context) {
} }
func (h *AnimeHandler) renderCatalogSection(c *gin.Context, section string) { func (h *AnimeHandler) renderCatalogSection(c *gin.Context, section string) {
userID := "" // TODO: get from auth context user, _ := c.Get("User")
userID := ""
if u, ok := user.(*domain.User); ok {
userID = u.ID
}
data, err := h.svc.GetCatalogSection(c.Request.Context(), userID, section) data, err := h.svc.GetCatalogSection(c.Request.Context(), userID, section)
if err != nil { if err != nil {
log.Printf("catalog %s error: %v", section, err) log.Printf("catalog %s error: %v", section, err)
return return
} }
watchlistMap := make(map[int]bool)
if userID != "" {
entries, _ := h.watchlistSvc.GetWatchlist(c.Request.Context(), userID)
for _, e := range entries {
watchlistMap[int(e.AnimeID)] = true
}
}
data["Section"] = section data["Section"] = section
data["_fragment"] = "catalog_section" data["_fragment"] = "catalog_section"
data["WatchlistMap"] = watchlistMap
c.HTML(http.StatusOK, "index.gohtml", data) c.HTML(http.StatusOK, "index.gohtml", data)
} }
func (h *AnimeHandler) HandleDiscover(c *gin.Context) { func (h *AnimeHandler) HandleDiscover(c *gin.Context) {
user, _ := c.Get("User")
c.HTML(http.StatusOK, "discover.gohtml", gin.H{ c.HTML(http.StatusOK, "discover.gohtml", gin.H{
"CurrentPath": "/discover", "CurrentPath": "/discover",
"User": user,
}) })
} }
@@ -88,15 +115,28 @@ func (h *AnimeHandler) HandleDiscoverTop(c *gin.Context) {
} }
func (h *AnimeHandler) renderDiscoverSection(c *gin.Context, section string) { func (h *AnimeHandler) renderDiscoverSection(c *gin.Context, section string) {
userID := "" // TODO: get from auth context user, _ := c.Get("User")
userID := ""
if u, ok := user.(*domain.User); ok {
userID = u.ID
}
data, err := h.svc.GetDiscoverSection(c.Request.Context(), userID, section) data, err := h.svc.GetDiscoverSection(c.Request.Context(), userID, section)
if err != nil { if err != nil {
log.Printf("discover %s error: %v", section, err) log.Printf("discover %s error: %v", section, err)
return return
} }
watchlistMap := make(map[int]bool)
if userID != "" {
entries, _ := h.watchlistSvc.GetWatchlist(c.Request.Context(), userID)
for _, e := range entries {
watchlistMap[int(e.AnimeID)] = true
}
}
data["Section"] = section data["Section"] = section
data["_fragment"] = "discover_section" data["_fragment"] = "discover_section"
data["WatchlistMap"] = watchlistMap
c.HTML(http.StatusOK, "discover.gohtml", data) c.HTML(http.StatusOK, "discover.gohtml", data)
} }
@@ -145,19 +185,30 @@ func (h *AnimeHandler) HandleBrowse(c *gin.Context) {
genresList, _ := h.svc.GetGenres(c.Request.Context()) genresList, _ := h.svc.GetGenres(c.Request.Context())
user, _ := c.Get("User")
watchlistMap := make(map[int]bool)
if u, ok := user.(*domain.User); ok {
entries, _ := h.watchlistSvc.GetWatchlist(c.Request.Context(), u.ID)
for _, e := range entries {
watchlistMap[int(e.AnimeID)] = true
}
}
c.HTML(http.StatusOK, "browse.gohtml", gin.H{ c.HTML(http.StatusOK, "browse.gohtml", gin.H{
"CurrentPath": "/browse", "CurrentPath": "/browse",
"Query": q, "Query": q,
"Type": animeType, "Type": animeType,
"Status": status, "Status": status,
"OrderBy": orderBy, "OrderBy": orderBy,
"Sort": sort, "Sort": sort,
"Genres": genres, "Genres": genres,
"SFW": sfw, "SFW": sfw,
"GenresList": genresList, "GenresList": genresList,
"Animes": res.Animes, "Animes": res.Animes,
"HasNextPage": res.HasNextPage, "HasNextPage": res.HasNextPage,
"NextPage": page + 1, "NextPage": page + 1,
"User": user,
"WatchlistMap": watchlistMap,
}) })
} }
@@ -189,7 +240,7 @@ func (h *AnimeHandler) HandleAnimeDetails(c *gin.Context) {
c.HTML(http.StatusOK, "anime.gohtml", gin.H{ c.HTML(http.StatusOK, "anime.gohtml", gin.H{
"_fragment": tplName, "_fragment": tplName,
"Data": data, "Items": data,
}) })
return return
} }
@@ -200,9 +251,11 @@ func (h *AnimeHandler) HandleAnimeDetails(c *gin.Context) {
return return
} }
user, _ := c.Get("User")
c.HTML(http.StatusOK, "anime.gohtml", gin.H{ c.HTML(http.StatusOK, "anime.gohtml", gin.H{
"Anime": anime, "Anime": anime,
"CurrentPath": fmt.Sprintf("/anime/%d", id), "CurrentPath": fmt.Sprintf("/anime/%d", id),
"User": user,
}) })
} }
@@ -213,16 +266,31 @@ func (h *AnimeHandler) HandleHTMLWatchOrder(c *gin.Context) {
return return
} }
user, _ := c.Get("User")
userID := ""
if u, ok := user.(*domain.User); ok {
userID = u.ID
}
relations, err := h.svc.GetRelations(c.Request.Context(), id) relations, err := h.svc.GetRelations(c.Request.Context(), id)
if err != nil { if err != nil {
c.Status(http.StatusInternalServerError) c.Status(http.StatusInternalServerError)
return return
} }
watchlistMap := make(map[int]bool)
if userID != "" {
entries, _ := h.watchlistSvc.GetWatchlist(c.Request.Context(), userID)
for _, e := range entries {
watchlistMap[int(e.AnimeID)] = true
}
}
c.HTML(http.StatusOK, "anime.gohtml", gin.H{ c.HTML(http.StatusOK, "anime.gohtml", gin.H{
"_fragment": "watch_order", "_fragment": "watch_order",
"Relations": relations, "Relations": relations,
"AnimeID": id, "AnimeID": id,
"WatchlistMap": watchlistMap,
}) })
} }
@@ -239,20 +307,31 @@ func (h *AnimeHandler) HandleQuickSearch(c *gin.Context) {
return return
} }
user, _ := c.Get("User")
watchlistMap := make(map[int]bool)
if u, ok := user.(*domain.User); ok {
entries, _ := h.watchlistSvc.GetWatchlist(c.Request.Context(), u.ID)
for _, e := range entries {
watchlistMap[int(e.AnimeID)] = true
}
}
type quickSearchResult struct { type quickSearchResult struct {
ID int `json:"id"` ID int `json:"id"`
Title string `json:"title"` Title string `json:"title"`
Type string `json:"type"` Type string `json:"type"`
Image string `json:"image"` Image string `json:"image"`
InWatchlist bool `json:"in_watchlist"`
} }
output := make([]quickSearchResult, len(res.Animes)) output := make([]quickSearchResult, len(res.Animes))
for i, anime := range res.Animes { for i, anime := range res.Animes {
output[i] = quickSearchResult{ output[i] = quickSearchResult{
ID: anime.MalID, ID: anime.MalID,
Title: anime.DisplayTitle(), Title: anime.DisplayTitle(),
Type: anime.Type, Type: anime.Type,
Image: anime.ImageURL(), Image: anime.ImageURL(),
InWatchlist: watchlistMap[anime.MalID],
} }
} }
c.JSON(http.StatusOK, output) c.JSON(http.StatusOK, output)
@@ -264,5 +343,21 @@ func (h *AnimeHandler) HandleRandomAnime(c *gin.Context) {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch random anime"}) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch random anime"})
return return
} }
c.JSON(http.StatusOK, gin.H{"data": anime})
user, _ := c.Get("User")
inWatchlist := false
if u, ok := user.(*domain.User); ok {
entries, _ := h.watchlistSvc.GetWatchlist(c.Request.Context(), u.ID)
for _, e := range entries {
if int(e.AnimeID) == anime.MalID {
inWatchlist = true
break
}
}
}
c.JSON(http.StatusOK, gin.H{
"data": anime,
"in_watchlist": inWatchlist,
})
} }

View File

@@ -128,7 +128,7 @@ func (s *animeService) GetAnimeByID(ctx context.Context, id int) (domain.Anime,
return s.jikan.GetAnimeByID(ctx, id) return s.jikan.GetAnimeByID(ctx, id)
} }
func (s *animeService) SearchAdvanced(ctx context.Context, q, animeType, status, orderBy, sort string, genres []int, sfw bool, page, limit int) (jikan.SearchResponse, error) { func (s *animeService) SearchAdvanced(ctx context.Context, q, animeType, status, orderBy, sort string, genres []int, sfw bool, page, limit int) (jikan.SearchResult, error) {
return s.jikan.SearchAdvanced(ctx, q, animeType, status, orderBy, sort, genres, sfw, page, limit) return s.jikan.SearchAdvanced(ctx, q, animeType, status, orderBy, sort, genres, sfw, page, limit)
} }
@@ -144,7 +144,7 @@ func (s *animeService) GetRecommendations(ctx context.Context, id int) ([]domain
return s.jikan.GetAnimeRecommendations(ctx, id) return s.jikan.GetAnimeRecommendations(ctx, id)
} }
func (s *animeService) GetRelations(ctx context.Context, id int) ([]jikan.Relation, error) { func (s *animeService) GetRelations(ctx context.Context, id int) ([]jikan.RelationEntry, error) {
return s.jikan.GetFullRelations(ctx, id) return s.jikan.GetFullRelations(ctx, id)
} }

View File

@@ -1,6 +1,8 @@
package app package app
import ( import (
"mal/integrations/jikan"
"mal/integrations/playback/allanime"
"mal/internal/database" "mal/internal/database"
"mal/internal/auth" "mal/internal/auth"
"mal/internal/anime" "mal/internal/anime"
@@ -18,17 +20,22 @@ func NewApp() *fx.App {
return fx.New( return fx.New(
database.Module, database.Module,
jikan.Module, jikan.Module,
allanime.Module,
auth.Module, auth.Module,
anime.Module, anime.Module,
watchlist.Module, watchlist.Module,
playback.Module, playback.Module,
templates.Module, templates.Module,
server.Module, server.Module,
fx.Decorate(func(r *templates.Renderer) render.HTMLRender { fx.Provide(func(r *templates.Renderer) render.HTMLRender {
return r return r
}), }),
fx.Invoke(func(r *gin.Engine, registers []server.RouteRegister) { fx.Invoke(fx.Annotate(
server.RegisterRoutes(r, registers) func(r *gin.Engine, authMiddleware gin.HandlerFunc, registers []server.RouteRegister) {
}), r.Use(authMiddleware)
server.RegisterRoutes(r, registers)
},
fx.ParamTags(``, ``, `group:"routes"`),
)),
) )
} }

View File

@@ -2,7 +2,6 @@ package handler
import ( import (
"mal/internal/domain" "mal/internal/domain"
"mal/internal/server"
"net/http" "net/http"
"time" "time"
@@ -43,7 +42,11 @@ func (h *AuthHandler) HandleLogin(c *gin.Context) {
} }
c.SetCookie("session_id", session.ID, int(24*time.Hour.Seconds()), "/", "", false, true) c.SetCookie("session_id", session.ID, int(24*time.Hour.Seconds()), "/", "", false, true)
c.Header("HX-Redirect", "/") if c.GetHeader("HX-Request") == "true" {
c.Header("HX-Redirect", "/")
c.Status(http.StatusOK)
return
}
c.Redirect(http.StatusSeeOther, "/") c.Redirect(http.StatusSeeOther, "/")
} }

View File

@@ -2,10 +2,13 @@ package auth
import ( import (
"mal/internal/auth/handler" "mal/internal/auth/handler"
"mal/internal/auth/middleware"
"mal/internal/auth/repository" "mal/internal/auth/repository"
"mal/internal/auth/service" "mal/internal/auth/service"
"mal/internal/domain"
"mal/internal/server" "mal/internal/server"
"github.com/gin-gonic/gin"
"go.uber.org/fx" "go.uber.org/fx"
) )
@@ -14,6 +17,9 @@ var Module = fx.Options(
repository.NewAuthRepository, repository.NewAuthRepository,
service.NewAuthService, service.NewAuthService,
handler.NewAuthHandler, handler.NewAuthHandler,
func(svc domain.AuthService) gin.HandlerFunc {
return middleware.AuthMiddleware(svc)
},
), ),
fx.Provide( fx.Provide(
server.AsRouteRegister(func(h *handler.AuthHandler) server.RouteRegister { server.AsRouteRegister(func(h *handler.AuthHandler) server.RouteRegister {

View File

@@ -7,8 +7,6 @@ import (
"mal/internal/db" "mal/internal/db"
"mal/internal/domain" "mal/internal/domain"
"time" "time"
"github.com/google/uuid"
) )
type authRepository struct { type authRepository struct {

View File

@@ -1,3 +1,4 @@
-- +goose Up
CREATE TABLE IF NOT EXISTS user ( CREATE TABLE IF NOT EXISTS user (
id TEXT PRIMARY KEY, id TEXT PRIMARY KEY,
username TEXT NOT NULL UNIQUE, username TEXT NOT NULL UNIQUE,
@@ -40,3 +41,10 @@ CREATE TABLE IF NOT EXISTS watch_list_entry (
current_time_seconds REAL NOT NULL DEFAULT 0, current_time_seconds REAL NOT NULL DEFAULT 0,
UNIQUE(user_id, anime_id) UNIQUE(user_id, anime_id)
); );
-- +goose Down
DROP TABLE IF EXISTS watch_list_entry;
DROP TABLE IF EXISTS anime;
DROP TABLE IF EXISTS account;
DROP TABLE IF EXISTS session;
DROP TABLE IF EXISTS user;

View File

@@ -1,6 +1,9 @@
-- +goose Up
-- Add English and Japanese title columns to anime table -- Add English and Japanese title columns to anime table
ALTER TABLE anime ADD COLUMN title_english TEXT; ALTER TABLE anime ADD COLUMN title_english TEXT;
ALTER TABLE anime ADD COLUMN title_japanese TEXT; ALTER TABLE anime ADD COLUMN title_japanese TEXT;
-- Rename existing title to title_original for clarity -- Rename existing title to title_original for clarity
ALTER TABLE anime RENAME COLUMN title TO title_original; ALTER TABLE anime RENAME COLUMN title TO title_original;
-- +goose Down

View File

@@ -1,2 +1,5 @@
-- +goose Up
-- Add airing status column to anime table -- Add airing status column to anime table
ALTER TABLE anime ADD COLUMN airing BOOLEAN DEFAULT 0; ALTER TABLE anime ADD COLUMN airing BOOLEAN DEFAULT 0;
-- +goose Down

View File

@@ -1,3 +1,4 @@
-- +goose Up
-- Note: watch_list_entry columns now in 001_init.sql -- Note: watch_list_entry columns now in 001_init.sql
-- Add notification preferences -- Add notification preferences
@@ -8,3 +9,5 @@ CREATE TABLE IF NOT EXISTS notification_preference (
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE(user_id) UNIQUE(user_id)
); );
-- +goose Down

View File

@@ -1,3 +1,4 @@
-- +goose Up
ALTER TABLE anime ADD COLUMN status TEXT DEFAULT ''; ALTER TABLE anime ADD COLUMN status TEXT DEFAULT '';
ALTER TABLE anime ADD COLUMN relations_synced_at DATETIME; ALTER TABLE anime ADD COLUMN relations_synced_at DATETIME;
@@ -7,3 +8,5 @@ CREATE TABLE IF NOT EXISTS anime_relation (
relation_type TEXT NOT NULL, relation_type TEXT NOT NULL,
PRIMARY KEY (anime_id, related_anime_id) PRIMARY KEY (anime_id, related_anime_id)
); );
-- +goose Down

View File

@@ -1,6 +1,9 @@
-- +goose Up
CREATE TABLE IF NOT EXISTS jikan_cache ( CREATE TABLE IF NOT EXISTS jikan_cache (
key TEXT PRIMARY KEY, key TEXT PRIMARY KEY,
data TEXT NOT NULL, data TEXT NOT NULL,
expires_at DATETIME NOT NULL, expires_at DATETIME NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
); );
-- +goose Down

View File

@@ -1,3 +1,4 @@
-- +goose Up
CREATE INDEX IF NOT EXISTS idx_watch_list_entry_user_status_updated_at CREATE INDEX IF NOT EXISTS idx_watch_list_entry_user_status_updated_at
ON watch_list_entry(user_id, status, updated_at); ON watch_list_entry(user_id, status, updated_at);
@@ -9,3 +10,5 @@ ON anime(relations_synced_at, status);
CREATE INDEX IF NOT EXISTS idx_jikan_cache_expires_at CREATE INDEX IF NOT EXISTS idx_jikan_cache_expires_at
ON jikan_cache(expires_at); ON jikan_cache(expires_at);
-- +goose Down

View File

@@ -1,3 +1,4 @@
-- +goose Up
CREATE TABLE IF NOT EXISTS anime_fetch_retry ( CREATE TABLE IF NOT EXISTS anime_fetch_retry (
anime_id INTEGER PRIMARY KEY, anime_id INTEGER PRIMARY KEY,
attempts INTEGER NOT NULL DEFAULT 0, attempts INTEGER NOT NULL DEFAULT 0,
@@ -9,3 +10,5 @@ CREATE TABLE IF NOT EXISTS anime_fetch_retry (
CREATE INDEX IF NOT EXISTS idx_anime_fetch_retry_next_retry_at CREATE INDEX IF NOT EXISTS idx_anime_fetch_retry_next_retry_at
ON anime_fetch_retry(next_retry_at); ON anime_fetch_retry(next_retry_at);
-- +goose Down

View File

@@ -1 +1,3 @@
-- +goose Up
-- Note: watch_list_entry columns now in 001_init.sql -- Note: watch_list_entry columns now in 001_init.sql
-- +goose Down

View File

@@ -1,3 +1,4 @@
-- +goose Up
CREATE TABLE IF NOT EXISTS continue_watching_entry ( CREATE TABLE IF NOT EXISTS continue_watching_entry (
id TEXT PRIMARY KEY, id TEXT PRIMARY KEY,
user_id TEXT NOT NULL REFERENCES user(id) ON DELETE CASCADE, user_id TEXT NOT NULL REFERENCES user(id) ON DELETE CASCADE,
@@ -11,3 +12,5 @@ CREATE TABLE IF NOT EXISTS continue_watching_entry (
CREATE INDEX IF NOT EXISTS idx_continue_watching_user_updated CREATE INDEX IF NOT EXISTS idx_continue_watching_user_updated
ON continue_watching_entry(user_id, updated_at DESC); ON continue_watching_entry(user_id, updated_at DESC);
-- +goose Down

View File

@@ -1,3 +1,4 @@
-- +goose Up
PRAGMA foreign_keys = OFF; PRAGMA foreign_keys = OFF;
BEGIN TRANSACTION; BEGIN TRANSACTION;
@@ -20,3 +21,5 @@ ALTER TABLE user_new RENAME TO user;
COMMIT; COMMIT;
PRAGMA foreign_keys = ON; PRAGMA foreign_keys = ON;
-- +goose Down

View File

@@ -1,2 +1,4 @@
-- +goose Up
DROP TABLE IF EXISTS account; DROP TABLE IF EXISTS account;
DROP TABLE IF EXISTS notification_preference; DROP TABLE IF EXISTS notification_preference;
-- +goose Down

View File

@@ -1,3 +1,4 @@
-- +goose Up
-- Add "watching" and "on_hold" to the valid statuses for watch_list_entry -- Add "watching" and "on_hold" to the valid statuses for watch_list_entry
PRAGMA foreign_keys=OFF; PRAGMA foreign_keys=OFF;
@@ -24,3 +25,5 @@ FROM watch_list_entry_old;
DROP TABLE watch_list_entry_old; DROP TABLE watch_list_entry_old;
PRAGMA foreign_keys=ON; PRAGMA foreign_keys=ON;
-- +goose Down

View File

@@ -1,5 +1,7 @@
-- +goose Up
-- Add duration column to anime table to store episode duration in seconds -- Add duration column to anime table to store episode duration in seconds
ALTER TABLE anime ADD COLUMN duration_seconds REAL; ALTER TABLE anime ADD COLUMN duration_seconds REAL;
-- Add duration_seconds column to continue_watching_entry to track episode duration -- Add duration_seconds column to continue_watching_entry to track episode duration
ALTER TABLE continue_watching_entry ADD COLUMN duration_seconds REAL; ALTER TABLE continue_watching_entry ADD COLUMN duration_seconds REAL;
-- +goose Down

View File

@@ -1,3 +1,5 @@
-- +goose Up
ALTER TABLE user ADD COLUMN avatar_url TEXT NOT NULL DEFAULT ''; ALTER TABLE user ADD COLUMN avatar_url TEXT NOT NULL DEFAULT '';
UPDATE user SET avatar_url = 'https://api.dicebear.com/9.x/dylan/svg?seed=' || username WHERE avatar_url = ''; UPDATE user SET avatar_url = 'https://api.dicebear.com/9.x/dylan/svg?seed=' || username WHERE avatar_url = '';
-- +goose Down

View File

@@ -38,14 +38,7 @@ func GetMigrationsDir() (string, error) {
return filepath.Join(wd, "migrations"), nil return filepath.Join(wd, "migrations"), nil
} }
// Init opens the database, runs migrations, and returns a Queries instance // Init opens the database and returns a Queries instance
func Init(db *sql.DB) (*Queries, error) { func Init(db *sql.DB) (*Queries, error) {
migrationsDir, err := GetMigrationsDir()
if err != nil {
return nil, err
}
if err := RunMigrations(db, migrationsDir); err != nil {
return nil, fmt.Errorf("failed to run migrations: %w", err)
}
return New(db), nil return New(db), nil
} }

View File

@@ -2,7 +2,6 @@ package server
import ( import (
"context" "context"
"fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
@@ -23,6 +22,8 @@ func ProvideRouter(htmlRender render.HTMLRender) *gin.Engine {
} }
r := gin.New() r := gin.New()
r.Use(gin.Logger(), gin.Recovery()) r.Use(gin.Logger(), gin.Recovery())
r.Static("/static", "./static")
r.Static("/dist", "./dist")
r.HTMLRender = htmlRender r.HTMLRender = htmlRender
return r return r
} }

View File

@@ -1,13 +1,12 @@
package templates package templates
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"html/template" "html/template"
"io" "io"
"log"
"net/http" "net/http"
"os"
"path/filepath" "path/filepath"
"slices" "slices"
"strconv" "strconv"
@@ -21,7 +20,7 @@ import (
// FS is the interface for template filesystem, to be provided by the main app or a mock. // FS is the interface for template filesystem, to be provided by the main app or a mock.
type FS interface { type FS interface {
ReadFile(name string) ([]byte, error) ReadFile(name string) ([]byte, error)
ReadDir(name string) ([]osDirEntry, error) ReadDir(name string) ([]os.DirEntry, error)
} }
// We will use embed.FS but wrapped in an interface if needed, or just use it directly. // We will use embed.FS but wrapped in an interface if needed, or just use it directly.
@@ -153,14 +152,16 @@ func ProvideRenderer() (*Renderer, error) {
return nil, err return nil, err
} }
basePath := filepath.Join(".", "templates", "base.gohtml")
for _, page := range pages { for _, page := range pages {
name := filepath.Base(page) name := filepath.Base(page)
if name == "base.gohtml" { if name == "base.gohtml" {
continue continue
} }
tmpl := template.New(name).Funcs(funcs) tmpl := template.New("base.gohtml").Funcs(funcs)
tmpl = template.Must(tmpl.ParseFiles(filepath.Join(".", "templates", "base.gohtml"))) tmpl = template.Must(tmpl.ParseFiles(basePath))
if len(components) > 0 { if len(components) > 0 {
tmpl = template.Must(tmpl.ParseFiles(components...)) tmpl = template.Must(tmpl.ParseFiles(components...))
} }
@@ -192,10 +193,18 @@ func (h HTMLRender) Render(w http.ResponseWriter) error {
return fmt.Errorf("template %s not found", h.Name) return fmt.Errorf("template %s not found", h.Name)
} }
if block, ok := h.Data.(map[string]any)["_fragment"]; ok { var block any
if blockStr, ok := block.(string); ok {
return tmpl.ExecuteTemplate(w, blockStr, h.Data) // Handle both map[string]any and gin.H (which is map[string]any but might
} // behave differently depending on the Go version/compiler in type assertions)
if dataMap, ok := h.Data.(map[string]any); ok {
block = dataMap["_fragment"]
} else if ginH, ok := h.Data.(gin.H); ok {
block = ginH["_fragment"]
}
if blockStr, ok := block.(string); ok && blockStr != "" {
return tmpl.ExecuteTemplate(w, blockStr, h.Data)
} }
return tmpl.ExecuteTemplate(w, "base.gohtml", h.Data) return tmpl.ExecuteTemplate(w, "base.gohtml", h.Data)

View File

@@ -2,10 +2,8 @@ package handler
import ( import (
"mal/internal/domain" "mal/internal/domain"
"mal/internal/server"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@@ -26,7 +24,12 @@ func (h *WatchlistHandler) Register(r *gin.Engine) {
} }
func (h *WatchlistHandler) HandleUpdateWatchlist(c *gin.Context) { func (h *WatchlistHandler) HandleUpdateWatchlist(c *gin.Context) {
userID := "" // TODO: get from auth context user, _ := c.Get("User")
userID := ""
if u, ok := user.(*domain.User); ok {
userID = u.ID
}
animeID, _ := strconv.ParseInt(c.PostForm("anime_id"), 10, 64) animeID, _ := strconv.ParseInt(c.PostForm("anime_id"), 10, 64)
status := c.PostForm("status") status := c.PostForm("status")
@@ -45,7 +48,12 @@ func (h *WatchlistHandler) HandleUpdateWatchlist(c *gin.Context) {
} }
func (h *WatchlistHandler) HandleDeleteWatchlist(c *gin.Context) { func (h *WatchlistHandler) HandleDeleteWatchlist(c *gin.Context) {
userID := "" // TODO: get from auth context user, _ := c.Get("User")
userID := ""
if u, ok := user.(*domain.User); ok {
userID = u.ID
}
animeID, _ := strconv.ParseInt(c.Param("id"), 10, 64) animeID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
if animeID <= 0 { if animeID <= 0 {
@@ -63,7 +71,12 @@ func (h *WatchlistHandler) HandleDeleteWatchlist(c *gin.Context) {
} }
func (h *WatchlistHandler) HandleDeleteContinueWatching(c *gin.Context) { func (h *WatchlistHandler) HandleDeleteContinueWatching(c *gin.Context) {
userID := "" // TODO: get from auth context user, _ := c.Get("User")
userID := ""
if u, ok := user.(*domain.User); ok {
userID = u.ID
}
animeID, _ := strconv.ParseInt(c.Param("id"), 10, 64) animeID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
if animeID <= 0 { if animeID <= 0 {
@@ -81,7 +94,12 @@ func (h *WatchlistHandler) HandleDeleteContinueWatching(c *gin.Context) {
} }
func (h *WatchlistHandler) HandleGetWatchlist(c *gin.Context) { func (h *WatchlistHandler) HandleGetWatchlist(c *gin.Context) {
userID := "" // TODO: get from auth context user, _ := c.Get("User")
userID := ""
if u, ok := user.(*domain.User); ok {
userID = u.ID
}
entries, err := h.svc.GetWatchlist(c.Request.Context(), userID) entries, err := h.svc.GetWatchlist(c.Request.Context(), userID)
if err != nil { if err != nil {
c.Status(http.StatusInternalServerError) c.Status(http.StatusInternalServerError)
@@ -91,5 +109,6 @@ func (h *WatchlistHandler) HandleGetWatchlist(c *gin.Context) {
c.HTML(http.StatusOK, "watchlist.gohtml", gin.H{ c.HTML(http.StatusOK, "watchlist.gohtml", gin.H{
"Entries": entries, "Entries": entries,
"CurrentPath": "/watchlist", "CurrentPath": "/watchlist",
"User": user,
}) })
} }

View File

@@ -3,7 +3,6 @@ package service
import ( import (
"context" "context"
"database/sql" "database/sql"
"fmt"
"mal/integrations/jikan" "mal/integrations/jikan"
"mal/internal/db" "mal/internal/db"
"mal/internal/domain" "mal/internal/domain"

View File

@@ -2,8 +2,8 @@
@import url('https://fonts.googleapis.com/css2?family=DM+Sans:opsz,wght@9..40,400;9..40,500;9..40,600;9..40,700&display=swap'); @import url('https://fonts.googleapis.com/css2?family=DM+Sans:opsz,wght@9..40,400;9..40,500;9..40,600;9..40,700&display=swap');
@import '@toolwind/anchors'; @import '@toolwind/anchors';
@source "."; @source "../../templates/**/*.gohtml";
@source "../web/**/*.templ"; @source "../**/*.ts";
@theme { @theme {
--color-background: light-dark(#ffffff, #080808); --color-background: light-dark(#ffffff, #080808);

View File

@@ -2,7 +2,7 @@
<div class="mt-12 w-full"> <div class="mt-12 w-full">
<h2 class="mb-6 text-lg font-normal text-foreground">Characters & Cast</h2> <h2 class="mb-6 text-lg font-normal text-foreground">Characters & Cast</h2>
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5"> <div class="grid grid-cols-1 gap-4 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5">
{{range (slice . 0 (min (len .) 10))}} {{range (slice .Items 0 (min (len .Items) 10))}}
<div class="flex gap-3 bg-background-surface p-3 ring-1 ring-border"> <div class="flex gap-3 bg-background-surface p-3 ring-1 ring-border">
<div class="h-16 w-12 shrink-0 overflow-hidden bg-background-surface"> <div class="h-16 w-12 shrink-0 overflow-hidden bg-background-surface">
<img src="{{.Character.Images.Jpg.ImageURL}}" alt="{{.Character.Name}}" class="h-full w-full object-cover" loading="lazy" /> <img src="{{.Character.Images.Jpg.ImageURL}}" alt="{{.Character.Name}}" class="h-full w-full object-cover" loading="lazy" />
@@ -21,11 +21,11 @@
{{end}} {{end}}
{{define "anime_recommendations"}} {{define "anime_recommendations"}}
{{if .}} {{if .Items}}
<div class="w-full"> <div class="w-full">
<h2 class="mb-6 text-lg font-normal text-foreground">Recommendations</h2> <h2 class="mb-6 text-lg font-normal text-foreground">Recommendations</h2>
<div class="grid grid-cols-2 gap-4 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6 xl:grid-cols-8"> <div class="grid grid-cols-2 gap-4 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6 xl:grid-cols-8">
{{range (slice . 0 (min (len .) 8))}} {{range (slice .Items 0 (min (len .Items) 8))}}
<a href="/anime/{{.Entry.MalID}}" class="group flex flex-col gap-2"> <a href="/anime/{{.Entry.MalID}}" class="group flex flex-col gap-2">
<div class="aspect-2/3 overflow-hidden bg-background-surface shadow-md"> <div class="aspect-2/3 overflow-hidden bg-background-surface shadow-md">
<img src="{{.Entry.Images.Webp.LargeImageURL}}" alt="{{.Entry.Title}}" class="h-full w-full object-cover transition-transform duration-500 group-hover:scale-105" loading="lazy" /> <img src="{{.Entry.Images.Webp.LargeImageURL}}" alt="{{.Entry.Title}}" class="h-full w-full object-cover transition-transform duration-500 group-hover:scale-105" loading="lazy" />
@@ -43,7 +43,7 @@
{{if .WatchlistIDs}}<script>initWatchlist({{.WatchlistIDs}})</script>{{end}} {{if .WatchlistIDs}}<script>initWatchlist({{.WatchlistIDs}})</script>{{end}}
{{$anime := .Anime}} {{$anime := .Anime}}
<div class="flex flex-col gap-10 pr-0 lg:pr-80"> <div class="flex flex-col gap-10 lg:pr-80">
<div class="flex flex-col gap-8 md:flex-row lg:gap-12"> <div class="flex flex-col gap-8 md:flex-row lg:gap-12">
<div class="flex w-64 shrink-0 flex-col items-center gap-6 md:w-80 md:items-start lg:w-96"> <div class="flex w-64 shrink-0 flex-col items-center gap-6 md:w-80 md:items-start lg:w-96">
<div class="aspect-2/3 w-full overflow-hidden bg-background-surface shadow-lg"> <div class="aspect-2/3 w-full overflow-hidden bg-background-surface shadow-lg">

View File

@@ -33,7 +33,7 @@
html[data-theme="light"] .theme-icon-light { display: none; } html[data-theme="light"] .theme-icon-light { display: none; }
html[data-theme="light"] .theme-icon-dark { display: block; } html[data-theme="light"] .theme-icon-dark { display: block; }
</style> </style>
<script type="module" src="/dist/static/theme.js" defer></script> <script type="module" src="/dist/theme.js" defer></script>
<template id="toast-template"> <template id="toast-template">
<div class="toast bg-foreground/10 border border-border flex items-center gap-3 px-4 py-3 shadow-lg transform transition-all duration-300 translate-y-2 opacity-0"> <div class="toast bg-foreground/10 border border-border flex items-center gap-3 px-4 py-3 shadow-lg transform transition-all duration-300 translate-y-2 opacity-0">
<span class="toast-message text-sm text-foreground"></span> <span class="toast-message text-sm text-foreground"></span>
@@ -44,15 +44,15 @@
</button> </button>
</div> </div>
</template> </template>
<script type="module" src="/dist/static/dropdown.js" defer></script> <script type="module" src="/dist/dropdown.js" defer></script>
<script type="module" src="/dist/static/discover.js" defer></script> <script type="module" src="/dist/discover.js" defer></script>
<script type="module" src="/dist/static/anime.js" defer></script> <script type="module" src="/dist/anime.js" defer></script>
<script type="module" src="/dist/static/timezone.js" defer></script> <script type="module" src="/dist/timezone.js" defer></script>
<script type="module" src="/dist/static/player/main.js" defer></script> <script type="module" src="/dist/static/player/main.js" defer></script>
<script type="module" src="/dist/static/search.js" defer></script> <script type="module" src="/dist/search.js" defer></script>
<script type="module" src="/dist/static/sort_filter.js" defer></script> <script type="module" src="/dist/sort_filter.js" defer></script>
<script type="module" src="/dist/static/dedupe.js" defer></script> <script type="module" src="/dist/dedupe.js" defer></script>
<script type="module" src="/dist/static/toast.js" defer></script> <script type="module" src="/dist/toast.js" defer></script>
<script src="https://unpkg.com/htmx.org@1.9.12"></script> <script src="https://unpkg.com/htmx.org@1.9.12"></script>
<script> <script>
document.addEventListener('htmx:afterSwap', function(evt) { document.addEventListener('htmx:afterSwap', function(evt) {
@@ -195,20 +195,19 @@ if (window.showToast) showToast({ message: 'Something went wrong' })
<button id="mobile-overlay" class="hidden fixed inset-0 z-40 w-full cursor-default border-none bg-black/60 backdrop-blur-sm outline-none lg:hidden" onclick="toggleMobileMenu()" aria-label="Close mobile menu"></button> <button id="mobile-overlay" class="hidden fixed inset-0 z-40 w-full cursor-default border-none bg-black/60 backdrop-blur-sm outline-none lg:hidden" onclick="toggleMobileMenu()" aria-label="Close mobile menu"></button>
<!-- Sidebar --> <!-- Sidebar -->
<div id="mobile-menu" class="fixed inset-y-0 left-0 z-50 shrink-0 overflow-hidden transform lg:relative lg:z-auto lg:translate-x-0 w-64 shadow-2xl transition-transform duration-300 -translate-x-full lg:block"> <div id="mobile-menu" class="fixed inset-y-0 left-0 z-50 shrink-0 overflow-hidden transform lg:relative lg:z-auto lg:translate-x-0 w-64 transition-transform duration-300 -translate-x-full lg:block">
{{block "sidebar" .}} {{block "sidebar" .}}
{{template "navigation" dict "CurrentPath" .CurrentPath}} {{template "navigation" dict "CurrentPath" .CurrentPath}}
{{end}} {{end}}
</div> </div>
<main class="w-full flex-1 overflow-x-hidden flex flex-col h-screen overflow-y-auto"> <main class="w-full flex-1 flex flex-col h-screen overflow-y-auto">
<div class="sticky top-0 z-40 w-full"> <div class="sticky top-0 z-40 w-full">
{{template "header" .}} {{template "header" .}}
</div> </div>
<div class="flex-1 p-4 md:p-8 lg:p-10"> <div class="flex-1 p-4 md:p-8 lg:p-10">
{{template "content" .}} {{template "content" .}}
</div> </div>
{{template "footer" .}}
</main> </main>
</div> </div>
{{else}} {{else}}
@@ -216,7 +215,6 @@ if (window.showToast) showToast({ message: 'Something went wrong' })
<div class="flex-1"> <div class="flex-1">
{{template "content" .}} {{template "content" .}}
</div> </div>
{{template "footer" .}}
</main> </main>
{{end}} {{end}}
</div> </div>

View File

@@ -9,7 +9,7 @@
<div id="continue-watching-{{.AnimeID}}" class="continue-watching-item group relative w-70 shrink-0 snap-start space-y-2 2xl:w-lg"> <div id="continue-watching-{{.AnimeID}}" class="continue-watching-item group relative w-70 shrink-0 snap-start space-y-2 2xl:w-lg">
<div class="bg-background/80 relative aspect-video w-full overflow-hidden"> <div class="bg-background/80 relative aspect-video w-full overflow-hidden">
<a href="/anime/{{.AnimeID}}/watch" class="block h-full w-full"> <a href="/watch/{{.AnimeID}}" class="block h-full w-full">
<img src="{{if .ImageUrl}}{{.ImageUrl}}{{else}}https://placehold.co/500x500{{end}}" alt="{{$title}}" class="h-full w-full object-cover" /> <img src="{{if .ImageUrl}}{{.ImageUrl}}{{else}}https://placehold.co/500x500{{end}}" alt="{{$title}}" class="h-full w-full object-cover" />
</a> </a>
@@ -29,7 +29,7 @@
</div> </div>
<div> <div>
<a href="/anime/{{.AnimeID}}/watch" class="block"> <a href="/watch/{{.AnimeID}}" class="block">
<h3 class="text-foreground truncate text-lg font-normal"> <h3 class="text-foreground truncate text-lg font-normal">
{{$title}} {{$title}}
</h3> </h3>

View File

@@ -46,8 +46,9 @@
</div> </div>
</ui-dropdown> </ui-dropdown>
<a href="/anime/{{$anime.MalID}}/watch" class="bg-background-button hover:bg-background-button-hover px-5 py-2.5 text-sm font-medium text-foreground transition-colors"> <a href="/watch/{{$anime.MalID}}" class="bg-background-button hover:bg-background-button-hover px-5 py-2.5 text-sm font-medium text-foreground transition-colors">
Watch <i class="fa-solid fa-play mr-2"></i>
Watch Now
</a> </a>
</div> </div>