initial commit
This commit is contained in:
commit
cd234531b9
37 changed files with 5529 additions and 0 deletions
1
backend/.gitignore
vendored
Normal file
1
backend/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
node_modules
|
10
backend/db.js
Normal file
10
backend/db.js
Normal file
|
@ -0,0 +1,10 @@
|
|||
const knex = require('knex');
|
||||
const knexConfig = require('./knexfile.js');
|
||||
|
||||
const environment = process.env.NODE_ENV || 'development';
|
||||
const config = knexConfig[environment];
|
||||
|
||||
const db = knex(config);
|
||||
|
||||
module.exports = db;
|
||||
|
40
backend/knexfile.js
Normal file
40
backend/knexfile.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
require('dotenv').config();
|
||||
|
||||
module.exports = {
|
||||
|
||||
development: {
|
||||
client: 'postgresql',
|
||||
connection: {
|
||||
host: process.env.DB_HOST,
|
||||
port: process.env.DB_PORT,
|
||||
database: process.env.DB_NAME,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD
|
||||
},
|
||||
pool: {
|
||||
min: 2,
|
||||
max: 10
|
||||
},
|
||||
migrations: {
|
||||
tableName: 'knex_migrations',
|
||||
directory: './migrations'
|
||||
}
|
||||
},
|
||||
|
||||
production: {
|
||||
client: 'postgresql',
|
||||
connection: {
|
||||
database: 'my_db',
|
||||
user: 'username',
|
||||
password: 'password'
|
||||
},
|
||||
pool: {
|
||||
min: 2,
|
||||
max: 10
|
||||
},
|
||||
migrations: {
|
||||
tableName: 'knex_migrations'
|
||||
}
|
||||
}
|
||||
|
||||
};
|
21
backend/migrations/20250702165756_create_recipes_table.js
Normal file
21
backend/migrations/20250702165756_create_recipes_table.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.up = function (knex) {
|
||||
return knex.schema.createTable('recipes', function (table) {
|
||||
table.increments('id').primary();
|
||||
table.string('name').notNullable().unique();
|
||||
table.string('cuisine').notNullable();
|
||||
table.timestamps(true, true);
|
||||
})
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.down = function (knex) {
|
||||
return knex.schema.dropTable('users');
|
||||
};
|
1244
backend/package-lock.json
generated
Normal file
1244
backend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
20
backend/package.json
Normal file
20
backend/package.json
Normal file
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"name": "backend",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"dev": "node server.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"dependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^17.0.1",
|
||||
"express": "^5.1.0",
|
||||
"knex": "^3.1.0",
|
||||
"pg": "^8.16.3"
|
||||
}
|
||||
}
|
63
backend/server.js
Normal file
63
backend/server.js
Normal file
|
@ -0,0 +1,63 @@
|
|||
const express = require("express");
|
||||
const db = require("./db");
|
||||
const port = 6063;
|
||||
const cors = require('cors'); // to remove cors origin error in dev TODO: remove when dockerized
|
||||
|
||||
const app = express();
|
||||
app.use(cors()); // to remove cors origin error in dev TODO: remove when dockerized
|
||||
app.use(express.json());
|
||||
|
||||
// routes
|
||||
app.get("/recipes", async (req, res) => {
|
||||
try {
|
||||
const recipes = await db('recipes').select('id', 'name');
|
||||
res.json(recipes);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/recipe/:id", async (req, res) => {
|
||||
const id = req.params.id
|
||||
try {
|
||||
const recipe = await db('recipes').where('id', '=', id).select('id', 'name', 'cuisine').first();
|
||||
res.json(recipe);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.post("/add-recipe", async (req, res) => {
|
||||
const { name, cuisine } = req.body;
|
||||
try {
|
||||
const [id] = await db('recipes').insert({
|
||||
name: name,
|
||||
cuisine: cuisine
|
||||
}, ['id'])
|
||||
res.status(200).send({ message: "Recipe added", id: id.id });
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.delete("/delete-recipe", async (req, res) => {
|
||||
const { id } = req.body;
|
||||
try {
|
||||
await db('recipes').where({ id: id }).del();
|
||||
res.status(200).send({ message: "Recipe deleted" });
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(port, () => console.log(`Server has started on port: ${port}`));
|
||||
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('Closing database connection...');
|
||||
await db.destroy();
|
||||
process.exit(0);
|
||||
});
|
14
backend/test.js
Normal file
14
backend/test.js
Normal file
|
@ -0,0 +1,14 @@
|
|||
const db = require('./db');
|
||||
|
||||
async function testConnection() {
|
||||
try {
|
||||
await db.raw('SELECT 1+1 as result');
|
||||
console.log('Database connected successfully!');
|
||||
} catch (error) {
|
||||
console.error('Database connection failed:', error);
|
||||
} finally {
|
||||
await db.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
testConnection();
|
Loading…
Add table
Add a link
Reference in a new issue