Skip to content

Commit

Permalink
Merge pull request #8 from rotirk20/development
Browse files Browse the repository at this point in the history
Adding location routes and structure for backend
  • Loading branch information
rotirk20 authored Sep 21, 2024
2 parents c552e68 + f07b2cb commit 07b151e
Show file tree
Hide file tree
Showing 6 changed files with 383 additions and 46 deletions.
18 changes: 18 additions & 0 deletions backend/config/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const mysql = require('mysql');

const db = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME
});

db.connect((err) => {
if (err) {
console.error('Error connecting to MySQL:', err);
return;
}
console.log('Connected to MySQL database');
});

module.exports = db;
28 changes: 28 additions & 0 deletions backend/controllers/locationsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const db = require('../config/db');

// Get all locations
const getAllLocations = (req, res) => {
db.query('SELECT * FROM locations', (err, results) => {
if (err) {
return res.status(500).send(err);
}
res.json(results);
});
};

// Add a new location
const addLocation = (req, res) => {
const { name, latitude, longitude } = req.body;
if (!name || !latitude || !longitude) {
return res.status(400).json({ message: 'Please provide name, latitude, and longitude' });
}
const query = 'INSERT INTO locations (name, latitude, longitude) VALUES (?, ?, ?)';
db.query(query, [name, latitude, longitude], (err, results) => {
if (err) {
return res.status(500).send(err);
}
res.json({ message: 'Location added successfully', locationId: results.insertId });
});
};

module.exports = { getAllLocations, addLocation };
Loading

0 comments on commit 07b151e

Please sign in to comment.