-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathrecords.controller.js
More file actions
123 lines (103 loc) · 4 KB
/
records.controller.js
File metadata and controls
123 lines (103 loc) · 4 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
120
121
122
123
// This will help us connect to the database
import db from "../db/connection.js";
// This help convert the id from string to ObjectId for the _id.
import { ObjectId } from "mongodb";
// Controller function to fetch all records
export const getAllRecords = async (req, res) => {
try {
// Get the "records" collection
let collection = await db.collection("records");
// Fetch all documents from the collection and convert them to an array
let results = await collection.find({}).toArray();
// Send the results as a JSON response with a success status code
res.status(200).send(results);
} catch (error) {
// Handle errors and send an error response with a 500 status code
res.status(500).send({ message: "Error fetching records", error });
}
};
// Controller function to fetch a single record by its ID
export const getRecordById = async (req, res) => {
try {
// Get the "records" collection
let collection = await db.collection("records");
// Construct a query to find a record by its ObjectId
let query = { _id: new ObjectId(req.params.id) };
// Find the record matching the query
let result = await collection.findOne(query);
// If no record is found, send a "Not found" response with a 404 status code
if (!result) {
res.status(404).send("Not found");
} else {
// Send the found record as a JSON response with a success status code
res.status(200).send(result);
}
} catch (error) {
// Handle errors and send an error response with a 500 status code
console.error(err);
res.status(500).send({ message: "Error fetching record", error });
}
};
// Controller function to create a new record
export const createRecord = async (req, res) => {
try {
// Create a new document based on the request body
let newDocument = {
name: req.body.name,
position: req.body.position,
level: req.body.level,
};
// Get the "records" collection
let collection = await db.collection("records");
// Insert the new document into the collection
let result = await collection.insertOne(newDocument);
// Send the result of the insertion as a JSON response with a success status code
res.status(201).send(result);
} catch (err) {
// Handle errors and send an error response with a 500 status code
console.error(err);
res.status(500).send("Error adding record");
}
};
// Controller function to update a record by its ID
export const updateRecordById = async (req, res) => {
try {
// Construct a query to find a record by its ObjectId
const query = { _id: new ObjectId(req.params.id) };
// Define updates to be applied to the record
const updates = {
$set: {
name: req.body.name,
position: req.body.position,
level: req.body.level,
},
};
// Get the "records" collection
let collection = await db.collection("records");
// Update the record matching the query with the specified updates
let result = await collection.updateOne(query, updates);
// Send the result of the update operation as a JSON response with a success status code
res.status(200).send(result);
} catch (err) {
// Handle errors and send an error response with a 500 status code
console.error(err);
res.status(500).send("Error updating record");
}
};
// Controller function to delete a record by its ID
export const deleteRecordById = async (req, res) => {
try {
// Construct a query to find a record by its ObjectId
const query = { _id: new ObjectId(req.params.id) };
// Get the "records" collection
const collection = db.collection("records");
// Delete the record matching the query
let result = await collection.deleteOne(query);
// Send the result of the deletion as a JSON response with a success status code
res.status(200).send(result);
} catch (err) {
// Handle errors and send an error response with a 500 status code
console.error(err);
res.status(500).send("Error deleting record");
}
};