diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..b512c09d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/api/accounts/accounts-middleware.js b/api/accounts/accounts-middleware.js index ca95d597..26015052 100644 --- a/api/accounts/accounts-middleware.js +++ b/api/accounts/accounts-middleware.js @@ -1,12 +1,49 @@ -exports.checkAccountPayload = (req, res, next) => { - // KODLAR BURAYA - // Not: Validasyon için Yup(şu an yüklü değil!) kullanabilirsiniz veya kendiniz manuel yazabilirsiniz. -} +const accountsModel = require('./accounts-model'); -exports.checkAccountNameUnique = (req, res, next) => { - // KODLAR BURAYA +exports.checkAccountId = async (req, res, next) => { + try { + const isExistID = await accountsModel.getById(req.params.id); + if (!isExistID) { + res.status(404).json({ message: "account not found" }); + } else { + req.existUserID = isExistID; + next(); + } + } catch (error) { + next(error); + } } -exports.checkAccountId = (req, res, next) => { - // KODLAR BURAYA +exports.checkAccountPayload = (req, res, next) => { + try { + let { name, budget } = req.body; + + if (name === undefined || budget === undefined) { + res.status(400).json({ message: "name and budget are required" }) + } else if (name.trim().length < 3 || name.trim().length > 100) { + res.status(400).json({ message: "name of account must be between 3 and 100" }) + } else if (typeof budget !== "number" || isNaN(budget)) { + res.status(400).json({ message: "budget of account must be a number" }) + } else if (budget < 0 || budget > 1000000) { + res.status(400).json({ message: "budget of account is too large or too small" }); + } else { + req.body.name = req.body.name.trim(); + next(); + } + } catch (error) { + next(error); + } } + +exports.checkAccountNameUnique = async (req, res, next) => { + try { + const isExist = await accountsModel.getByName(req.body.name); + if (isExist) { + res.status(400).json({ message: "that name is taken" }); + } else { + next(); + } + } catch (error) { + next(error); + } +} \ No newline at end of file diff --git a/api/accounts/accounts-model.js b/api/accounts/accounts-model.js index b963499f..ee64a585 100644 --- a/api/accounts/accounts-model.js +++ b/api/accounts/accounts-model.js @@ -1,21 +1,43 @@ +const db = require('../../data/db-config') + const getAll = () => { - // KODLAR BURAYA + /* + SELECT * + FROM [Accounts] + */ + return db("accounts"); +} + +const getById = (id) => { + /* + SELECT * + FROM [Accounts] + WHERE id = '{id}' limit 1 + */ + return db("accounts").where("id", id).first(); // first: array'in ilk objesi } -const getById = id => { - // KODLAR BURAYA +const getByName = (name) => { + /* + SELECT * + FROM [Accounts] + WHERE name = '{name}' limit 1 + */ + return db("accounts").where("name", name).first(); // first: array'in ilk objesi } -const create = account => { - // KODLAR BURAYA +const create = async (account) => { + const [id] = await db("accounts").insert(account); + return getById(id); } -const updateById = (id, account) => { - // KODLAR BURAYA +const updateById = async (id, account) => { + await db("accounts").where("id", id).update(account); + return getById(id); } -const deleteById = id => { - // KODLAR BURAYA +const deleteById = (id) => { + return db("accounts").where("id", id).del(); } module.exports = { @@ -24,4 +46,5 @@ module.exports = { create, updateById, deleteById, -} + getByName +} \ No newline at end of file diff --git a/api/accounts/accounts-router.js b/api/accounts/accounts-router.js index a8ba969f..d3c5a3ed 100644 --- a/api/accounts/accounts-router.js +++ b/api/accounts/accounts-router.js @@ -1,27 +1,57 @@ -const router = require('express').Router() +const router = require('express').Router(); +const accountsModel = require('./accounts-model'); +const mw = require('./accounts-middleware'); -router.get('/', (req, res, next) => { - // KODLAR BURAYA -}) +router.get('/', async (req, res, next) => { + try { + const allAccounts = await accountsModel.getAll(); + res.json(allAccounts); + } catch (error) { + next(error); + }; +}); -router.get('/:id', (req, res, next) => { - // KODLAR BURAYA -}) +router.get('/:id', mw.checkAccountId, (req, res, next) => { + try { + res.json(req.existUserID); + } catch (error) { + next(error); + }; +}); -router.post('/', (req, res, next) => { - // KODLAR BURAYA +router.post('/', mw.checkAccountPayload, mw.checkAccountNameUnique, async (req, res, next) => { + try { + const createdPost = { + name: req.body.name, + budget: req.body.budget + } + const insertedAcc = await accountsModel.create(createdPost); + res.status(201).json(insertedAcc) + } catch (error) { + next(error); + } }) -router.put('/:id', (req, res, next) => { - // KODLAR BURAYA -}); - -router.delete('/:id', (req, res, next) => { - // KODLAR BURAYA +router.put('/:id', mw.checkAccountId, mw.checkAccountPayload, mw.checkAccountNameUnique, async (req, res, next) => { + try { + const updatePost = { + name: req.body.name, + budget: req.body.budget + } + const updatedAcc = await accountsModel.updateById(req.params.id, updatePost); + res.status(200).json(updatedAcc) + } catch (error) { + next(error); + } }) -router.use((err, req, res, next) => { // eslint-disable-line - // KODLAR BURAYA +router.delete('/:id', mw.checkAccountId, async (req, res, next) => { + try { + await accountsModel.deleteById(req.params.id); + res.json({ message: `${req.params.id} is successfully deleted...` }) + } catch (error) { + next(error); + } }) -module.exports = router; +module.exports = router; \ No newline at end of file diff --git a/api/server.js b/api/server.js index 5d25c978..2a096cc0 100644 --- a/api/server.js +++ b/api/server.js @@ -1,7 +1,14 @@ const express = require("express"); +const accountRouter = require("./accounts/accounts-router"); const server = express(); server.use(express.json()); -module.exports = server; +server.use("/api/accounts", accountRouter); + +server.use((err, req, res, next) => { // eslint-disable-line + res.status((err.status || 500)).json({ message: "SERVER ERROR, PLEASE TRY AGAIN LATER" }) +}) + +module.exports = server; \ No newline at end of file diff --git a/data/testing.db3 b/data/testing.db3 new file mode 100644 index 00000000..48d1efb5 Binary files /dev/null and b/data/testing.db3 differ diff --git a/package-lock.json b/package-lock.json index 4ce88d1e..4242d68f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "node-db1-project", + "name": "node-db-project-1", "version": "1.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "node-db1-project", + "name": "node-db-project-1", "version": "1.0.0", "dependencies": { "express": "^4.18.1", diff --git a/queries.sql b/queries.sql index 6c251068..77043865 100644 --- a/queries.sql +++ b/queries.sql @@ -1,17 +1,18 @@ -- Veritabanı Sorguları -- Posta kodu 1010 olan tüm müşterileri bulun - +SELECT * FROM [Customers] as "Müşteriler" WHERE PostalCode = '1010'; -- id'si 11 olan tedarikçinin telefon numarasını bulun - +SELECT Phone FROM [Suppliers] as "Tedarikçiler" WHERE SupplierID = '11'; -- Verilen ilk 10 siparişi, sipariş tarihine göre azalan şekilde listeleyin - +SELECT * FROM [Orders] ORDER BY OrderDate DESC LIMIT 10; -- Londra, Madrid veya Brezilya'da yaşayan tüm müşterileri bulun - +SELECT * FROM [Customers] WHERE City IN ('London', 'Madrid') OR Country = 'Brasil'; -- "The Shire" için bir müşteri kaydı ekleyin, ilgili kişi adı "Bilbo Baggins", adres - "Bag End" içinde "1 Hobbit-Hole", posta kodu "111" ve ülke "Middle Earth" - +INSERT INTO Customers VALUES (93, 'Bilbo Baggins', 'Bilbo Baggins', '1 Hobbit Hole, Bag End', 'The Shire', '111', 'Middle Earth') -- Posta kodu "11122" olarak değişecek şekilde Bilbo Baggins kaydını güncelleyin - +UPDATE [Customers] SET PostalCode = '11122' WHERE ContactName = 'Bilbo Baggins'; -- (Zorlayıcı Görev) Müşteriler tablosunda kaç farklı şehrin saklandığını keşfetmek için bir sorgu bulun. Tekrarlar çift sayılmamalıdır - --- (Zorlayıcı Görev) 20 karakterden uzun adları olan tüm tedarikçileri bulun. Adın uzunluğunu almak için "length(SupplierName)" kullanabilirsiniz. \ No newline at end of file +SELECT COUNT(DISTINCT City) FROM Customers +-- (Zorlayıcı Görev) 20 karakterden uzun adları olan tüm tedarikçileri bulun. Adın uzunluğunu almak için "length(SupplierName)" kullanabilirsiniz. +SELECT * FROM [Suppliers] WHERE length(SupplierName) > 20 \ No newline at end of file