-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from abhinovpankaj/main
Add project report end point for uplaoding report to stroage and return url
- Loading branch information
Showing
12 changed files
with
225 additions
and
42 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"use strict"; | ||
const { ObjectId } = require('mongodb'); | ||
// var ObjectId = require('mongodb').ObjectId; | ||
var mongo = require('../database/mongo'); | ||
const Role = require('./role'); | ||
|
||
var addProjectReport = function (projectReport, callback) { | ||
mongo.ProjectReports.insertOne({project_id:projectReport.project_id,url: projectReport.url,name:projectReport.name,timestamp:projectReport.timestamp, uploader: projectReport.uploader}, {w: 1}, function (err, result) { | ||
if (err) { | ||
var error = new Error("addProjectReport()." + err.message); | ||
error.status = err.status; | ||
callback (error); | ||
return; | ||
} | ||
callback(null, result); | ||
}); | ||
}; | ||
|
||
var getProjectReportsbyProjectId = async function (project_id, callback) { | ||
|
||
var result = mongo.ProjectReports.find({project_id: project_id}); | ||
|
||
const res = []; | ||
for await (const doc of result){ | ||
res.push(doc); | ||
} | ||
|
||
if(res.length===0){ | ||
var error1 = new Error("getProjectReportsbyProjectId(). \nMessage: No Document Found."); | ||
error1.status = 404; | ||
callback (error1); | ||
return; | ||
} | ||
callback(null, res); | ||
|
||
}; | ||
|
||
var removeReport = async function (id, callback) { | ||
var result = await mongo.ProjectReports.deleteOne({_id: ObjectId(id)}); | ||
if(result.deletedCount==1){ | ||
callback(null,{status:201,message:"Document deleted successfully."}); | ||
} | ||
else{ | ||
var error2 = new Error("Error occurred. Didn't remove document. " + err.message); | ||
error2.status = err.status; | ||
callback (error2); | ||
return; | ||
} | ||
|
||
}; | ||
|
||
module.exports = { | ||
addProjectReport: addProjectReport, | ||
getProjectReportsbyProjectId: getProjectReportsbyProjectId, | ||
removeReport: removeReport | ||
}; |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
"use strict"; | ||
var express = require('express'); | ||
var router = express.Router(); | ||
|
||
var path = require('path'); | ||
const projectReports = require("../model/projectReports"); | ||
var jwt = require('jsonwebtoken'); | ||
const Role=require('../model/role'); | ||
|
||
require("dotenv").config(); | ||
|
||
router.route('/add') | ||
.post( function(req, res) { | ||
try { | ||
// Get document input | ||
const { project_id, name, url, uploader } = req.body; | ||
var timestamp = (new Date(Date.now())).toISOString(); | ||
// Validate document input | ||
if (!(project_id && name && url)) { | ||
res.status(400).send("All input is required"); | ||
} | ||
|
||
// Create document in our database | ||
projectReports.addProjectReport({ | ||
project_id, | ||
name, | ||
url, | ||
uploader, | ||
timestamp | ||
},function(err,result){ | ||
if (err) { | ||
res.status(err.status).send(err.message); | ||
} | ||
if (result){ | ||
res.status(201).json(result); | ||
} | ||
}); | ||
|
||
}catch (err) { | ||
console.log(err); | ||
} | ||
|
||
}); | ||
|
||
router.route('/:project_id') | ||
.get(async function(req,res){ | ||
try{ | ||
const project_id = req.params.project_id; | ||
projectReports.getProjectReportsbyProjectId(project_id ,async function(err,records){ | ||
if (err) { res.status(err.status).send(err.message); | ||
} | ||
else { | ||
if (records){ | ||
res.status(200).json(records); | ||
} | ||
else | ||
res.status(401).send("reports not found."); | ||
} | ||
}); | ||
} | ||
catch{ | ||
res.status(500).send("Internal server error."); | ||
} | ||
}); | ||
|
||
router.route('/delete') | ||
.post(async function(req,res){ | ||
try { | ||
// Get user input | ||
const document = req.body; | ||
projectReports.removeReport(document._id,function(err,result){ | ||
if(err){ | ||
res.status(err.status).send(err.message); | ||
} | ||
else{ | ||
res.status(result.status).send(result.message); | ||
} | ||
}) | ||
|
||
} | ||
catch (err) { | ||
console.log(err); | ||
res.status(500).send(`Internal server error ${err}`) | ||
} | ||
}); | ||
|
||
module.exports = router ; |
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
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