aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/server/routes/decks.ts
blob: 069b933f89532c0b27d7ee126aa8e3f4661d79b6 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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, newCardCount, totalCardCount, reviewCardCount] =
						await Promise.all([
							cardRepo.countDueCards(deck.id, now),
							cardRepo.countNewCards(deck.id),
							cardRepo.countTotalCards(deck.id),
							cardRepo.countReviewStateCards(deck.id),
						]);
					return {
						...deck,
						dueCardCount,
						newCardCount,
						totalCardCount,
						reviewCardCount,
					};
				}),
			);
			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,
			});

			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");
			}

			const now = new Date();
			const [dueCardCount, newCardCount, totalCardCount, reviewCardCount] =
				await Promise.all([
					cardRepo.countDueCards(deck.id, now),
					cardRepo.countNewCards(deck.id),
					cardRepo.countTotalCards(deck.id),
					cardRepo.countReviewStateCards(deck.id),
				]);

			return c.json(
				{
					deck: {
						...deck,
						dueCardCount,
						newCardCount,
						totalCardCount,
						reviewCardCount,
					},
				},
				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,
});