Skip to content

Commit 383aef1

Browse files
authored
Merge pull request #11 from anshulk/simple-crud
CrudController, TaskComment Crud
2 parents e09b68b + 18d5952 commit 383aef1

4 files changed

Lines changed: 153 additions & 2 deletions

File tree

server/config/Multer.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const init = () => {
88
destination: (req, file, callback) => {
99
callback(null, path.resolve(__dirname, "../upload"));
1010
},
11-
// eslint-disable-next-line consistent-return
1211
filename: (req, file, callback) => {
1312
const match = ["image/png", "image/jpeg", "image/jpg"];
1413

@@ -17,7 +16,7 @@ const init = () => {
1716
return callback(message, null);
1817
}
1918
const filename = `${Date.now()}-${req.query.taskId}-${file.originalname}`;
20-
callback(null, filename);
19+
return callback(null, filename);
2120
}
2221
});
2322
const upload = multer({ storage }).array("files");
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
class CrudController {
2+
PAGE_SIZE = 10
3+
LIMIT = 10
4+
5+
constructor(model) {
6+
this.model = model;
7+
}
8+
9+
create = async (req, res) => {
10+
const data = await this.model.create(req.body);
11+
return res.send({
12+
data
13+
});
14+
}
15+
16+
list = async (req, res) => {
17+
try {
18+
const sort_by_string = req.query.sort_by;
19+
delete req.query.sort_by;
20+
21+
const limit = req.query.limit || this.LIMIT;
22+
delete req.query.limit;
23+
24+
const page = parseInt(req.query.page) || 1;
25+
delete req.query.page;
26+
27+
const skip = (page - 1) * this.PAGE_SIZE;
28+
29+
for (let [key, value] of Object.entries(req.query)) {
30+
req.query[key] = Mongoose.Types.ObjectId.isValid(value)
31+
? Mongoose.Types.ObjectId(value)
32+
: value;
33+
}
34+
35+
const sort_by = sort_by_string.split(' ').reduce((acc, item) => {
36+
if (item.charAt(0) == '-') {
37+
acc[item.slice(1, item.length)] = -1
38+
} else {
39+
acc[item] = 1
40+
}
41+
return acc;
42+
}, {})
43+
44+
console.log(sort_by);
45+
46+
const result = await this.model.aggregate([{
47+
$facet: {
48+
data: [
49+
{ $match: req.query },
50+
{ $skip: skip },
51+
{ $limit: limit },
52+
{ $sort: sort_by }
53+
],
54+
totalCount: [
55+
{ $match: req.query },
56+
{ $count: 'count' }
57+
]
58+
}
59+
}])
60+
61+
const [{ data }] = result;
62+
let [{ totalCount }] = result;
63+
let [{ count }] = totalCount;
64+
65+
console.log(totalCount, count);
66+
67+
return res.send({
68+
page_size:this.PAGE_SIZE,
69+
count,
70+
page,
71+
data,
72+
count
73+
});
74+
} catch (error) {
75+
return res.status(500).send({
76+
'error': error.message
77+
});
78+
}
79+
}
80+
81+
get = async (req, res) => {
82+
const id = req.params.id;
83+
const data = await this.model.findById(id);
84+
return res.send({
85+
error: false,
86+
data,
87+
message: "OK"
88+
});
89+
}
90+
91+
update = async (req, res) => {
92+
try {
93+
const id = req.params.id;
94+
const new_data = req.body;
95+
await this.model.findByIdAndUpdate(id, new_data);
96+
const data = await this.model.findById(id);
97+
return res.send({
98+
data
99+
});
100+
} catch (error) {
101+
return res.status(500).send({
102+
'error': error.message
103+
});
104+
}
105+
}
106+
107+
delete = async (req, res) => {
108+
try {
109+
const id = req.params.id;
110+
let data = await this.model.findByIdAndRemove(id);
111+
return res.send({
112+
data
113+
})
114+
} catch (error) {
115+
return res.status(500).send({
116+
'error': error.message
117+
});
118+
}
119+
}
120+
}
121+
122+
module.exports = CrudController;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const CrudController = require("./CrudController");
2+
3+
module.exports = new CrudController(TaskComment);

server/routes/TaskComment.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module.exports = [
2+
{
3+
type: "GET",
4+
path: "/",
5+
handlers: ["TaskCommentController.list"]
6+
},
7+
{
8+
type: "POST",
9+
path: "/",
10+
handlers: ["TaskCommentController.create"]
11+
},
12+
{
13+
type: "GET",
14+
path: "/:id",
15+
handlers: ["TaskCommentController.get"]
16+
},
17+
{
18+
type: "DELETE",
19+
path: "/:id",
20+
handlers: ["TaskCommentController.delete"]
21+
},
22+
{
23+
type: "PATCH",
24+
path: "/:id",
25+
handlers: ["TaskCommentController.update"]
26+
}
27+
];

0 commit comments

Comments
 (0)