Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
53 changes: 45 additions & 8 deletions api/accounts/accounts-middleware.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
43 changes: 33 additions & 10 deletions api/accounts/accounts-model.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -24,4 +46,5 @@ module.exports = {
create,
updateById,
deleteById,
}
getByName
}
66 changes: 48 additions & 18 deletions api/accounts/accounts-router.js
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 8 additions & 1 deletion api/server.js
Original file line number Diff line number Diff line change
@@ -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;
Binary file added data/testing.db3
Binary file not shown.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions queries.sql
Original file line number Diff line number Diff line change
@@ -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.
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