Skip to content

Commit

Permalink
Merge pull request #13 from ShahandFahad/usermgmt
Browse files Browse the repository at this point in the history
Usermgmt
  • Loading branch information
ShahandFahad authored Jan 30, 2024
2 parents 573caa1 + b0bc7ab commit 5f804b4
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 6 deletions.
19 changes: 16 additions & 3 deletions services/usermanagement/app.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
const express = require("express");
const app = express();

app.use(express.json()); // json parser
app.use(express.json()); // json parser: req handler middleware

app.get("/", (req, res) => {
res.status(200).send("User Management Serivice");
// Run in development env
if (process.env.NODE_ENV === "development") {
const morgan = require("morgan");
app.use(morgan("dev"));
}

// User routes
const userRoutes = require("./src/routes/userRoutes");

// Routes
app.use("/api/v1/user", userRoutes);

// Handle Non-Existing Routes: Return 404
app.all("*", (req, res) => {
res.status(404).json({ staus: "Failed", message: "Not Found!" });
});

module.exports = app;
1 change: 1 addition & 0 deletions services/usermanagement/config/env/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ API_KEY=your_api_key_here
DB_HOST=your_db_host
DB_USER=your_db_user
DB_PASSWORD=your_db_password
DATABASE_URL=mongodb://localhost:27017/mydatabase
9 changes: 6 additions & 3 deletions services/usermanagement/config/serverConfig.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Load Environment Variables: At the top of your app.js file
require("dotenv").config({ path: `${__dirname}/env/.env.development` });
// import app.js here
const app = require("../app");

// Start server
const PORT = 8001;
app.listen(PORT, () => {
const port = process.env.PORT || 8000;
app.listen(port, () => {
console.log(
`User Management: Server is running on: http://localhost:${PORT}`
`User Management: Server is running on: http://localhost:${port}`
);
});
12 changes: 12 additions & 0 deletions services/usermanagement/package-lock.json

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

1 change: 1 addition & 0 deletions services/usermanagement/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"homepage": "https://github.com/ShahandFahad/solarscape#readme",
"dependencies": {
"dotenv": "^16.4.1",
"express": "^4.18.2",
"mongoose": "^8.1.1"
},
Expand Down
24 changes: 24 additions & 0 deletions services/usermanagement/src/controllers/userController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// GET: Get all users from DB
exports.getAllUsers = (req, res) => {
res.status(200).json({ status: "Success", message: "Get All Users" });
};

// POST: Post | Add new user to DB
exports.createUser = (req, res) => {
res.status(200).json({ status: "Success", message: "New User Added" });
};

// GET: Get user by ID
exports.getUser = (req, res) => {
res.status(200).json({ status: "Success", message: "Get User by ID" });
};

// UPDATE: Update user details
exports.updateUser = (req, res) => {
res.status(200).json({ status: "Success", message: "User Updated" });
};

// DELETE: Delete user
exports.deleteUser = (req, res) => {
res.status(200).json({ status: "Success", message: "User Deleted" });
};
19 changes: 19 additions & 0 deletions services/usermanagement/src/routes/userRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const express = require("express");
const router = express.Router();

// Get user route controller
const userController = require("../controllers/userController");

// Router setup
router
.route("/")
.get(userController.getAllUsers)
.post(userController.createUser);

router
.route("/:id")
.get(userController.getUser)
.patch(userController.updateUser)
.delete(userController.deleteUser);

module.exports = router;

0 comments on commit 5f804b4

Please sign in to comment.