Skip to content

Commit

Permalink
[WIP]
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolay Martyanov <ohmspectator@gmail.com>
  • Loading branch information
OhmSpectator committed Oct 5, 2023
1 parent b0783b7 commit 6ac877b
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 18 deletions.
4 changes: 3 additions & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Gitignore for NodeJS project

node_modules
package-lock.json
package-lock.json

*.log
14 changes: 14 additions & 0 deletions backend/config/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require('dotenv').config();

const dbName = process.env.DB_NAME;
const dbUser = process.env.DB_USER;
const dbPassword = process.env.DB_PASSWORD;

const { Sequelize } = require('sequelize');

const sequelize = new Sequelize(dbName, dbUser, dbPassword, {
host: 'localhost',
dialect: 'postgres',
});

module.exports = sequelize;
14 changes: 13 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@
"name": "track-your-regions-backend",
"version": "1.0.0",
"description": "Backend for Track Your Regions",
"dependencies": {
"dotenv": "^16.3.1",
"express": "^4.18.2",
"pg": "^8.11.3",
"sequelize": "^6.33.0"
},
"devDependencies": {
"sequelize-auto": "^0.8.8"
"jest": "^27.0.6",
"nodemon": "^2.0.22",
"sequelize-auto": "^0.8.8",
"supertest": "^6.1.6"
},
"scripts": {
"start": "node src/app.js",
"dev": "nodemon src/app.js",
"test": "jest",
"generate-models": "./scripts/generate-models.sh"
}
}
30 changes: 14 additions & 16 deletions backend/scripts/generate-models.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
#!/usr/bin/env bash

# Check if .env file exists
if [ ! -f .env ]; then
echo "Please create .env file in the root directory"
exit 1
fi
OUTPUT_DIR="./src/models/generated"

# Load environment variables
source .env
# Create output directory if it doesn't exist
if [ ! -d "$OUTPUT_DIR" ]; then
mkdir -p "$OUTPUT_DIR"
fi

# Check the required environment variables
REQUIRED_VARIABLES=(DB_NAME DB_USER DB_PASSWORD)
for VARIABLE in "${REQUIRED_VARIABLES[@]}"; do
if [ -z "${!VARIABLE}" ]; then
echo "Please set $VARIABLE in .env file"
exit 1
fi
done
sequelize-auto -o "$OUTPUT_DIR" --schema gadm > model-generation.log

sequelize-auto -o './src/models' -d "$DB_NAME" -h localhost -u "$DB_USER" -x "$DB_PASSWORD" -p 5432 -e postgres --schema gadm
#Check if the generation was successful
if [ $? -eq 0 ]; then
echo "Models generated successfully"
rm model-generation.log
else
echo "Models generation failed"
exit 1
fi
16 changes: 16 additions & 0 deletions backend/src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const express = require('express');
const routes = require('./routes');
const sequelize = require('../config/database');

const app = express();

app.use(express.json());
app.use('/', routes);

const PORT = 3000;

sequelize.sync().then(() => {
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
});
12 changes: 12 additions & 0 deletions backend/src/controllers/regionController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const Region = require('../models/region');

exports.getRegionById = async (req, res) => {
const { regionId } = req.params;
try {
const region = await Region.findByPk(regionId);
if (!region) return res.status(404).send('Region not found');
res.json(region);
} catch (err) {
res.status(500).send(err.message);
}
};
27 changes: 27 additions & 0 deletions backend/src/models/region.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { DataTypes } = require('sequelize');
const sequelize = require('../../config/database');

const Region = sequelize.define('Region', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
parentRegionId: {
type: DataTypes.INTEGER,
references: {
model: 'Region',
key: 'id',
},
},
hasSubregions: {
type: DataTypes.BOOLEAN,
allowNull: false,
},
});

module.exports = Region;
9 changes: 9 additions & 0 deletions backend/src/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require('express');

const regionRoutes = require('./regionRoutes');

const router = express.Router();

router.use('/region', regionRoutes);

module.exports = router;
8 changes: 8 additions & 0 deletions backend/src/routes/regionRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require('express');
const regionController = require('../controllers/regionController');

const router = express.Router();

router.get('/:regionId', regionController.getRegionById);

module.exports = router;

0 comments on commit 6ac877b

Please sign in to comment.