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 .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=9000;
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
8 changes: 8 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require("express");
const server = express();
const tarifRouter = require("./tarifler/tarif-router");

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

module.exports = server;
22 changes: 22 additions & 0 deletions api/tarifler/tarif-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const tarifModel = require("../tarifler/tarif-model");


const checkTarifId = async function (req, res, next) {
try {
const isExist = await tarifModel.idyeGoreTarifGetir(req.params.id);
if (isExist.length == 0) {
res.status(404).message({ message: `${req.params.id} id'li tarif bulunamadı` });

} else {
req.tarif = isExist;
next();
}

} catch (error) {
next(error);

}
}
module.exports = {
checkTarifId
}
40 changes: 40 additions & 0 deletions api/tarifler/tarif-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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.*")
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", "ia.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],
kayit_tarihi: tarifler[0].kayit_tarihi,
adimlar: []
}
tarifler.forEach(async (tarif) => {


});
for (let i = 0; i < tarifler.length; i++) {
const tarif = tarifler[i];
}
return tarifModel;
}
module.exports = {
idyeGoreTarifGetir
}
14 changes: 14 additions & 0 deletions api/tarifler/tarif-router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const express = require("express");
const router = express.Router();

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

router.get("/:id", mw.checkTarifId, (req, res, next) => {
try {
res.json(req.tarif);

} catch (error) {
next(error);
}
});
module.exports = router;
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 configfile = require("../knexfile");


const environmemnt = "development";
module.exports = knex(configFile[environment]);
47 changes: 47 additions & 0 deletions data/migrations/20230304212152_tarifler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
const all = knex.schema.createTable("tarifler", t => {
t.increments("tarif_id"),
t.string("tarif_adi").notNullable().unique()
t.timestamp("kayit_tarihi").defaultTo(knex.fn.now())

})
.createTable("adimlar", t => {
t.increments("adim_id")
t.integer("adim_sirasi").notNullable().unsigned()
t.string("adim_talimati").notNullable()
t.integer("tarif_id").notNullable().unsigned()
.references("tarif_id")
.inTable("tarifler")
})
.createTable("icindekiler", t => {
t.increments("icindekiler_id")
t.string("icindekiler_adi").notNullable()
t.float("miktar").notNullable()
})
.createTable("icindekiler_adimlar", t => {
t.increments("icindekiler_adimlar_id")
t.integer("adim_id")
.references("adim_id")
.inTable("adimlar")
t.integer("icindekiler_id")
.references("icindekiler_id")
.inTable("icindekiler")
});
return all;
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema
.dropTableIfExists("icindekiler_adimlar")
.dropTableIfExists("adimlar")
.dropTableIfExists("icindekiler")
.dropTableIfExists("tarifler")
};
Binary file added dev.sqlite3
Binary file not shown.
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const server = require("./api/server");

const port = 9001;

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

/**
* @type { Object.<string, import("knex").Knex.Config> }
*/
module.exports = {
development: {
client: "sqlite3",
connection: {
filename: "./dev.sqlite3"
},
migrations: {
directory: "./data/migrations"
},
seeds: {
directory: "./data/seeds "
},
useNullAsDefault: true,
pool: {
afterCreate: (conn, done) => {
conn.run("PRAGMA foreign_keys=ON", done);
}
}
}
};
Loading