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
77 changes: 71 additions & 6 deletions api/accounts/accounts-middleware.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,77 @@
const Account = require("./accounts-model");
const yup = require("yup");
//yup ile yaparsak:
const accountSchema = yup.object().shape({
name: yup
.string()
.required("name and budget are required")
.min(3, "name of account must be between 3 and 100")
.max(100, "name of account must be between 3 and 100"),
budget: yup
.number("budget of account must be a number")
.required("name and budget are required")
.min(0, "budget of account is too large or too small")
.max(1000000, "budget of account is too large or too small"),
});
exports.checkAccountPayload = (req, res, next) => {
// KODLAR BURAYA
// Not: Validasyon için Yup(şu an yüklü değil!) kullanabilirsiniz veya kendiniz manuel yazabilirsiniz.
}
//yup ile yaparsak:(async ile yaptık)
// try {
// await accountSchema.validate(req.body);
// req.account=req.body
// } catch (error) {
// res.status(400).json({mesaage:error.mesaage || "Hata oluştu"})
// }
const { name, budget } = req.body;
if (!name || 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") {
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" });
}
next();
};

exports.checkAccountNameUnique = (req, res, next) => {
exports.checkAccountNameUnique = async (req, res, next) => {
// KODLAR BURAYA
}
try {
req.body.name = req.body.name.trim();
//await accountSchema.validate(req.body);
let allAccount = await Account.getAll();
let isFound = false;
for (let i = 0; i < allAccount.length; i++) {
if (allAccount[i].name == req.body.name) {
isFound = true;
break;
}
}
if (isFound) {
res.status(400).json({ message: "that name is taken" });
}
} catch (error) {
next(error);
}
next();
};

exports.checkAccountId = (req, res, next) => {
// KODLAR BURAYA
}
exports.checkAccountId = async (req, res, next) => {
try {
const account = await Account.getById(req.params.id);
if (!account) {
res.status(404).json({ message: "account not found" });
} else {
req.account = account;
next();
}
} catch (error) {
next(error);
}
};
46 changes: 32 additions & 14 deletions api/accounts/accounts-model.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
const db = require("../../data/db-config");

const getAll = () => {
// KODLAR BURAYA
}
//select * from accounts
return db("accounts");
};

const getById = id => {
// KODLAR BURAYA
}
const getById = (id) => {
//select * from accounts where id=id
// return db("accounts").where({id}).first();
return db("accounts").where("id", id).first();
};

const create = account => {
// KODLAR BURAYA
}
const create = (account) => {
//inset into account values (id,name,budget)
const insertedAccount = db("accounts")
.insert(account)
.then((id) => {
return getById(id[0]);
});
return insertedAccount;
};

const updateById = (id, account) => {
// KODLAR BURAYA
}
// update accounts name=account.name, budget=account.budget where id=id
return db("accounts")
.where("id", id)
.update(account)
.then((rows) => {
return getById(id);
});
};

const deleteById = id => {
// KODLAR BURAYA
}
const deleteById = (id) => {
// delete from accounts where id=id
return db("accounts").where("id", id).del();
};

module.exports = {
getAll,
getById,
create,
updateById,
deleteById,
}
};
78 changes: 61 additions & 17 deletions api/accounts/accounts-router.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,71 @@
const router = require('express').Router()
const router = require("express").Router();
const Account = require("./accounts-model");
const mw = require("./accounts-middleware");

router.get('/', (req, res, next) => {
// KODLAR BURAYA
})
router.get("/", (req, res, next) => {
Account.getAll()
.then((account) => {
res.json(account);
})
.catch((err) => {
res.json([]);
});
});

router.get('/:id', (req, res, next) => {
// KODLAR BURAYA
})
router.get("/:id", mw.checkAccountId, async (req, res, next) => {
try {
res.json(req.account);
} catch (error) {
next(error);
}
});

router.post('/', (req, res, next) => {
// KODLAR BURAYA
})
router.post(
"/",
mw.checkAccountPayload,
mw.checkAccountNameUnique,
async (req, res, next) => {
// KODLAR BURAYA
try {
let insertData = await Account.create(req.body);
res.status(201).json(insertData);
} catch (error) {
next(error);
}
}
);

router.put('/:id', (req, res, next) => {
// KODLAR BURAYA
});
router.put(
"/:id",
mw.checkAccountId,
mw.checkAccountPayload,
async (req, res, next) => {
try {
const updateAccount = await Account.updateById(req.params.id, req.body);
res.json(updateAccount);
} catch (error) {
next(error);
}
}
);

router.delete('/:id', (req, res, next) => {
router.delete("/:id", mw.checkAccountId, async (req, res, next) => {
// KODLAR BURAYA
})
try {
await Account.deleteById(req.params.id);
res.json(req.account);
} catch (error) {
next(error);
}
});

router.use((err, req, res, next) => { // eslint-disable-line
router.use((err, req, res, next) => {
// eslint-disable-line
// KODLAR BURAYA
})
res.status(err.status || 500).json({
customMessage: "Bir hata oluştu",
message: err.message,
});
});

module.exports = router;
7 changes: 7 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ const server = express();

server.use(express.json());

const userAccount = require("./accounts/accounts-router");

server.use("/api/accounts", userAccount);
server.get("/", (req, res) => {
res.json({ message: "Hey, server is up and running..." });
});

module.exports = server;
Binary file modified data/budget.db3
Binary file not shown.
Binary file added data/testing.db3
Binary file not shown.
77 changes: 74 additions & 3 deletions package-lock.json

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

Loading