Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
APP_NAME=HR
PORT=5000
NODE_ENV=development

STATIC_DIR=assets
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**node_modules
4 changes: 3 additions & 1 deletion server/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/node_modules
**node_modules
/node_modules
.env
143 changes: 143 additions & 0 deletions server/app/controllers/employeeController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
const Employee = require("../../models/employeeModel");

// Create and Save a new employee
exports.create = (req, res) => {
// Validate request
if (!req.body.email) {
return res.status(400).send({
message: "Email can not be empty",
});
}

// Create a employee
const employee = new Employee({
first_name: req.body.first_name || "First name is required",
last_name: req.body.last_name || "last name is required",
user_name: req.body.user_name || "First name is required",
email: req.body.email || "Email is required",
employee_id: req.body.first_name || "ID is required",
joining_date: req.body.joining_date,
phone_number: req.body.phone_number,
qualification: req.body.qualification,
department: req.body.department,
designation: req.body.designation,
});

// Save employee in the database
employee
.save()
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message: err.message || "Some error occurred while adding employee.",
});
});
};

// Retrieve and return all employees from the database.
exports.findAll = (req, res) => {
Employee.find()
.then((employees) => {
res.send(employees);
})
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving employees.",
});
});
};

// Find a single employee with a employeeId
exports.findOne = (req, res) => {
Employee.findById(req.params.employeeId)
.then((employee) => {
if (!employee) {
return res.status(404).send({
message: "Employee not found with id " + req.params.employeeId,
});
}
res.send(employee);
})
.catch((err) => {
if (err.kind === "ObjectId") {
return res.status(404).send({
message: "Employee not found with id " + req.params.employeeId,
});
}
return res.status(500).send({
message: "Error retrieving employee with id " + req.params.employeeId,
});
});
};

// Update a employee identified by the employeeId in the request
exports.update = (req, res) => {
// Validate Request
if (!req.body.email) {
return res.status(400).send({
message: "Email can not be empty",
});
}

// Find employee and update it with the request body
Employee.findByIdAndUpdate(
req.params.employeeId,
{
first_name: req.body.first_name,
last_name: req.body.last_name,
user_name: req.body.user_name,
email: req.body.email,
employee_id: req.body.employee_id,
joining_date: req.body.joining_date,
phone_number: req.body.phone_number,
qualification: req.body.qualification,
department: req.body.department,
designation: req.body.designation,
},
{ new: true }
)
.then((employee) => {
if (!employee) {
return res.status(404).send({
message: "Employee not found with id " + req.params.employeeId,
});
}
res.send(employee);
})
.catch((err) => {
if (err.kind === "ObjectId") {
return res.status(404).send({
message: "Employee not found with id " + req.params.employeeId,
});
}
return res.status(500).send({
message: "Error updating employee with id " + req.params.employeeId,
});
});
};

// Delete a employee with the specified employeeid in the request
exports.delete = (req, res) => {
Employee.findByIdAndRemove(req.params.employeeId)
.then((employee) => {
if (!employee) {
return res.status(404).send({
message: "Employee not found with id " + req.params.employeeId,
});
}
res.send({ message: "Employee deleted successfully!" });
})
.catch((err) => {
if (err.kind === "ObjectId" || err.name === "NotFound") {
return res.status(404).send({
message: "Employee not found with id " + req.params.employeeId,
});
}
return res.status(500).send({
message: "Could not delete employee with id " + req.params.employeeId,
});
});
};
7 changes: 7 additions & 0 deletions server/app/controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const employeeController = require("./employeeController");
const userController = require("./user.controller");

module.exports = {
employeeController,
userController,
};
Loading