feat: MVP-4 Sprint 3 - Version history
- Add NoteVersion model for storing note snapshots - Add versions API (list, create, get, restore) - Add version history UI dialog in note detail page - Create version snapshot before each note update
This commit is contained in:
96
src/lib/versions.ts
Normal file
96
src/lib/versions.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { NotFoundError } from '@/lib/errors'
|
||||
|
||||
export async function createVersion(noteId: string): Promise<{ id: string; noteId: string; title: string; content: string; createdAt: Date }> {
|
||||
const note = await prisma.note.findUnique({
|
||||
where: { id: noteId },
|
||||
select: { id: true, title: true, content: true },
|
||||
})
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError('Note')
|
||||
}
|
||||
|
||||
const version = await prisma.noteVersion.create({
|
||||
data: {
|
||||
noteId: note.id,
|
||||
title: note.title,
|
||||
content: note.content,
|
||||
},
|
||||
})
|
||||
|
||||
return version
|
||||
}
|
||||
|
||||
export async function getVersions(noteId: string): Promise<{ id: string; noteId: string; title: string; content: string; createdAt: Date }[]> {
|
||||
const note = await prisma.note.findUnique({
|
||||
where: { id: noteId },
|
||||
})
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError('Note')
|
||||
}
|
||||
|
||||
return prisma.noteVersion.findMany({
|
||||
where: { noteId },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
select: {
|
||||
id: true,
|
||||
noteId: true,
|
||||
title: true,
|
||||
content: true,
|
||||
createdAt: true,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function getVersion(versionId: string): Promise<{ id: string; noteId: string; title: string; content: string; createdAt: Date }> {
|
||||
const version = await prisma.noteVersion.findUnique({
|
||||
where: { id: versionId },
|
||||
})
|
||||
|
||||
if (!version) {
|
||||
throw new NotFoundError('Version')
|
||||
}
|
||||
|
||||
return version
|
||||
}
|
||||
|
||||
export async function restoreVersion(noteId: string, versionId: string): Promise<{ id: string; title: string; content: string; updatedAt: Date }> {
|
||||
const note = await prisma.note.findUnique({
|
||||
where: { id: noteId },
|
||||
})
|
||||
|
||||
if (!note) {
|
||||
throw new NotFoundError('Note')
|
||||
}
|
||||
|
||||
const version = await prisma.noteVersion.findUnique({
|
||||
where: { id: versionId },
|
||||
})
|
||||
|
||||
if (!version) {
|
||||
throw new NotFoundError('Version')
|
||||
}
|
||||
|
||||
if (version.noteId !== noteId) {
|
||||
throw new NotFoundError('Version')
|
||||
}
|
||||
|
||||
const updatedNote = await prisma.note.update({
|
||||
where: { id: noteId },
|
||||
data: {
|
||||
title: version.title,
|
||||
content: version.content,
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
content: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
})
|
||||
|
||||
return updatedNote
|
||||
}
|
||||
Reference in New Issue
Block a user