refactor: general architectural cleanup and bug fixes
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"mal/internal/app"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
@@ -25,7 +25,7 @@ type Client struct {
|
||||
lastReqTime time.Time // rate limiting: last request timestamp
|
||||
}
|
||||
|
||||
func NewClient(db db.Querier) *Client {
|
||||
func NewClient(queries *db.Queries) *Client {
|
||||
return &Client{
|
||||
httpClient: &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
@@ -37,7 +37,7 @@ func NewClient(db db.Querier) *Client {
|
||||
},
|
||||
},
|
||||
baseURL: "https://api.jikan.moe/v4",
|
||||
db: db,
|
||||
db: queries,
|
||||
retrySignal: make(chan struct{}, 1),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package jikan
|
||||
|
||||
import (
|
||||
"mal/internal/db"
|
||||
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
|
||||
@@ -1,28 +1,29 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"mal/internal/domain"
|
||||
"mal/internal/server"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type AnimeHandler struct {
|
||||
svc domain.AnimeService
|
||||
watchlistSvc domain.WatchlistService
|
||||
}
|
||||
|
||||
func NewAnimeHandler(svc domain.AnimeService) *AnimeHandler {
|
||||
return &AnimeHandler{svc: svc}
|
||||
func NewAnimeHandler(svc domain.AnimeService, watchlistSvc domain.WatchlistService) *AnimeHandler {
|
||||
return &AnimeHandler{
|
||||
svc: svc,
|
||||
watchlistSvc: watchlistSvc,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *AnimeHandler) Register(r *gin.Engine) {
|
||||
log.Println("Registering anime routes")
|
||||
r.GET("/", h.HandleCatalog)
|
||||
r.GET("/api/catalog/airing", h.HandleCatalogAiring)
|
||||
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) {
|
||||
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{
|
||||
"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) {
|
||||
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)
|
||||
if err != nil {
|
||||
log.Printf("catalog %s error: %v", section, err)
|
||||
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["_fragment"] = "catalog_section"
|
||||
data["WatchlistMap"] = watchlistMap
|
||||
c.HTML(http.StatusOK, "index.gohtml", data)
|
||||
}
|
||||
|
||||
func (h *AnimeHandler) HandleDiscover(c *gin.Context) {
|
||||
user, _ := c.Get("User")
|
||||
c.HTML(http.StatusOK, "discover.gohtml", gin.H{
|
||||
"CurrentPath": "/discover",
|
||||
"User": user,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -88,15 +115,28 @@ func (h *AnimeHandler) HandleDiscoverTop(c *gin.Context) {
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
log.Printf("discover %s error: %v", section, err)
|
||||
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["_fragment"] = "discover_section"
|
||||
data["WatchlistMap"] = watchlistMap
|
||||
c.HTML(http.StatusOK, "discover.gohtml", data)
|
||||
}
|
||||
|
||||
@@ -145,6 +185,15 @@ func (h *AnimeHandler) HandleBrowse(c *gin.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{
|
||||
"CurrentPath": "/browse",
|
||||
"Query": q,
|
||||
@@ -158,6 +207,8 @@ func (h *AnimeHandler) HandleBrowse(c *gin.Context) {
|
||||
"Animes": res.Animes,
|
||||
"HasNextPage": res.HasNextPage,
|
||||
"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{
|
||||
"_fragment": tplName,
|
||||
"Data": data,
|
||||
"Items": data,
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -200,9 +251,11 @@ func (h *AnimeHandler) HandleAnimeDetails(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
user, _ := c.Get("User")
|
||||
c.HTML(http.StatusOK, "anime.gohtml", gin.H{
|
||||
"Anime": anime,
|
||||
"CurrentPath": fmt.Sprintf("/anime/%d", id),
|
||||
"User": user,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -213,16 +266,31 @@ func (h *AnimeHandler) HandleHTMLWatchOrder(c *gin.Context) {
|
||||
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)
|
||||
if err != nil {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
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{
|
||||
"_fragment": "watch_order",
|
||||
"Relations": relations,
|
||||
"AnimeID": id,
|
||||
"WatchlistMap": watchlistMap,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -239,11 +307,21 @@ func (h *AnimeHandler) HandleQuickSearch(c *gin.Context) {
|
||||
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 {
|
||||
ID int `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Type string `json:"type"`
|
||||
Image string `json:"image"`
|
||||
InWatchlist bool `json:"in_watchlist"`
|
||||
}
|
||||
|
||||
output := make([]quickSearchResult, len(res.Animes))
|
||||
@@ -253,6 +331,7 @@ func (h *AnimeHandler) HandleQuickSearch(c *gin.Context) {
|
||||
Title: anime.DisplayTitle(),
|
||||
Type: anime.Type,
|
||||
Image: anime.ImageURL(),
|
||||
InWatchlist: watchlistMap[anime.MalID],
|
||||
}
|
||||
}
|
||||
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"})
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ func (s *animeService) GetAnimeByID(ctx context.Context, id int) (domain.Anime,
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -144,7 +144,7 @@ func (s *animeService) GetRecommendations(ctx context.Context, id int) ([]domain
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"mal/integrations/jikan"
|
||||
"mal/integrations/playback/allanime"
|
||||
"mal/internal/database"
|
||||
"mal/internal/auth"
|
||||
"mal/internal/anime"
|
||||
@@ -18,17 +20,22 @@ func NewApp() *fx.App {
|
||||
return fx.New(
|
||||
database.Module,
|
||||
jikan.Module,
|
||||
allanime.Module,
|
||||
auth.Module,
|
||||
anime.Module,
|
||||
watchlist.Module,
|
||||
playback.Module,
|
||||
templates.Module,
|
||||
server.Module,
|
||||
fx.Decorate(func(r *templates.Renderer) render.HTMLRender {
|
||||
fx.Provide(func(r *templates.Renderer) render.HTMLRender {
|
||||
return r
|
||||
}),
|
||||
fx.Invoke(func(r *gin.Engine, registers []server.RouteRegister) {
|
||||
fx.Invoke(fx.Annotate(
|
||||
func(r *gin.Engine, authMiddleware gin.HandlerFunc, registers []server.RouteRegister) {
|
||||
r.Use(authMiddleware)
|
||||
server.RegisterRoutes(r, registers)
|
||||
}),
|
||||
},
|
||||
fx.ParamTags(``, ``, `group:"routes"`),
|
||||
)),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package handler
|
||||
|
||||
import (
|
||||
"mal/internal/domain"
|
||||
"mal/internal/server"
|
||||
"net/http"
|
||||
"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)
|
||||
if c.GetHeader("HX-Request") == "true" {
|
||||
c.Header("HX-Redirect", "/")
|
||||
c.Status(http.StatusOK)
|
||||
return
|
||||
}
|
||||
c.Redirect(http.StatusSeeOther, "/")
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,13 @@ package auth
|
||||
|
||||
import (
|
||||
"mal/internal/auth/handler"
|
||||
"mal/internal/auth/middleware"
|
||||
"mal/internal/auth/repository"
|
||||
"mal/internal/auth/service"
|
||||
"mal/internal/domain"
|
||||
"mal/internal/server"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
@@ -14,6 +17,9 @@ var Module = fx.Options(
|
||||
repository.NewAuthRepository,
|
||||
service.NewAuthService,
|
||||
handler.NewAuthHandler,
|
||||
func(svc domain.AuthService) gin.HandlerFunc {
|
||||
return middleware.AuthMiddleware(svc)
|
||||
},
|
||||
),
|
||||
fx.Provide(
|
||||
server.AsRouteRegister(func(h *handler.AuthHandler) server.RouteRegister {
|
||||
|
||||
@@ -7,8 +7,6 @@ import (
|
||||
"mal/internal/db"
|
||||
"mal/internal/domain"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type authRepository struct {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
-- +goose Up
|
||||
CREATE TABLE IF NOT EXISTS user (
|
||||
id TEXT PRIMARY KEY,
|
||||
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,
|
||||
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;
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
-- +goose Up
|
||||
-- Add English and Japanese title columns to anime table
|
||||
ALTER TABLE anime ADD COLUMN title_english TEXT;
|
||||
ALTER TABLE anime ADD COLUMN title_japanese TEXT;
|
||||
|
||||
-- Rename existing title to title_original for clarity
|
||||
ALTER TABLE anime RENAME COLUMN title TO title_original;
|
||||
|
||||
-- +goose Down
|
||||
|
||||
@@ -1,2 +1,5 @@
|
||||
-- +goose Up
|
||||
-- Add airing status column to anime table
|
||||
ALTER TABLE anime ADD COLUMN airing BOOLEAN DEFAULT 0;
|
||||
|
||||
-- +goose Down
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
-- +goose Up
|
||||
-- Note: watch_list_entry columns now in 001_init.sql
|
||||
|
||||
-- Add notification preferences
|
||||
@@ -8,3 +9,5 @@ CREATE TABLE IF NOT EXISTS notification_preference (
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(user_id)
|
||||
);
|
||||
|
||||
-- +goose Down
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
-- +goose Up
|
||||
ALTER TABLE anime ADD COLUMN status TEXT DEFAULT '';
|
||||
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,
|
||||
PRIMARY KEY (anime_id, related_anime_id)
|
||||
);
|
||||
|
||||
-- +goose Down
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
-- +goose Up
|
||||
CREATE TABLE IF NOT EXISTS jikan_cache (
|
||||
key TEXT PRIMARY KEY,
|
||||
data TEXT NOT NULL,
|
||||
expires_at DATETIME NOT NULL,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- +goose Down
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
-- +goose Up
|
||||
CREATE INDEX IF NOT EXISTS idx_watch_list_entry_user_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
|
||||
ON jikan_cache(expires_at);
|
||||
|
||||
-- +goose Down
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
-- +goose Up
|
||||
CREATE TABLE IF NOT EXISTS anime_fetch_retry (
|
||||
anime_id INTEGER PRIMARY KEY,
|
||||
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
|
||||
ON anime_fetch_retry(next_retry_at);
|
||||
|
||||
-- +goose Down
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
-- +goose Up
|
||||
-- Note: watch_list_entry columns now in 001_init.sql
|
||||
-- +goose Down
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
-- +goose Up
|
||||
CREATE TABLE IF NOT EXISTS continue_watching_entry (
|
||||
id TEXT PRIMARY KEY,
|
||||
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
|
||||
ON continue_watching_entry(user_id, updated_at DESC);
|
||||
|
||||
-- +goose Down
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
-- +goose Up
|
||||
PRAGMA foreign_keys = OFF;
|
||||
|
||||
BEGIN TRANSACTION;
|
||||
@@ -20,3 +21,5 @@ ALTER TABLE user_new RENAME TO user;
|
||||
COMMIT;
|
||||
|
||||
PRAGMA foreign_keys = ON;
|
||||
|
||||
-- +goose Down
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
-- +goose Up
|
||||
DROP TABLE IF EXISTS account;
|
||||
DROP TABLE IF EXISTS notification_preference;
|
||||
-- +goose Down
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
-- +goose Up
|
||||
-- Add "watching" and "on_hold" to the valid statuses for watch_list_entry
|
||||
|
||||
PRAGMA foreign_keys=OFF;
|
||||
@@ -24,3 +25,5 @@ FROM watch_list_entry_old;
|
||||
DROP TABLE watch_list_entry_old;
|
||||
|
||||
PRAGMA foreign_keys=ON;
|
||||
|
||||
-- +goose Down
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
-- +goose Up
|
||||
-- Add duration column to anime table to store episode duration in seconds
|
||||
ALTER TABLE anime ADD COLUMN duration_seconds REAL;
|
||||
|
||||
-- Add duration_seconds column to continue_watching_entry to track episode duration
|
||||
ALTER TABLE continue_watching_entry ADD COLUMN duration_seconds REAL;
|
||||
-- +goose Down
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
-- +goose Up
|
||||
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 = '';
|
||||
-- +goose Down
|
||||
|
||||
@@ -38,14 +38,7 @@ func GetMigrationsDir() (string, error) {
|
||||
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) {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -23,6 +22,8 @@ func ProvideRouter(htmlRender render.HTMLRender) *gin.Engine {
|
||||
}
|
||||
r := gin.New()
|
||||
r.Use(gin.Logger(), gin.Recovery())
|
||||
r.Static("/static", "./static")
|
||||
r.Static("/dist", "./dist")
|
||||
r.HTMLRender = htmlRender
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strconv"
|
||||
@@ -21,7 +20,7 @@ import (
|
||||
// FS is the interface for template filesystem, to be provided by the main app or a mock.
|
||||
type FS interface {
|
||||
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.
|
||||
@@ -153,14 +152,16 @@ func ProvideRenderer() (*Renderer, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
basePath := filepath.Join(".", "templates", "base.gohtml")
|
||||
|
||||
for _, page := range pages {
|
||||
name := filepath.Base(page)
|
||||
if name == "base.gohtml" {
|
||||
continue
|
||||
}
|
||||
|
||||
tmpl := template.New(name).Funcs(funcs)
|
||||
tmpl = template.Must(tmpl.ParseFiles(filepath.Join(".", "templates", "base.gohtml")))
|
||||
tmpl := template.New("base.gohtml").Funcs(funcs)
|
||||
tmpl = template.Must(tmpl.ParseFiles(basePath))
|
||||
if len(components) > 0 {
|
||||
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)
|
||||
}
|
||||
|
||||
if block, ok := h.Data.(map[string]any)["_fragment"]; ok {
|
||||
if blockStr, ok := block.(string); ok {
|
||||
return tmpl.ExecuteTemplate(w, blockStr, h.Data)
|
||||
var block any
|
||||
|
||||
// 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)
|
||||
|
||||
@@ -2,10 +2,8 @@ package handler
|
||||
|
||||
import (
|
||||
"mal/internal/domain"
|
||||
"mal/internal/server"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -26,7 +24,12 @@ func (h *WatchlistHandler) Register(r *gin.Engine) {
|
||||
}
|
||||
|
||||
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)
|
||||
status := c.PostForm("status")
|
||||
|
||||
@@ -45,7 +48,12 @@ func (h *WatchlistHandler) HandleUpdateWatchlist(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)
|
||||
|
||||
if animeID <= 0 {
|
||||
@@ -63,7 +71,12 @@ func (h *WatchlistHandler) HandleDeleteWatchlist(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)
|
||||
|
||||
if animeID <= 0 {
|
||||
@@ -81,7 +94,12 @@ func (h *WatchlistHandler) HandleDeleteContinueWatching(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)
|
||||
if err != nil {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
@@ -91,5 +109,6 @@ func (h *WatchlistHandler) HandleGetWatchlist(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "watchlist.gohtml", gin.H{
|
||||
"Entries": entries,
|
||||
"CurrentPath": "/watchlist",
|
||||
"User": user,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"mal/integrations/jikan"
|
||||
"mal/internal/db"
|
||||
"mal/internal/domain"
|
||||
|
||||
@@ -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 '@toolwind/anchors';
|
||||
|
||||
@source ".";
|
||||
@source "../web/**/*.templ";
|
||||
@source "../../templates/**/*.gohtml";
|
||||
@source "../**/*.ts";
|
||||
|
||||
@theme {
|
||||
--color-background: light-dark(#ffffff, #080808);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<div class="mt-12 w-full">
|
||||
<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">
|
||||
{{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="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" />
|
||||
@@ -21,11 +21,11 @@
|
||||
{{end}}
|
||||
|
||||
{{define "anime_recommendations"}}
|
||||
{{if .}}
|
||||
{{if .Items}}
|
||||
<div class="w-full">
|
||||
<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">
|
||||
{{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">
|
||||
<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" />
|
||||
@@ -43,7 +43,7 @@
|
||||
{{if .WatchlistIDs}}<script>initWatchlist({{.WatchlistIDs}})</script>{{end}}
|
||||
{{$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 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">
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
html[data-theme="light"] .theme-icon-light { display: none; }
|
||||
html[data-theme="light"] .theme-icon-dark { display: block; }
|
||||
</style>
|
||||
<script type="module" src="/dist/static/theme.js" defer></script>
|
||||
<script type="module" src="/dist/theme.js" defer></script>
|
||||
<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">
|
||||
<span class="toast-message text-sm text-foreground"></span>
|
||||
@@ -44,15 +44,15 @@
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
<script type="module" src="/dist/static/dropdown.js" defer></script>
|
||||
<script type="module" src="/dist/static/discover.js" defer></script>
|
||||
<script type="module" src="/dist/static/anime.js" defer></script>
|
||||
<script type="module" src="/dist/static/timezone.js" defer></script>
|
||||
<script type="module" src="/dist/dropdown.js" defer></script>
|
||||
<script type="module" src="/dist/discover.js" defer></script>
|
||||
<script type="module" src="/dist/anime.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/search.js" defer></script>
|
||||
<script type="module" src="/dist/static/sort_filter.js" defer></script>
|
||||
<script type="module" src="/dist/static/dedupe.js" defer></script>
|
||||
<script type="module" src="/dist/static/toast.js" defer></script>
|
||||
<script type="module" src="/dist/search.js" defer></script>
|
||||
<script type="module" src="/dist/sort_filter.js" defer></script>
|
||||
<script type="module" src="/dist/dedupe.js" defer></script>
|
||||
<script type="module" src="/dist/toast.js" defer></script>
|
||||
<script src="https://unpkg.com/htmx.org@1.9.12"></script>
|
||||
<script>
|
||||
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>
|
||||
|
||||
<!-- 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" .}}
|
||||
{{template "navigation" dict "CurrentPath" .CurrentPath}}
|
||||
{{end}}
|
||||
</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">
|
||||
{{template "header" .}}
|
||||
</div>
|
||||
<div class="flex-1 p-4 md:p-8 lg:p-10">
|
||||
{{template "content" .}}
|
||||
</div>
|
||||
{{template "footer" .}}
|
||||
</main>
|
||||
</div>
|
||||
{{else}}
|
||||
@@ -216,7 +215,6 @@ if (window.showToast) showToast({ message: 'Something went wrong' })
|
||||
<div class="flex-1">
|
||||
{{template "content" .}}
|
||||
</div>
|
||||
{{template "footer" .}}
|
||||
</main>
|
||||
{{end}}
|
||||
</div>
|
||||
|
||||
@@ -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 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" />
|
||||
</a>
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<a href="/anime/{{.AnimeID}}/watch" class="block">
|
||||
<a href="/watch/{{.AnimeID}}" class="block">
|
||||
<h3 class="text-foreground truncate text-lg font-normal">
|
||||
{{$title}}
|
||||
</h3>
|
||||
|
||||
@@ -46,8 +46,9 @@
|
||||
</div>
|
||||
</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">
|
||||
Watch
|
||||
<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">
|
||||
<i class="fa-solid fa-play mr-2"></i>
|
||||
Watch Now
|
||||
</a>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user