Skip to content

Commit

Permalink
Implemented addEmployee and getEmployees
Browse files Browse the repository at this point in the history
  • Loading branch information
senuravihanjayadeva committed Feb 25, 2022
1 parent b2070a6 commit 470165d
Show file tree
Hide file tree
Showing 7 changed files with 1,557 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
/node_modules
/examples
.env
/.env
.vscode
34 changes: 34 additions & 0 deletions controllers/Employee.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const Employee = require("../models/Employee.model");

const addEmployee = (req, res) => {
const { name, position, salary } = req.body;

const employee = new Employee({
name,
position,
salary,
});

employee
.save()
.then((createdEmployee) => {
res.json(createdEmployee);
})
.catch((error) => {
res.status(400).json(error);
});
};

const getEmployees = async (req, res) => {
try {
const employees = await Employee.find();
res.json(employees);
} catch (error) {
res.status(400).json(error);
}
};

module.exports = {
addEmployee,
getEmployees,
};
10 changes: 10 additions & 0 deletions models/Employee.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const mongoose = require("mongoose");
const { Schema } = mongoose;

const employeeSchema = new Schema({
name: String,
position: String,
salary: Number,
});

module.exports = Employee = mongoose.model('Employee', employeeSchema);
Loading

0 comments on commit 470165d

Please sign in to comment.