Skip to content

Commit 75223b7

Browse files
authored
Merge pull request #467 from tcet-opensource/457-Create-Application-Model-and-full-Endpoint
457-Create-Application-Model-and-full-Endpoint
2 parents 16aa476 + 058de2f commit 75223b7

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed

controller/application.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {
2+
addNewApplication,
3+
deleteApplicationById,
4+
updateApplicationById,
5+
getApplication,
6+
} from "#services/application";
7+
import { logger } from "#util";
8+
9+
async function addApplication(req, res) {
10+
const {
11+
ERPID,
12+
type,
13+
data,
14+
collection,
15+
} = req.body;
16+
try {
17+
const application = await addNewApplication(
18+
ERPID,
19+
type,
20+
data,
21+
collection,
22+
);
23+
res.json({
24+
res: `added application ${application.ERPID}`,
25+
id: application.id,
26+
});
27+
} catch (error) {
28+
logger.error("Error while inserting", error);
29+
res.status(500);
30+
res.json({ err: "Error while inserting in DB" });
31+
}
32+
}
33+
async function deleteApplication(req, res) {
34+
const { id } = req.params;
35+
try {
36+
await deleteApplicationById(id);
37+
res.json({ res: "Application deleted successfully" });
38+
} catch (error) {
39+
logger.error("Error while deleting", error);
40+
res.status(500);
41+
res.json({ err: "Error while deleting from DB" });
42+
}
43+
}
44+
45+
async function updateApplication(req, res) {
46+
const { id } = req.params;
47+
const { ...data } = req.body;
48+
49+
try {
50+
await updateApplicationById(id, data);
51+
res.json({ res: `${id} application updated` });
52+
} catch (error) {
53+
logger.error("Error while inserting", error);
54+
res.status(500);
55+
res.json({ err: "Error while inserting in DB" });
56+
}
57+
}
58+
59+
async function showApplication(req, res) {
60+
try {
61+
const filter = req.body;
62+
const { limit, page } = req.query;
63+
const application = await getApplication(filter, limit, page);
64+
return res.json({ res: application });
65+
} catch (error) {
66+
logger.error("Error while fetching", error);
67+
res.status(500);
68+
return res.json({ err: "Error while fetching the data" });
69+
}
70+
}
71+
72+
export default {
73+
addApplication,
74+
updateApplication,
75+
deleteApplication,
76+
showApplication,
77+
};
78+

models/application.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import connector from "#models/databaseUtil";
2+
3+
const applicationSchema = {
4+
ERPID: { type: String, required: true },
5+
type: {
6+
type: String,
7+
enum: ["insert", "update", "delete"],
8+
required: true,
9+
},
10+
data: {
11+
type: connector.Schema.Types.ObjectId,
12+
ref: "Data",
13+
required: "true",
14+
},
15+
collection: { type: String, required: true },
16+
};
17+
const Application = connector.model("Application",applicationSchema );
18+
19+
//crud operation
20+
async function create(applicationData) {
21+
const {
22+
ERPID,
23+
type,
24+
data,
25+
collection,
26+
} = applicationData;
27+
const application = new Application({
28+
ERPID,
29+
type,
30+
data,
31+
collection,
32+
});
33+
const applicationDoc = await application.save();
34+
return applicationDoc;
35+
}
36+
37+
async function createMultiple(applicationDataArray) {
38+
const applications = applicationDataArray.map(
39+
({
40+
ERPID,
41+
type,
42+
data,
43+
collection,
44+
}) =>
45+
Application({
46+
ERPID,
47+
type,
48+
data,
49+
collection,
50+
}),
51+
);
52+
53+
const applicationDocs = await Application.insertMany(applications);
54+
return applicationDocs;
55+
}
56+
57+
async function read(filter, limit = 0, page = 1) {
58+
const applicationDoc = await Application.find(filter)
59+
.limit(limit)
60+
.skip((page - 1) * limit)
61+
.exec();
62+
const count = await Application.count();
63+
const totalPages = Math.ceil(count / limit);
64+
return { totalPages, data: applicationDoc };
65+
}
66+
67+
async function update(filter, updateObject, options = { multi: true }) {
68+
const updateApplication = await Application.updateMany(
69+
filter,
70+
{ $set: updateObject },
71+
options,
72+
);
73+
return updateApplication.acknowledged;
74+
}
75+
76+
async function remove(filter) {
77+
const deleteApplication = await Application.deleteMany(filter).exec();
78+
return deleteApplication.acknowledged;
79+
}
80+
81+
export default {
82+
create,
83+
read,
84+
update,
85+
remove,
86+
createMultiple,
87+
};

services/application.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Application from "#models/application";
2+
import databaseError from "#error/database";
3+
4+
export async function addNewApplication(
5+
ERPID,
6+
type,
7+
data,
8+
collection,
9+
) {
10+
const newApplication = await Application.create({
11+
ERPID,
12+
type,
13+
data,
14+
collection,
15+
});
16+
if (String(newApplication.ERPID) === ERPID) {
17+
return newApplication;
18+
}
19+
throw new databaseError.DataEntryError("Add Application");
20+
}
21+
22+
export async function getApplication(filter, limit, page) {
23+
const applications = await Application.read(filter, limit, page);
24+
if (applications) {
25+
return applications;
26+
}
27+
throw new databaseError.DataNotFoundError("Application");
28+
}
29+
30+
export async function deleteAppliactionById(applicationId) {
31+
const deleted = await Application.remove({ _id: applicationId });
32+
if (deleted) {
33+
return deleted;
34+
}
35+
throw new databaseError.DataDeleteError("Application");
36+
}
37+
38+
export async function updateApplicationById(id, data) {
39+
const updated = await Application.update({ _id: id }, data);
40+
if (updated) {
41+
return updated;
42+
}
43+
throw new databaseError.DataEntryError("Application");
44+
}

0 commit comments

Comments
 (0)