Skip to content

This project is a Post Management System built using Node.js and Express. It allows users to create, edit, delete, and view posts, with each post associated with a specific user. The system supports the following key features:

License

Notifications You must be signed in to change notification settings

Shubhamindev/Micro-Instagram

Repository files navigation

Micro-Instagram Backend

This is the Micro-Instagram-Backend for ChefKart, a platform for managing users and posts. It allows for CRUD (Create, Read, Update, Delete) operations on users and posts, allowing users to create posts, view posts, and manage them.

Prerequisites

Before you can use the API, you need the following:

Node.js (v14 or higher)
MySQL (for the database)
Dbeaver(for GUI of Database)
PostMan(To Check the API)

Clone this repository to your local machine:
Copy code
git clone https://github.com/Shubhamindev/Micro-Instagram)
cd chefkart

Install dependencies

Copy code
npm install

Configure your database settings:

Update config/config.json with your MySQL connection details.
Run database migrations:
Copy code
npx sequelize-cli db:migrate
Seed the database (optional for initial data):
Copy code
npx sequelize-cli db:seed:all
API Endpoints
The API is built with Express.js and Sequelize ORM and has the following routes:

1. Get All Users

Method: GET
Endpoint: /api/users
Description: Fetch all users from the database.
Example Response:
json
[ { "id": 1, "name": "John Doe", "email": "john@example.com", "post_count": 0 } ]

2. Get All Posts

Method: GET
Endpoint: /api/posts
Description: Fetch all posts, including associated user data.
Example Response:
json
[ { "id": 1, "title": "Post Title", "description": "Post description", "userId": 1, "user": { "id": 1, "name": "John Doe" } } ]

3. Create a Post for a User

Method: POST

Endpoint: /api/users/:userId/posts

Replace :userId with an actual userId from your database.
Body (JSON):

json
{ "title": "New Post Title", "description": "New Post Description", "images": ["image1.jpg", "image2.jpg"] }
Description: Creates a new post for a specific user. The userId is passed in the URL.

Example Response:

json
{ "id": 2, "title": "New Post Title", "description": "New Post Description", "images": ["image1.jpg", "image2.jpg"], "userId": 1 }

4. Edit an Existing Post

Method: PUT

Endpoint: /api/posts/:id

Replace :id with the post ID you want to update.
Body (JSON):

json
{ "title": "Updated Post Title", "description": "Updated Post Description", "images": ["updated_image.jpg"] }
Description: Updates the details of an existing post.

Example Response:

json
{ "id": 2, "title": "Updated Post Title", "description": "Updated Post Description", "images": ["updated_image.jpg"] }

6. Get All Posts of a Specific User

Endpoint: GET /api/users/:userId/posts
Description: Fetch all posts for a specific user by their userId.
Response:
json
[ { "id": 1, "title": "Post 1 by User", "description": "Description of the first post", "images": ["image1.jpg", "image2.jpg"], "userId": 1, "user": { "id": 1, "username": "JohnDoe", "email": "john@example.com" } }, { "id": 2, "title": "Post 2 by User", "description": "Description of the second post", "images": ["image3.jpg"], "userId": 1, "user": { "id": 1, "username": "JohnDoe", "email": "john@example.com" } } ]
Description: Fetch all posts for a given user. You need to provide the userId of the user whose posts you want to fetch.

5. Delete a Post

Method: DELETE

Endpoint: /api/posts/:id

Replace :id with the post ID you want to delete.
Description: Deletes a post from the database.

Example Response:

Status: 204 No Content
Running the Application

To run the application locally, use the following command:

bash
Copy code
npm start or node server.js
The server will start on http://localhost:3000.

Testing the API

You can test the API using tools like Postman or cURL.

Here are the endpoints you can test:

Get All Users: GET http://localhost:3000/api/users
Get All Posts: GET http://localhost:3000/api/posts
Create a Post: POST http://localhost:3000/api/users/1/posts
Edit a Post: PUT http://localhost:3000/api/users/1/posts
Delete a Post: DELETEhttp://localhost:3000/api/users/1/posts
Get Post of Specific User: GET http://localhost:3000/api/users/1/posts

Error Handling

The API returns appropriate HTTP status codes and error messages for failed requests.
404 Not Found: When a user or post is not found.
500 Internal Server Error: When something goes wrong on the server side.

Watch the demo video here:

Watch the video

License

This project is licensed under the GPL License

About

This project is a Post Management System built using Node.js and Express. It allows users to create, edit, delete, and view posts, with each post associated with a specific user. The system supports the following key features:

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published