-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from rotirk20/development
Adding location routes and structure for backend
- Loading branch information
Showing
6 changed files
with
383 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Oops, something went wrong.