Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blog Platform api #343

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions New_APIs/Blog_Platform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
## Description

- Backend infrastructure for a blogging platform, using Node.js, Express.js, and MongoDB, facilitating seamless management of user and admin roles with role-based authentication.
- Robust user authentication functionalities, including sign-in and sign-up features, ensuring secure access for both users and administrators.
- Users can create, edit, delete and view their own blogs.
- Users can like/unlike, comment, search by tags and view blogs.
- Administrators can manage user, blogs and tags.

## How to run it? 🕹️

1. Fork the repository.

2. Clone the project.

```
git clone repository-url
```

3. Install dependencies.

```
npm install
```

4. Create and update `.env` file.

```
PORT = port-no
MONGODB_URL = your mongodb database url
JWT_SECRET_KEY = your jwt secret key
```

5. Run the server.

```
node app.js
```

6. Check the endpoints via postman/frontend.


## EndPoints

1. Sign in

```http
POST /auth/signin
```


2. Sign up

```http
POST /users/
```

3. Get user by id

```http
GET /users/:id
```

4. Get all user (Admin)

```http
GET /users/
```

5. Update User (User)

```http
PUT /users/:id
```

7. Delete user (User)

```http
DELETE /users/:id
```

8. Create blog (User)

```http
POST /blogs/
```

9. Update blog (User)

```http
PUT /blogs/:id
```

10. Delete blog (User)

```http
DELETE /blogs/:id
```

11. Get all blogs

```http
GET /blogs/
```

12. Create comment (User)

```http
POST /comments/
```

13. Update comment (User)

```http
PUT /comments/:id
```

14. Delete comment (User)

```http
DELETE /comments/:id
```

15. Get comments by blog

```http
GET /comments/
```

15. Like/unlike blog (User)

```http
PUT /likes/
```

15. Get likes by blog

```http
GET /likes/:blogId
```

16. Create tag (Admin)

```http
POST /tags/
```

17. Update tag (Admin)

```http
PUT /tags/:id
```

18. Delete tag (Admin)

```http
DELETE /tags/:id
```

19. Get all tags

```http
GET /tags/
```
27 changes: 27 additions & 0 deletions New_APIs/Blog_Platform/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const authRoutes = require('./routes/authRouter')
const userRoutes = require('./routes/userRoutes')
const tagRoutes = require('./routes/tagRoutes')
const blogRoutes = require('./routes/blogRoutes')
const likeRoutes = require('./routes/likeRoutes')
const commentRoutes = require('./routes/commentRouter')
const connectToDB = require('./config/dbConnection')

const port = process.env.PORT || 8080;
const app = express();
app.use(bodyParser.json())

connectToDB()

app.use('/auth', authRoutes)
app.use('/users', userRoutes)
app.use('/tags', tagRoutes)
app.use('/blogs', blogRoutes)
app.use('/likes', likeRoutes)
app.use('/comments', commentRoutes)

app.listen(port, () => {
console.log('Application is running on port ' + port)
})
16 changes: 16 additions & 0 deletions New_APIs/Blog_Platform/config/dbConnection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require('dotenv').config();
const mongoose = require('mongoose');

const connectToDB = async () => {
await mongoose.connect(process.env.MONGODB_URL)
.then(() => {
console.log("Connected to MongoDB");
})
.catch(
(error) => {
console.error("Error connecting to MongoDB:", error);
}
);
}

module.exports = connectToDB
46 changes: 46 additions & 0 deletions New_APIs/Blog_Platform/controllers/authController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const User = require('../models/User')
const bcrypt = require('bcrypt')
const { generateToken } = require('../utils/generateToken')
const { STATUS } = require('../utils/status')

exports.signIn = async (req, res) => {
const { email, password } = req.body

if (!email || !password) {
return res.status(404).json({ message: "Please send all the required fields: email, password"})
}

try {
const user = await User.findOne({email: email})

if(!user) {
return res.status(404).json({ message: "User not found with email " + email})
}

if (user.status === STATUS.DISABLE) {
return res.status(401).json({ message: "User account is disbled by the admin!"})
}

const isValid = await bcrypt.compare(password, user.password)
if (!isValid) {
return res.status(404).json({ message: "Invalid credentials!"})
}

const token = generateToken(user._id, user.roles)

const response = {
token: token,
user: {
userId: user._id,
name: user.name,
email: user.email,
about: user.about,
}
}

res.status(200).json(response)
}catch (error) {
console.log("Failed to Sign in: " + error)
return res.status(500).json({ message: "Internal server error" })
}
}
Loading
Loading