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
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PORT=9000
NODE_ENV=development
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
18 changes: 18 additions & 0 deletions api/recipe/recipe-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const tarifModel = require("./recipe-model");

const checkTarifId = async function (req, res, next) {
try {
const isExist = await tarifModel.idyeGoreTarifGetir(req.params.id);
if (!isExist.length == 0) {
res.status(404).json({ message: "tarif bulunamadı" });
} else {
req.tarif = isExist;
next();
}
} catch (error) {
next(error);
}
};
module.exports = {
checkTarifId,
};
64 changes: 64 additions & 0 deletions api/recipe/recipe-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const db = require("../../data/db-config");

const icindekileriGetir = async function (adim_id) {
const icindekiler = await db("icindekiler_adimlar as ia")
.leftJoin("icindekiler as i", "ia.icindekiler_id", "i.icindekiler_id")
.select("i.*")
.where("adim_id", adim_id);
return icindekiler;
};
const idyeGoreTarifGetir = async function (tarif_id) {
const tarifler = await db("tarifler as t")
.leftJoin("adimlar as a", "t.tarif_id", "a.tarif_id")
.leftJoin("icindekiler_adimlar as ia", "ia.adim_id", "a.adim_id")
.leftJoin("icindekiler as i", "i.icindekiler_id", "i.icindekiler_id")
.select(
"t.*",
"a.adim_id",
"a.adim_sirasi",
"a.adim_talimati",
"i.icindekiler_id",
"i.icindekiler_adi",
"i.miktar"
)
.where("t.tarif_id", tarif_id);
if (!tarifler.length === 0) {
return [];
}
const tarifModel = {
tarif_id: tarif_id,
tarif_adi: tarifler[0].tarif_adi,
kayit_tarihi: tarifler[0].kayit_tarihi,
adimlar: [],
};
// tarifler.forEach((tarif) => {//await olmadı
// const adimModel = {
// adim_sirasi: tarif.adim_sirasi,
// adim_id: tarif.adim_id,
// adim_talimati: tarif.adim_talimati,
// icindekiler: [],
// };
// const icindekiler = icindekileriGetir(tarif.adim_id).then(
// (icindekiler_data) => {
// return icindekiler_data;
// }
// );
// adimModel.icindekiler = icindekiler;
// tarifModel.adimlar.push(adimModel);
// });
//return tarifModel;
for (let i = 0; i < tarifler.length; i++) {
const tarif = tarifler[i];
const adimModel = {
adim_sirasi: tarif.adim_sirasi,
adim_id: tarif.adim_id,
adim_talimati: tarif.adim_talimati,
icindekiler: [],
};
const icindekiler = await icindekileriGetir(tarif.adim_id);
adimModel.icindekiler = icindekiler;
tarifModel.adimlar.push(adimModel);
}
return tarifModel;
};
module.exports = { idyeGoreTarifGetir };
13 changes: 13 additions & 0 deletions api/recipe/recipe-router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const express = require("express");
const router = express.Router();

const mw = require("./recipe-middleware");

router.get("/:id", mw.checkTarifId, async (req, res, next) => {
try {
res.json(req.tarif);
} catch (error) {
next(error);
}
});
module.exports = router;
10 changes: 10 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const express = require("express");

const tarifRouter = require("./recipe/recipe-router");

const server = express();

server.use(express.json());
server.use("/api/tarifler", tarifRouter);

module.exports = server;
6 changes: 6 additions & 0 deletions data/db-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const knex = require("knex");
const config = require("../knexfile.js");

const environment = process.env.NODE_ENV || "development";

module.exports = knex(config[environment]);
68 changes: 68 additions & 0 deletions data/migrations/20230302104220_create-recipe-table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
* Tarifler tablosu
tarif_id(pk) tarif_adi kayit_tarihi

Adımlar tablosu
adim_id(pk) adim_sirası adim_talimatı tarif_id(fk)

İçindekiler tablosu
icindekiler_id(pk) icindekiler_adi miktar

Ortak Tablo
icindekiler_id(fk) adim_id(fk) tarif_id(fk)
*/
exports.up = function (knex) {
return knex.schema
.createTable("tarifler", (tbl) => {
tbl.increments("tarif_id"); //tarif_id(pk)
tbl.string("tarif_adi", 128).notNullable().unique();
tbl.timestamp("kayit_tarihi").defaultTo(knex.fn.now()); //default olursa o günü alsın.
})
.createTable("adimlar", (tbl) => {
tbl.increments("adim_id"); //adim_id(pk)
tbl.integer("adim_sirasi").notNullable().unsigned();
tbl.string("adim_talimati").notNullable();
tbl
.integer("tarif_id")
.unsigned()
.notNullable()
.references("tarif_id")
.inTable("tarifler")
.onUpdate("CASCADE")
.onDelete("CASCADE"); //many to many //tarif_id(fk)
})
.createTable("icindekiler", (tbl) => {
tbl.increments("icindekiler_id"); //icindekiler_id(pk)
tbl.string("icindekiler_adi", 128).notNullable();
tbl.float("miktar").notNullable();
})
.createTable("icindekiler_adimlar", (tbl) => {
tbl.increments("icindekiler_adimlar_id");
tbl
.integer("adim_id")
.references("adim_id")
.inTable("adimlar")
.onUpdate("CASCADE")
.onDelete("CASCADE");
tbl
.integer("icindekiler_id")
.references("icindekiler_id")
.inTable("icindekiler")
.onUpdate("CASCADE")
.onDelete("CASCADE");
});
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema
.dropTableIfExists("icindekiler_adimlar")
.dropTableIfExists("adimlar")
.dropTableIfExists("icindekiler")
.dropTableIfExists("tarifler");
};
24 changes: 24 additions & 0 deletions data/seeds/001-tarifler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.seed = async function (knex) {
// Deletes ALL existing entries
await knex("tarifler").insert([{ tarif_adi: "Spagetti Bolonez" }]);
await knex("adimlar").insert([
{
adim_sirasi: 1,
adim_talimati: "Büyük bir tencereyi orta ateşe koyun",
tarif_id: 1,
},
{
adim_sirasi: 2,
adim_talimati: "1 yemek kaşığı zeytinyağı ekleyin",
tarif_id: 1,
},
]);
await knex("icindekiler").insert([
{ icindekiler_adi: "zeytinyağı", miktar: 0.014 },
]);
await knex("icindekiler_adimlar").insert([{ icindekiler_id: 1, adim_id: 1 }]);
};
Binary file added dev.sqlite3
Binary file not shown.
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const server = require("./api/server.js");
require("dotenv").config();

const PORT = process.env.PORT || 9000;

server.listen(PORT, () => {
console.log(`Listening on port ${PORT}...`);
});
58 changes: 58 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Update with your config settings.

/**
* @type { Object.<string, import("knex").Knex.Config> }
*/
module.exports = {
development: {
client: "sqlite3",
connection: {
filename: "./dev.sqlite3",
},
useNullAsDefault: true,
pool: {
afterCreate: (conn, done) => {
// sqlite engine'e bağlandığımızda aşağıdaki kod çalışacak:
conn.run("PRAGMA foreign_keys = ON", done); // FK kullanımını açmaya zorlayacak
},
},
migrations: {
directory: "./data/migrations",
},
seeds: {
directory: "./data/seeds",
},
},

// staging: {
// client: 'postgresql',
// connection: {
// database: 'my_db',
// user: 'username',
// password: 'password'
// },
// pool: {
// min: 2,
// max: 10
// },
// migrations: {
// tableName: 'knex_migrations'
// }
// },

// production: {
// client: 'postgresql',
// connection: {
// database: 'my_db',
// user: 'username',
// password: 'password'
// },
// pool: {
// min: 2,
// max: 10
// },
// migrations: {
// tableName: 'knex_migrations'
// }
// }
};
Loading