Skip to content

Commit 9fd5e3a

Browse files
committed
first commit from project setup
0 parents  commit 9fd5e3a

7 files changed

Lines changed: 1353 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.env

database-access/db-connection.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const MongoClient = require("mongodb").MongoClient;
2+
module.exports = {
3+
4+
connection: async () => {
5+
// The MongoClient is the object that references the connection to our
6+
// datastore (Atlas, for example)
7+
const client = new MongoClient(process.env.DB_URI);
8+
9+
// The connect() method does not attempt a connection; instead it instructs
10+
// the driver to connect using the settings provided when a connection
11+
// is required.
12+
try {
13+
await client.connect();
14+
} catch (err) {
15+
console.log("error connecting to database", err);
16+
}
17+
18+
// Provide the name of the database and collection you want to use.
19+
// If the database and/or collection do not exist, the driver and Atlas
20+
// will create them automatically when you first write data.
21+
const datastoreName = "digital-market-place-updates";
22+
23+
24+
// Create references to the database and collection in order to run
25+
// operations on them.
26+
const database = client.db(datastoreName);
27+
// const userCollection = database.collection("users");
28+
29+
return database;
30+
}
31+
}

database-access/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const dbConnection = require("./db-connection");
2+
const makeUserdb = require("./store-user")
3+
4+
const userUseCase = makeUserdb({dbConnection});
5+
6+
module.exports = userUseCase;

database-access/store-user.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
3+
module.exports = async function makeUser({ dbConnection }) {
4+
5+
6+
return Object.freeze({
7+
findAllUsers,
8+
findUserById,
9+
createUser,
10+
updateUser,
11+
deleteUser
12+
})
13+
14+
15+
async function findAllUsers() {
16+
const db = await dbConnection()
17+
const result = await db.collection('comments').find({})
18+
return (await result.toArray()).map(({ _id: id, ...found }) => ({
19+
id,
20+
...found
21+
}))
22+
}
23+
24+
async function findUserById() {
25+
const db = await dbConnection()
26+
const result = await db.collection('comments').find({ _id })
27+
const found = await result.toArray()
28+
if (found.length === 0) {
29+
return null
30+
}
31+
const { _id: id, ...info } = found[0]
32+
return { id, ...info }
33+
}
34+
35+
async function createUser({ id: _id = Id.makeId(), ...userData }) {
36+
const db = await dbConnection()
37+
const result = await db
38+
.collection('comments')
39+
.insertOne({ _id, ...userData })
40+
const { _id: id, ...insertedInfo } = result.ops[0]
41+
return { id, ...insertedInfo }
42+
}
43+
44+
async function updateUser({ id: _id, userData }) {
45+
const db = await dbConnection()
46+
const result = await db
47+
.collection('comments')
48+
.updateOne({ _id }, { $set: { ...userData } })
49+
return result.modifiedCount > 0 ? { id: _id, ...userData } : null
50+
}
51+
52+
async function deleteUser({ id: _id }) {
53+
const db = await dbConnection()
54+
const result = await db.collection('comments').deleteOne({ _id })
55+
return result.deletedCount
56+
}
57+
}

index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
3+
const express = require('express');
4+
require('dotenv').config();
5+
const cors = require('cors');
6+
const { connection } = require('./database-access/db-connection.js');
7+
8+
connection().then((db) => {
9+
console.log("database connected: ", db.databaseName);
10+
})
11+
12+
const app = express();
13+
14+
const PORT = process.env.PORT || 5000;
15+
16+
app.use(cors());
17+
app.use(express.json());
18+
app.use(express.urlencoded({extended: false}));
19+
20+
21+
22+
app.use('/', (req, res) => {
23+
res.send('hello world');
24+
});
25+
26+
app.use((req, res, next) => {
27+
const dntHeader = req.headers['dnt']; // Access DNT header (if present)
28+
if (dntHeader === '1') {
29+
console.log('User has DNT enabled');
30+
// Implement logic to handle DNT preference (e.g., disable tracking features)
31+
}
32+
next(); // Pass control to the next middleware or route handler
33+
});
34+
35+
36+
37+
38+
app.listen(PORT, () => console.log(`Server started on port http://localhost:${PORT}`));
39+
40+
module.exports = app;

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "digital-market-place-updates",
3+
"version": "1.0.0",
4+
"description": "to sell products and services",
5+
"main": "index.js",
6+
"license": "ISC",
7+
"author": {
8+
"name": "avom brice ",
9+
"address": "frckbrice <bricefrkc@gmail,com> (https://maebrie.vercel.app)",
10+
"date": "jun 12 2024"
11+
},
12+
"scripts": {
13+
"start": "node index.js",
14+
"dev": "nodemon index.js"
15+
},
16+
"dependencies": {
17+
"bcrypt": "^5.1.1",
18+
"cors": "^2.8.5",
19+
"cuid": "^3.0.0",
20+
"dotenv": "^16.4.5",
21+
"express": "^4.19.2",
22+
"mongodb": "^6.7.0",
23+
"nodemon": "^3.1.3",
24+
"sanitize-html": "^2.13.0"
25+
}
26+
}

0 commit comments

Comments
 (0)