-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexperiences.js
More file actions
119 lines (101 loc) · 3.65 KB
/
experiences.js
File metadata and controls
119 lines (101 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import express from "express"; // third party module(needs to ne installed)
import experienceModel from "./schema.js";
import multer from "multer";
import { v2 as cloudinary } from "cloudinary";
import { CloudinaryStorage } from "multer-storage-cloudinary";
import { Transform } from "json2csv";
// import { validationResult } from "express-validator";
// import createError from "http-errors";
// import { blogPostsValidation } from "./validation.js";
// import { generatePDFStream } from "../lib/pdf.js";
// import { pipeline } from "stream";
const ExperienceRouter = express.Router();
/****************POST Experience******************/
ExperienceRouter.post("/:userName/experiences", async (req, res, next) => {
try {
// const user = await userModel.findById()
const newExperience = new experienceModel(req.body);
const mongoRes = await newExperience.save();
res.status(201).send(mongoRes);
} catch (error) {
next(error);
}
});
/****************UPLOAD COVER USING CLOUDINARY******************/
const cloudinaryStorage = new CloudinaryStorage({
cloudinary,
params: {
folder: "Strive-linkedIn",
},
});
const upload = multer({ storage: cloudinaryStorage }).single("image");
ExperienceRouter.post("/:userName/experiences/:expId/picture", upload, async (req, res, next) => {
try {
console.log(req.file);
console.log(req.file.path);
const experience = await experienceModel.findById(req.params.expId);
experience.image = req.file.path;
await experience.save();
res.send(req.file.path);
} catch (error) {
next(error);
}
});
/***************************Download csv**********************************************/
ExperienceRouter.get("/:userName/experiences/CSV", async (req, res, next) => {
try {
const fields = ["_id", "role", "company", "startDate","endDate", "description", "area"];
const options = { fields };
const jsonToCsv = new Transform(options);
const source = getPostsSource();
res.setHeader("Content-Disposition", "attachment; filename=export.csv");
pipeline(source, jsonToCsv, res, (err) => next(err)); // source (file on disk) -> transform (json 2 csv) -> destination (response)
} catch (error) {
next(error);
}
});
/****************GET EXPERIENCES******************/
ExperienceRouter.get("/:userName/experiences", async (req, res, next) => {
try {
const allExperiences = await experienceModel.find({}, { updatedAt: 0, createdAt: 0 }).populate("username", { name: 1, surname: 1, _id: 0 });
res.send(allExperiences);
} catch (error) {
console.log(error);
next(error);
}
});
/****************GET SPECIFIC EXPERIENCES******************/
ExperienceRouter.get("/:userName/experiences/:expId", async (req, res, next) => {
try {
const singleExp = await experienceModel.findById(req.params.expId);
if (singleExp) {
res.send(singleExp);
} else {
res.status(404).send_(`Not found `);
}
} catch (error) {
console.log(error);
next(error);
}
});
/****************UPDATE EXPERIENCES******************/
ExperienceRouter.put("/:userName/experiences/:expId", async (req, res, next) => {
try {
const updatedExp = await experienceModel.findByIdAndUpdate(req.params.id, req.body, { runValidators: true, new: true });
res.send(updatedExp);
} catch (error) {
next(error);
}
});
/****************DELETE EXPERIENCE******************/
ExperienceRouter.delete("/:userName/experiences/:expId", async (req, res, next) => {
try {
const deletedExp = await experienceModel.findByIdAndDelete(req.params.id);
if (deletedExp) {
res.status(204).send();
}
} catch (error) {
next(error);
}
});
export default ExperienceRouter;