initial production commit

This commit is contained in:
Fred 2025-07-25 18:07:20 +00:00
parent 6f43d17ddd
commit 773b4773eb
9 changed files with 116 additions and 93 deletions

View file

@ -10,4 +10,4 @@ COPY . .
EXPOSE 3000
CMD ["npm", "run", "dev"]
CMD ["npm", "run", "production"]

View file

@ -1,21 +1,26 @@
const express = require("express");
const db = require("./db");
const port = 3000;
const cors = require('cors')
const cors = require("cors");
const app = express();
app.use(cors());
app.use(
cors({
origin: "https://ec683cee72d30c5030.fredzernia.com/",
credentials: true,
}),
);
app.use(express.json());
// ####### ROUTES #######
app.get("/test", async (req, res) => {
console.log('test')
res.json({ test: 'test' })
app.get("/backend/test", async (req, res) => {
console.log("test");
res.json({ test: "test" });
});
// ### GET ALL RECIPES ###
app.get("/recipes", async (req, res) => {
app.get("/backend/recipes", async (req, res) => {
try {
const recipes = await db('recipes').select('id', 'name', 'cuisine');
const recipes = await db("recipes").select("id", "name", "cuisine");
res.json(recipes);
} catch (err) {
console.log(err);
@ -23,9 +28,9 @@ app.get("/recipes", async (req, res) => {
}
});
// ### GET ALL RECIPE_INGREDIENTS ###
app.get("/recipe-ingredients", async (req, res) => {
app.get("/backend/recipe-ingredients", async (req, res) => {
try {
const recipe_ingredients = await db('recipe_ingredients').select('*');
const recipe_ingredients = await db("recipe_ingredients").select("*");
res.json(recipe_ingredients);
} catch (err) {
console.log(err);
@ -33,9 +38,9 @@ app.get("/recipe-ingredients", async (req, res) => {
}
});
// ### GET ALL RECIPE_STEPS ###
app.get("/recipe-steps", async (req, res) => {
app.get("/backend/recipe-steps", async (req, res) => {
try {
const recipe_steps = await db('recipe_steps').select('*');
const recipe_steps = await db("recipe_steps").select("*");
res.json(recipe_steps);
} catch (err) {
console.log(err);
@ -44,21 +49,32 @@ app.get("/recipe-steps", async (req, res) => {
});
// ### GET RECIPE ###
app.get("/recipe/:id", async (req, res) => {
const id = req.params.id
app.get("/backend/recipe/:id", async (req, res) => {
const id = req.params.id;
try {
const recipeQuery = db('recipes').where('id', '=', id).select('id', 'name', 'author', 'cuisine', 'stars');
const recipeQuery = db("recipes")
.where("id", "=", id)
.select("id", "name", "author", "cuisine", "stars");
const ingredientsQuery = db.from('recipe_ingredients').where('recipe_id', '=', id).select('raw');
const ingredientsQuery = db
.from("recipe_ingredients")
.where("recipe_id", "=", id)
.select("raw");
const stepsQuery = db('recipe_steps').where('recipe_id', id).select('step_number', 'instruction');
const stepsQuery = db("recipe_steps")
.where("recipe_id", id)
.select("step_number", "instruction");
const [recipe, ingredients, steps] = await Promise.all([recipeQuery, ingredientsQuery, stepsQuery]);
const [recipe, ingredients, steps] = await Promise.all([
recipeQuery,
ingredientsQuery,
stepsQuery,
]);
const result = {
details: recipe[0],
ingredients: ingredients,
steps: steps
steps: steps,
};
res.json(result);
@ -69,29 +85,32 @@ app.get("/recipe/:id", async (req, res) => {
});
// ### ADD RECIPE ###
app.post("/add-recipe", async (req, res) => {
app.post("/backend/add-recipe", async (req, res) => {
const { name, author, cuisine, stars, ingredients, steps } = req.body;
try {
const [id] = await db('recipes').insert({
name: name,
author: author,
cuisine: cuisine,
stars: stars
}, ['id'])
const [id] = await db("recipes").insert(
{
name: name,
author: author,
cuisine: cuisine,
stars: stars,
},
["id"],
);
const ingredientInserts = ingredients.map(ing => ({
const ingredientInserts = ingredients.map((ing) => ({
recipe_id: id.id,
raw: ing
raw: ing,
}));
//
await db('recipe_ingredients').insert(ingredientInserts);
await db("recipe_ingredients").insert(ingredientInserts);
const stepInserts = Object.keys(steps).map(stepNumber => ({
const stepInserts = Object.keys(steps).map((stepNumber) => ({
recipe_id: id.id,
step_number: parseInt(stepNumber),
instruction: steps[stepNumber]
instruction: steps[stepNumber],
}));
await db('recipe_steps').insert(stepInserts);
await db("recipe_steps").insert(stepInserts);
res.status(200).send({ message: "Recipe added", id: id.id });
} catch (err) {
@ -101,13 +120,11 @@ app.post("/add-recipe", async (req, res) => {
});
// ### SET STARS ###
app.post("/set-stars", async (req, res) => {
app.post("/backend/set-stars", async (req, res) => {
const { id, stars } = req.body;
try {
await db('recipes')
.where({ id: id })
.update({ stars: stars })
res.status(200).send({ message: "stars updated" })
await db("recipes").where({ id: id }).update({ stars: stars });
res.status(200).send({ message: "stars updated" });
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
@ -115,12 +132,12 @@ app.post("/set-stars", async (req, res) => {
});
// ### DELETE RECIPE ###
app.delete("/delete-recipe", async (req, res) => {
app.delete("/backend/delete-recipe", async (req, res) => {
const { id } = req.body;
try {
await db('recipe_steps').where({ recipe_id: id }).del();
await db('recipe_ingredients').where({ recipe_id: id }).del();
await db('recipes').where({ id: id }).del();
await db("recipe_steps").where({ recipe_id: id }).del();
await db("recipe_ingredients").where({ recipe_id: id }).del();
await db("recipes").where({ id: id }).del();
res.status(200).send({ message: "Recipe deleted" });
} catch (err) {
console.log(err);
@ -130,8 +147,8 @@ app.delete("/delete-recipe", async (req, res) => {
app.listen(port, () => console.log(`Server has started on port: ${port}`));
process.on('SIGINT', async () => {
console.log('Closing database connection...');
process.on("SIGINT", async () => {
console.log("Closing database connection...");
await db.destroy();
process.exit(0);
});

View file

@ -1,40 +1,41 @@
require('dotenv').config();
require("dotenv").config();
module.exports = {
development: {
client: 'postgresql',
client: "postgresql",
connection: {
host: 'db',
port: 5432,
host: "db",
port: process.env.DB_PORT,
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD
password: process.env.DB_PASSWORD,
},
pool: {
min: 2,
max: 10
max: 10,
},
migrations: {
tableName: 'knex_migrations',
directory: './migrations'
}
tableName: "knex_migrations",
directory: "./migrations",
},
},
production: {
client: 'postgresql',
client: "postgresql",
connection: {
database: 'my_db',
user: 'username',
password: 'password'
host: "db",
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
max: 10,
},
migrations: {
tableName: 'knex_migrations'
}
}
tableName: "knex_migrations",
directory: "./migrations",
},
},
};

View file

@ -4,7 +4,8 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "node backendServer.js"
"dev": "node backendServer.js",
"production": "node backendServer.js"
},
"keywords": [],
"author": "",