/
Coderdev
/
web-nodejs-labs
Обзор
Документация
Войти
/
Coderdev
/
web-nodejs-labs
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
backend/src/routes/lab9/index.js
92 строки
3 KB
Coderdev
1
12 май 2026, 12:34
12 май 2026, 12:34
f5e899f
Код
Авторство
О чём код?
// backend/src/routes/lab9/index.js import productService from '../../services/lab9/product.service.js'; export default async function routes(fastify, options) { // ─── Базовый пример ─────────────────────────────────────────────── // GET /api/lab9/products fastify.get('/', async (request, reply) => { const products = await productService.getAll(fastify.pg); return products; }); // GET /api/lab9/products/:id fastify.get('/:id', async (request, reply) => { const product = await productService.getById(fastify.pg, request.params.id); if (!product) { return reply.code(404).send({ error: 'Product not found' }); } return product; }); // POST /api/lab9/products fastify.post('/', async (request, reply) => { const newProduct = await productService.create(fastify.pg, request.body); reply.code(201).send(newProduct); }); // PATCH /api/lab9/products/:id fastify.patch('/:id', async (request, reply) => { const updated = await productService.update(fastify.pg, request.params.id, request.body); if (!updated) { return reply.code(404).send({ error: 'Product not found' }); } return updated; }); // DELETE /api/lab9/products/:id fastify.delete('/:id', async (request, reply) => { const deleted = await productService.delete(fastify.pg, request.params.id); if (!deleted) { return reply.code(404).send({ error: 'Product not found' }); } return { message: 'Deleted', id: deleted.id }; }); // ─── Задание 1 ──────────────────────────────────────────────────── // GET /api/lab9/products/task1/stock fastify.get('/task1/stock', async (request, reply) => { const stock = await productService.getStock(fastify.pg); return stock; }); // GET /api/lab9/products/task1/products?q= fastify.get('/task1/products', async (request, reply) => { const { q } = request.query; const products = await productService.search(fastify.pg, q); return products; }); // ─── Задание 3 (корзина) ────────────────────────────────────────── // POST /api/lab9/products/cart/check // body: { items: [{id, qty}] } fastify.post('/cart/check', async (request, reply) => { const { items } = request.body; if (!items || !Array.isArray(items) || items.length === 0) { return reply.code(400).send({ error: 'items required' }); } const results = await productService.checkStock(fastify.pg, items); const failed = results.filter(r => !r.ok); if (failed.length > 0) { return reply.code(400).send({ ok: false, failed }); } return { ok: true }; }); // POST /api/lab9/products/cart/buy // body: { items: [{id, qty}] } fastify.post('/cart/buy', async (request, reply) => { const { items } = request.body; if (!items || !Array.isArray(items) || items.length === 0) { return reply.code(400).send({ error: 'items required' }); } const result = await productService.purchase(fastify.pg, items); if (!result.success) { return reply.code(400).send({ ok: false, failed: result.failed }); } return { ok: true, message: 'Покупка успешно оформлена' }; }); }