Skip to content

Commit

Permalink
Merge pull request #133 from Nintails-TF/API/further-API-improvements
Browse files Browse the repository at this point in the history
API/further-API-improvements
  • Loading branch information
TheSloanRanger authored Jan 25, 2024
2 parents 2cdadd0 + 4126278 commit 5f18e30
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 33 deletions.
42 changes: 19 additions & 23 deletions api/api_server.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
require('dotenv').config();

require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const atmRoutes = require('./atmRoutes'); // Import ATM routes
const branchRoutes = require('./branchRoutes'); // Import Branch routes
const connectDB = require('./database'); // Import the database connection function
const atmRoutes = require('./atmRoutes');
const branchRoutes = require('./branchRoutes');
const app = express();

// MongoDB URI
const uri = process.env.MONGO_URI;

// Middleware
app.use(express.json());

mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});

const db = mongoose.connection;
db.on("error", console.error.bind(console, "Connection error:"));
db.once("open", () => {
console.log("Connected to the database");
});
connectDB(); // Connect to the database

// Use the imported routes
app.use(atmRoutes);
app.use(branchRoutes);

// Root endpoint
// Root Endpoint
app.get("/", (req, res) => {
res.send("Hello World!");
res.send("Hello World!");
});

// 404 Error Handler (for any unhandled routes)
app.use((req, res, next) => {
res.status(404).send("Resource not found");
});

// Generic Error Handler
app.use((error, req, res, next) => {
console.error(error.stack);
res.status(500).send('Something broke!');
});

// Use the PORT environment variable
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`API server listening on port ${PORT}!`));

17 changes: 12 additions & 5 deletions api/atmRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ const router = express.Router();
const mongoose = require('mongoose');
const db = mongoose.connection;

// Error handling function
function handleError(res, error) {
console.error(error);
res.status(500).json({ error: error.message });
}


// ATM ENDPOINTS

// GET all ATMs
Expand All @@ -12,7 +19,7 @@ router.get("/api/atms", async (req, res) => {
console.log(atms);
res.json(atms);
} catch (error) {
res.status(500).json({ error: error.message });
handleError(res, error);
}
});

Expand All @@ -25,7 +32,7 @@ router.get("/api/atms/:id", async (req, res) => {
console.log(atm);
res.json(atm);
} catch (error) {
res.status(500).json({ error: error.message });
handleError(res, error);
}
});

Expand All @@ -42,7 +49,7 @@ router.put("/api/atms/:id", async (req, res) => {
console.log(updatedAtm);
res.json(updatedAtm);
} catch (error) {
res.status(500).json({ error: error.message });
handleError(res, error);
}
});

Expand All @@ -58,7 +65,7 @@ router.delete("/api/atms/:id", async (req, res) => {
console.log(deletedAtm);
res.status(204).json();
} catch (error) {
res.status(500).json({ error: error.message });
handleError(res, error);
}
});

Expand All @@ -69,7 +76,7 @@ router.post("/api/atms", async (req, res) => {
console.log(newAtm);
res.status(201).json(newAtm);
} catch (error) {
res.status(500).json({ error: error.message });
handleError(res, error);
}
});

Expand Down
17 changes: 12 additions & 5 deletions api/branchRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ const router = express.Router();
const mongoose = require('mongoose');
const db = mongoose.connection;

// Error handling function
function handleError(res, error) {
console.error(error);
res.status(500).json({ error: error.message });
}


// BRANCH ENDPOINTS

// GET all Branches
Expand All @@ -12,7 +19,7 @@ router.get("/api/branches", async (req, res) => {
console.log(branches);
res.json(branches);
} catch (error) {
res.status(500).json({ error: error.message });
handleError(res, error);
}
});

Expand All @@ -25,7 +32,7 @@ router.get("/api/branches/:id", async (req, res) => {
console.log(branch);
res.json(branch);
} catch (error) {
res.status(500).json({ error: error.message });
handleError(res, error);
}
});

Expand All @@ -42,7 +49,7 @@ router.put("/api/branches/:id", async (req, res) => {
console.log(updatedBranch);
res.json(updatedBranch);
} catch (error) {
res.status(500).json({ error: error.message });
handleError(res, error);
}
});

Expand All @@ -58,7 +65,7 @@ router.delete("/api/branches/:id", async (req, res) => {
console.log(deletedBranch);
res.status(204).json();
} catch (error) {
res.status(500).json({ error: error.message });
handleError(res, error);
}
});

Expand All @@ -69,7 +76,7 @@ router.post("/api/branches", async (req, res) => {
console.log(newBranch);
res.status(201).json(newBranch);
} catch (error) {
res.status(500).json({ error: error.message });
handleError(res, error);
}
});

Expand Down
17 changes: 17 additions & 0 deletions api/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const mongoose = require('mongoose');

const connectDB = async () => {
try {
const uri = process.env.MONGO_URI;
await mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("Connected to the database");
} catch (error) {
console.error("Connection error:", error.message);
process.exit(1);
}
};

module.exports = connectDB;
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5f18e30

Please sign in to comment.