Created
August 8, 2020 11:01
-
-
Save ShwetaRPawar/633e5f0b581661ca04e4cd39c97e0ad3 to your computer and use it in GitHub Desktop.
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
const { prisma } = require('../generated/prisma-client') | |
const { getUserId } = require ('../utils/authentication') | |
//show All employees | |
async function getAllEmployees(req, res){ | |
const userId = await getUserId(req); | |
console.log("userId: ", userId); | |
if (userId == null || userId.message) | |
{ | |
res.send(JSON.stringify({"status": 401, "error": 'JWT expired or not provided', "response": null})); | |
return; | |
} | |
const allEmployees = await prisma.employees(); | |
res.send(JSON.stringify({"status": 200, "error": null, "response": allEmployees})); | |
} | |
//show single employee | |
async function getSingleEmployee(req, res){ | |
try | |
{ | |
const userId = await getUserId(req); | |
console.log("userId: ", userId); | |
if (userId == null || userId.message) | |
{ | |
res.send(JSON.stringify({"status": 401, "error": 'JWT expired or not provided', "response": null})); | |
return; | |
} | |
const singleEmployee = await prisma.employee({ | |
id: req.params.id | |
}) | |
console.log(singleEmployee); | |
res.send(JSON.stringify({"status": 200, "error": null, "response": singleEmployee})); | |
} | |
catch(e) | |
{ | |
res.send(JSON.stringify({"status": 500, "error": e, "response": null})); | |
} | |
} | |
//add new employee | |
async function addNewEmployee(req, res){ | |
try | |
{ | |
const userId = await getUserId(req); | |
console.log("userId: ", userId); | |
if (userId == null || userId.message) | |
{ | |
res.send(JSON.stringify({"status": 401, "error": 'JWT expired or not provided', "response": null})); | |
return; | |
} | |
console.log(req.body.company_id) | |
const newEmployee = await prisma.createEmployee({ | |
name: req.body.name, | |
emp_id: req.body.emp_id, | |
company: { | |
connect: { | |
id: req.body.company_id | |
} | |
} | |
}) | |
res.send(JSON.stringify({"status": 200, "error": null, "response": newEmployee.id})); | |
} | |
catch(e) | |
{ | |
res.send(JSON.stringify({"status": 500, "error": e, "response": null})); | |
} | |
} | |
//update employee | |
async function updateEmployee(req, res){ | |
try | |
{ | |
const userId = await getUserId(req); | |
console.log("userId: ", userId); | |
if (userId == null || userId.message) | |
{ | |
res.send(JSON.stringify({"status": 401, "error": 'JWT expired or not provided', "response": null})); | |
return; | |
} | |
console.log(req.body.name) | |
const updateEmployee = await prisma.updateEmployee({ | |
data:{ | |
name: req.body.name, | |
emp_id: req.body.emp_id, | |
company: { | |
connect: { | |
id: req.body.company_id | |
} | |
}, | |
}, | |
where: {id: req.params.id}, | |
}) | |
res.send(JSON.stringify({"status": 200, "error": null, "response": updateEmployee.id})); | |
} | |
catch(e) | |
{ | |
res.send(JSON.stringify({"status": 500, "error": e, "response": null})); | |
} | |
} | |
//Delete employee | |
async function deleteEmployee(req, res){ | |
try | |
{ | |
const userId = await getUserId(req); | |
console.log("userId: ", userId); | |
if (userId == null || userId.message) | |
{ | |
res.send(JSON.stringify({"status": 401, "error": 'JWT expired or not provided', "response": null})); | |
return; | |
} | |
const deleteEmployee = await prisma.deleteEmployee({ | |
id: req.params.id, | |
}) | |
res.send(JSON.stringify({"status": 200, "error": null, "response": deleteEmployee.id})); | |
} | |
catch(e) | |
{ | |
res.send(JSON.stringify({"status": 500, "error": e, "response": null})); | |
} | |
} | |
module.exports = { | |
addNewEmployee, | |
getAllEmployees, | |
updateEmployee, | |
deleteEmployee, | |
getSingleEmployee | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment