Spaces:
Sleeping
Sleeping
File size: 935 Bytes
5647c24 | 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 | const db = require('../db/database');
const { v4: uuidv4 } = require('uuid');
const UserNote = {
create(data) {
if (!data.user_id) throw new Error('UserNote.create: user_id is required');
if (!data.title) throw new Error('UserNote.create: title is required');
if (!data.content) throw new Error('UserNote.create: content is required');
const id = data.id || uuidv4();
db.prepare(`
INSERT INTO user_notes (id, user_id, title, content)
VALUES (?, ?, ?, ?)
`).run(id, data.user_id, data.title, data.content);
return this.findById(id);
},
findById(id) {
return db.prepare('SELECT * FROM user_notes WHERE id = ?').get(id) || null;
},
findByUserId(userId) {
return db.prepare('SELECT * FROM user_notes WHERE user_id = ? ORDER BY created_at DESC').all(userId);
},
delete(id) {
db.prepare('DELETE FROM user_notes WHERE id = ?').run(id);
},
};
module.exports = UserNote;
|