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
128
129
130
131
132
133
134
135
|
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,
type ReviewLogRepository,
reviewLogRepository,
} from "../repositories/index.js";
import { createDeckSchema, updateDeckSchema } from "../schemas/index.js";
export interface DeckDependencies {
deckRepo: DeckRepository;
cardRepo: CardRepository;
reviewLogRepo: ReviewLogRepository;
}
const deckIdParamSchema = z.object({
id: z.uuid(),
});
const REVIEW_CARDS_LIMIT = 80;
export function createDecksRouter(deps: DeckDependencies) {
const { deckRepo, cardRepo, reviewLogRepo } = 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 [dueNewCards, dueReviewCards, reviewedNewCards] =
await Promise.all([
cardRepo.countDueNewCards(deck.id, now),
cardRepo.countDueReviewCards(deck.id, now),
reviewLogRepo.countTodayNewCardReviews(deck.id, now),
]);
// Apply the same limits as the study screen
const newCardBudget = Math.max(
0,
deck.newCardsPerDay - reviewedNewCards,
);
const newCardsToStudy = Math.min(dueNewCards, newCardBudget);
const reviewCardsToStudy = Math.min(
dueReviewCards,
REVIEW_CARDS_LIMIT,
);
const dueCardCount = newCardsToStudy + reviewCardsToStudy;
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");
}
const now = new Date();
const [dueNewCards, dueReviewCards, reviewedNewCards] = await Promise.all(
[
cardRepo.countDueNewCards(deck.id, now),
cardRepo.countDueReviewCards(deck.id, now),
reviewLogRepo.countTodayNewCardReviews(deck.id, now),
],
);
const newCardBudget = Math.max(0, deck.newCardsPerDay - reviewedNewCards);
const newCardsToStudy = Math.min(dueNewCards, newCardBudget);
const reviewCardsToStudy = Math.min(dueReviewCards, REVIEW_CARDS_LIMIT);
const dueCardCount = newCardsToStudy + reviewCardsToStudy;
return c.json({ deck: { ...deck, dueCardCount } }, 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,
reviewLogRepo: reviewLogRepository,
});
|