From 5b20a7805c020665d0c622cdb55f8baf3889fded Mon Sep 17 00:00:00 2001 From: numex-admin Date: Thu, 10 Sep 2026 08:59:30 +0000 Subject: [PATCH] =?UTF-8?q?Numex=20yay=C4=B1n:=20routes/urunler.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/urunler.js | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 routes/urunler.js diff --git a/routes/urunler.js b/routes/urunler.js new file mode 100644 index 0000000..a995b69 --- /dev/null +++ b/routes/urunler.js @@ -0,0 +1,48 @@ +// Ürün rotaları +const router = require('express').Router(); +const store = require('../models/store'); +const urunModel = require('../models/urun'); + +// Tüm ürünleri listele +router.get('/', (req, res) => { + res.json(urunModel.tumu()); +}); + +// Tek ürün +router.get('/:id', (req, res) => { + const urun = urunModel.bul(req.params.id); + if (!urun) return res.status(404).json({ hata: 'Ürün bulunamadı' }); + res.json(urun); +}); + +// Yeni ürün ekle +router.post('/', (req, res) => { + const hatalar = urunModel.dogrula(req.body); + if (hatalar.length) return res.status(400).json({ hata: hatalar }); + + const urunler = urunModel.tumu(); + const yeni = { + id: store.yeniId('urunler'), + ad: req.body.ad.trim(), + birim: (req.body.birim || 'adet').trim(), + dusukStokEsigi: Number(req.body.dusukStokEsigi) || 0, + olusturuldu: new Date().toISOString() + }; + urunler.push(yeni); + store.yaz('urunler', urunler); + res.status(201).json(yeni); +}); + +// Ürün sil +router.delete('/:id', (req, res) => { + const urunler = urunModel.tumu(); + const urun = urunler.find(x => x.id === Number(req.params.id)); + if (!urun) return res.status(404).json({ hata: 'Ürün bulunamadı' }); + store.yaz('urunler', urunler.filter(x => x.id !== urun.id)); + + const stok = store.oku('stok'); + store.yaz('stok', stok.filter(s => s.urunId !== urun.id)); + res.json({ mesaj: 'Ürün silindi', id: urun.id }); +}); + +module.exports = router;