My Store: select books and add them to chosen people's shelves
build-container / build (push) Successful in 3m53s
build-container / build (push) Successful in 3m53s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3376499805
commit
4e26bea455
5 files changed
+52
-2
No files matched your search
+17
@@ -154,6 +154,23 @@ async function handle(req, res) {
|
||||
if (req.method === 'DELETE') { needAdmin(me); fs.rm(coverOn(it), { force: true }, () => {}); it.cover = false; saveItems(l); return json(res, 200, view(it)); }
|
||||
if (req.method === 'GET') { if (!it.cover) throw fail(404, 'No cover'); return send(req, res, coverOn(it), it.cover, 'cover', false); }
|
||||
}
|
||||
if (p === '/people' && req.method === 'GET') { needAdmin(me); return json(res, 200, users.all().filter(u => !u.disabled).map(u => ({ id: u.id, name: u.name, books: users.can(u, 'read') }))); }
|
||||
if (p === '/assign' && req.method === 'POST') { // put the chosen books on the chosen people's shelves (admin). A book they already have is left alone.
|
||||
needAdmin(me); const b = JSON.parse(await readBody(req, 1e5) || '{}'), l = loadItems();
|
||||
const its = [...new Set(Array.isArray(b.items) ? b.items : [])].map(id => l.find(x => x.id === id)).filter(x => x && x.kind === 'book' && x.files[0]);
|
||||
const who = [...new Set(Array.isArray(b.users) ? b.users : [])].map(id => users.get(id)).filter(u => u && !u.disabled);
|
||||
if (!its.length) throw fail(400, 'Choose at least one book'); if (!who.length) throw fail(400, 'Choose at least one person');
|
||||
const out = [];
|
||||
for (const u of who) {
|
||||
const r = { id: u.id, name: u.name, added: 0, had: 0, failed: 0, skipped: !users.can(u, 'read') };
|
||||
if (!r.skipped) await users.runAs(u, async () => { for (const it of its) {
|
||||
try { const f = it.files[0], buf = fs.readFileSync(fileOn(it, f.id)); let cover; if (it.cover) { try { cover = { type: it.cover, data: fs.readFileSync(coverOn(it)) }; } catch {} }
|
||||
books.addBook(buf, f.ext, { title: it.title, author: it.creator }, cover).duplicate ? r.had++ : r.added++; } catch { r.failed++; }
|
||||
} });
|
||||
out.push(r);
|
||||
}
|
||||
return json(res, 200, { books: its.length, people: out });
|
||||
}
|
||||
if ((m = /^\/items\/([0-9a-f]{12})\/to-library$/.exec(p)) && req.method === 'POST') { // a book goes onto the person's own shelf
|
||||
if (!users.can(me, 'read')) throw fail(403, 'You do not have the Books tab');
|
||||
const it = loadItems().find(x => x.id === m[1]); if (!it || it.kind !== 'book' || !it.files[0]) throw fail(404, 'That is not a book');
|
||||
|
||||
Reference in new issue
Block a user