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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import { z } from "zod";
import { authMiddleware, Errors, getAuthUser } from "../middleware/index.js";
import {
type DeckRepository,
deckRepository,
type NoteRepository,
noteRepository,
} from "../repositories/index.js";
import { createNoteSchema, updateNoteSchema } from "../schemas/index.js";
export interface NoteDependencies {
noteRepo: NoteRepository;
deckRepo: DeckRepository;
}
const deckIdParamSchema = z.object({
deckId: z.uuid(),
});
const noteIdParamSchema = z.object({
deckId: z.uuid(),
noteId: z.uuid(),
});
export function createNotesRouter(deps: NoteDependencies) {
const { noteRepo, deckRepo } = deps;
return (
new Hono()
.use("*", authMiddleware)
// List notes in deck
.get("/", zValidator("param", deckIdParamSchema), async (c) => {
const user = getAuthUser(c);
const { deckId } = c.req.valid("param");
const deck = await deckRepo.findById(deckId, user.id);
if (!deck) {
throw Errors.notFound("Deck not found", "DECK_NOT_FOUND");
}
const notes = await noteRepo.findByDeckId(deckId);
return c.json({ notes }, 200);
})
// Create note (auto-generates cards)
.post(
"/",
zValidator("param", deckIdParamSchema),
zValidator("json", createNoteSchema),
async (c) => {
const user = getAuthUser(c);
const { deckId } = c.req.valid("param");
const data = c.req.valid("json");
const deck = await deckRepo.findById(deckId, user.id);
if (!deck) {
throw Errors.notFound("Deck not found", "DECK_NOT_FOUND");
}
try {
const result = await noteRepo.create(deckId, {
noteTypeId: data.noteTypeId,
fields: data.fields,
});
return c.json(
{
note: result.note,
fieldValues: result.fieldValues,
cards: result.cards,
},
201,
);
} catch (error) {
if (
error instanceof Error &&
error.message === "Note type not found"
) {
throw Errors.notFound(
"Note type not found",
"NOTE_TYPE_NOT_FOUND",
);
}
throw error;
}
},
)
// Get note with field values
.get("/:noteId", zValidator("param", noteIdParamSchema), async (c) => {
const user = getAuthUser(c);
const { deckId, noteId } = c.req.valid("param");
const deck = await deckRepo.findById(deckId, user.id);
if (!deck) {
throw Errors.notFound("Deck not found", "DECK_NOT_FOUND");
}
const note = await noteRepo.findByIdWithFieldValues(noteId, deckId);
if (!note) {
throw Errors.notFound("Note not found", "NOTE_NOT_FOUND");
}
return c.json({ note }, 200);
})
// Update note field values
.put(
"/:noteId",
zValidator("param", noteIdParamSchema),
zValidator("json", updateNoteSchema),
async (c) => {
const user = getAuthUser(c);
const { deckId, noteId } = c.req.valid("param");
const data = c.req.valid("json");
const deck = await deckRepo.findById(deckId, user.id);
if (!deck) {
throw Errors.notFound("Deck not found", "DECK_NOT_FOUND");
}
const note = await noteRepo.update(noteId, deckId, data.fields);
if (!note) {
throw Errors.notFound("Note not found", "NOTE_NOT_FOUND");
}
return c.json({ note }, 200);
},
)
// Delete note and its cards
.delete("/:noteId", zValidator("param", noteIdParamSchema), async (c) => {
const user = getAuthUser(c);
const { deckId, noteId } = c.req.valid("param");
const deck = await deckRepo.findById(deckId, user.id);
if (!deck) {
throw Errors.notFound("Deck not found", "DECK_NOT_FOUND");
}
const deleted = await noteRepo.softDelete(noteId, deckId);
if (!deleted) {
throw Errors.notFound("Note not found", "NOTE_NOT_FOUND");
}
return c.json({ success: true }, 200);
})
);
}
export const notes = createNotesRouter({
noteRepo: noteRepository,
deckRepo: deckRepository,
});
|