Skip to content

Instantly share code, notes, and snippets.

@ShwetaRPawar
Created August 8, 2020 10:55
Show Gist options
  • Save ShwetaRPawar/a1fa2c139aacfc92ca3fde688b826367 to your computer and use it in GitHub Desktop.
Save ShwetaRPawar/a1fa2c139aacfc92ca3fde688b826367 to your computer and use it in GitHub Desktop.
const { prisma } = require('../generated/prisma-client')
const { getUserId } = require ('../utils/authentication');
const { first } = require('lodash');
const { logger } = require('../utils/logging');
//show All company
async function getAllCompanys(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;
}
page = req.query.page
limit = page*10;
console.log('limit: ', limit);
offset = (page - 1)* 10;
console.log('offset: ', offset);
if (Number.isNaN(offset))
{
offset = 0;
}
const allCompany = await prisma.companies({first: limit, skip:offset});
res.send(JSON.stringify({"status": 200, "error": null, "response": allCompany}));
}
//show single company
async function getSingleCompany(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.params.id);
const singleCompany = await prisma.company({
id: req.params.id
})
console.log(singleCompany);
res.send(JSON.stringify({"status": 200, "error": null, "response": singleCompany}));
}
catch(e)
{
res.send(JSON.stringify({"status": 500, "error": e, "response": null}));
}
}
//add new company
async function addNewCompany(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 newCompany = await prisma.createCompany({
name: req.body.name,
email: req.body.email,
address: req.body.address
})
res.send(JSON.stringify({"status": 200, "error": null, "response": newCompany.id}));
}
catch(e)
{
res.send(JSON.stringify({"status": 500, "error": e, "response": null}));
}
}
//update company
async function updateCompany(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 updateCompany = await prisma.updateCompany({
where: {id: req.params.id},
data: {
name: req.body.name,
email: req.body.email,
address: req.body.address
},
})
res.send(JSON.stringify({"status": 200, "error": null, "response": updateCompany.id}));
}
catch(e)
{
res.send(JSON.stringify({"status": 500, "error": e, "response": e}));
}
}
//Delete company
async function deleteCompany(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 deleteCompany = await prisma.deleteCompany({
id: req.params.id,
})
res.send(JSON.stringify({"status": 200, "error": null, "response": deleteCompany.id}));
}
catch(e)
{
res.send(JSON.stringify({"status": 500, "error": e, "response": null}));
}
}
async function searchCompany(req, res){
try
{
logger.info("*: "+req.body.filter);
const companys = await prisma.companies({
where : {
name_contains: req.body.filter
}
})
res.send(JSON.stringify({"status": 200, "error": null, "response": companys}));
}
catch(e)
{
res.send(JSON.stringify({"status": 500, "error": e, "response": null}));
}
}
module.exports = {
addNewCompany,
getAllCompanys,
updateCompany,
deleteCompany,
getSingleCompany,
searchCompany
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment