aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/server/routes/decks.ts
blob: 2e170db7e141a50228b37ea07a8e96dd43f743cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import { z } from "zod";
import { authMiddleware, Errors, getAuthUser } from "../middleware/index.js";
import {
	type CardRepository,
	cardRepository,
	type DeckRepository,
	deckRepository,
} from "../repositories/index.js";
import { createDeckSchema, updateDeckSchema } from "../schemas/index.js";

export interface DeckDependencies {
	deckRepo: DeckRepository;
	cardRepo: CardRepository;
}

const deckIdParamSchema = z.object({
	id: z.uuid(),
});

export function createDecksRouter(deps: DeckDependencies) {
	const { deckRepo, cardRepo } = deps;

	return new Hono()
		.use("*", authMiddleware)
		.get("/", async (c) => {
			const user = getAuthUser(c);
			const decks = await deckRepo.findByUserId(user.id);
			const now = new Date();
			const decksWithDueCount = await Promise.all(
				decks.map(async (deck) => {
					const dueCardCount = await cardRepo.countDueCards(deck.id, now);
					return { ...deck, dueCardCount };
				}),
			);
			return c.json({ decks: decksWithDueCount }, 200);
		})
		.post("/", zValidator("json", createDeckSchema), async (c) => {
			const user = getAuthUser(c);
			const data = c.req.valid("json");

			const deck = await deckRepo.create({
				userId: user.id,
				name: data.name,
				description: data.description,
				newCardsPerDay: data.newCardsPerDay,
			});

			return c.json({ deck }, 201);
		})
		.get("/:id", zValidator("param", deckIdParamSchema), async (c) => {
			const user = getAuthUser(c);
			const { id } = c.req.valid("param");

			const deck = await deckRepo.findById(id, user.id);
			if (!deck) {
				throw Errors.notFound("Deck not found", "DECK_NOT_FOUND");
			}

			return c.json({ deck }, 200);
		})
		.put(
			"/:id",
			zValidator("param", deckIdParamSchema),
			zValidator("json", updateDeckSchema),
			async (c) => {
				const user = getAuthUser(c);
				const { id } = c.req.valid("param");
				const data = c.req.valid("json");

				const deck = await deckRepo.update(id, user.id, data);
				if (!deck) {
					throw Errors.notFound("Deck not found", "DECK_NOT_FOUND");
				}

				return c.json({ deck }, 200);
			},
		)
		.delete("/:id", zValidator("param", deckIdParamSchema), async (c) => {
			const user = getAuthUser(c);
			const { id } = c.req.valid("param");

			const deleted = await deckRepo.softDelete(id, user.id);
			if (!deleted) {
				throw Errors.notFound("Deck not found", "DECK_NOT_FOUND");
			}

			return c.json({ success: true }, 200);
		});
}

export const decks = createDecksRouter({
	deckRepo: deckRepository,
	cardRepo: cardRepository,
});