Blog Fake API for Testing

Here’s the refactored content tailored for the Blog Fake API: jsx Copy code

Overview:

This API is designed to help developers and students practice CRUD (Create, Read, Update, Delete) operations in a Blog application. Use this dummy API to simulate adding, updating, viewing, or deleting blog posts with realistic sample data like title, content, category, status, author, tags, and publish date.

Base URL:

Base URL: https://shrimo.com/fake-api/blog

Blog Schema:

Each blog post in this API follows this structure:

json

{
  "id": "string",
  "title": "string",
  "content": "string",
  "category": "string",
  "status": "Draft | Published",
  "author": "string",
  "tags": ["string"],
  "publishDate": "date"
}

API Endpoints:

1. Create a Blog Post

API URL:

https://shrimo.com/fake-api/blog

Endpoint:

/blog

Method:

POST

Description:

This endpoint allows you to create a new blog post. Provide details like title, content, category,status, author, tags, and an optional publish date.

Request Body:

json

{
  "title": "How to Learn JavaScript",
  "content": "This is a comprehensive guide to learning JavaScript from scratch.",
  "category": "Programming",
  "status": "Draft",
  "author": "John Doe",
  "tags": ["JavaScript", "Programming", "Tutorial"],
  "publishDate": "2024-11-15"
}

Example Response:

json

{
  "message": "Blog post created successfully.",
  "data": {
      "id": "672f0eaa814c297b16f90096",
      "title": "How to Learn JavaScript",
      "content": "This is a comprehensive guide to learning JavaScript from scratch.",
      "category": "Programming",
      "status": "Draft",
      "author": "John Doe",
      "tags": ["JavaScript", "Programming", "Tutorial"],
      "publishDate": "2024-11-15T00:00:00.000Z"
  }
}

2. Retrieve All Blog Posts

API URL:

https://shrimo.com/fake-api/blog

Endpoint:

/blog

Method:

GET

Description:

This GET endpoint fetches a list of all blog posts in the database. Each post includes essential details like title, content, category, status, tags, and publish date. You can also filter the posts by category or tags to refine the list based on your needs.

Example Response:

json

[
  {
    "_id": "672f0dee814c297b16f90093",
    "title": "How to Learn JavaScript",
    "content": "This is a comprehensive guide to learning JavaScript from scratch.",
    "category": "Programming",
    "status": "Published",
    "tags": ["JavaScript", "Tutorial", "Learning"],
    "publishDate": "2024-11-15T00:00:00.000Z",
    "createdAt": "2024-11-09T07:23:26.976Z",
    "updatedAt": "2024-11-09T07:23:26.976Z",
    "__v": 0
  }
]

3. Update a Blog Post

API URL:

https://shrimo.com/fake-api/blog/:id

Endpoint:

/blog/:id

Method:

PUT

Description:

This PUT endpoint allows you to update an existing blog postby its id. You can modify properties such as title, content, category, status, tags, and publish date. This is useful for managing and updating your blog content.

Request Body:

json

{
  "title": "How to Learn JavaScript - Updated",
  "content": "This updated guide includes advanced JavaScript topics.",
  "category": "Programming",
  "status": "Draft",
  "tags": ["JavaScript", "Programming", "Advanced"],
  "publishDate": "2024-11-20"
}

4. Delete a Blog Post

API URL:

https://shrimo.com/fake-api/blog/:id

Endpoint:

/blog/:id

Method:

DELETE

Description:

This DELETE endpoint allows you to delete a blog postby its id. Once deleted, the blog post will no longer be available in your list of published or draft posts. This is useful for removing outdated or irrelevant content from your blog.

Usage with Fetch and Axios

Learn how to interact with the Blog Post API using Fetch and Axios. These examples demonstrate how to create, retrieve, update, and delete blog posts through API calls. Use these methods to efficiently manage your content, whether you're working with JavaScript or React.

Create Blog Post (Fetch)

This example demonstrates how to send a POST request using Fetch to create a blog post.

javascript

const createBlog = async () => {
  const response = await fetch('https://shrimo.com/fake-api/blog', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      title: 'My First Blog Post',
      content: 'This is the content of the blog.',
      category: 'Tech',
      tags: ['API', 'JavaScript'],
      status: 'Published'
    })
  });
  const data = await response.json();
  console.log(data);
};

createBlog();
Retrieve All Blog Posts (Fetch)

Use the Fetch API to retrieve all blog posts, including details like title, content, category, and status.

javascript

const getBlogs = async () => {
  const response = await fetch('https://shrimo.com/fake-api/blog');
  const data = await response.json();
  console.log(data);
};

getBlogs();
Update Blog Post (Fetch)

Use the Fetch API to update an existing blog post by its ID, modifying details like title, content, category, and tags.

javascript

const updateBlog = async (id) => {
  const response = await fetch(`https://shrimo.com/fake-api/blog/${id}`, {
    method: 'PUT',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      title: 'Updated Blog Title',
      content: 'Updated content of the blog.',
      category: 'Updated Category',
      tags: ['Updated', 'API'],
      status: 'Published'
    })
  });
  const data = await response.json();
  console.log(data);
};

updateBlog('123456');
Delete Blog Post (Fetch)

Use the Fetch API to delete a blog post by its ID, removing it permanently from the system.

javascript

const deleteBlog = async (id) => {
  const response = await fetch(`https://shrimo.com/fake-api/blog/${id}`, {
    method: 'DELETE'
  });
  const data = await response.json();
  console.log(data);
};

deleteBlog('123456');

Create Todo (Axios)

In this example, Axios is used to send a POST request for creating a new Todo item.

javascript

import axios from 'axios';

const createTodo = async () => {
  try {
    const response = await axios.post('https://shrimo.com/fake-api/todos', {
      title: 'Learn JavaScript',
      description: 'Complete JavaScript basics',
      dueDate: '2024-11-15',
      priority: 'High',
      status: 'Not Started',
      tags: ['JavaScript', 'Learning']
    });
    console.log(response.data);
  } catch (error) {
    console.error('Error creating todo:', error);
  }
};

createTodo();
Retrieve All Todos (Axios)

Use Axios to retrieve all Todo items from the list, including their details such as title, description, status, and priority.

javascript

import axios from 'axios';

const getTodos = async () => {
  try {
    const response = await axios.get('https://shrimo.com/fake-api/todos');
    console.log(response.data);
  } catch (error) {
    console.error('Error retrieving todos:', error);
  }
};

getTodos();
Update Todo (Axios)

Use Axios to update an existing Todo item by ID, modifying properties like title, description, priority, status, and tags.

javascript

import axios from 'axios';

const updateTodo = async (id) => {
  try {
    const response = await axios.put(`https://shrimo.com/fake-api/todos/${id}`, {
      title: 'Learn JavaScript - Updated',
      description: 'Complete JavaScript basics',
      priority: 'High',
      status: 'In Progress'
    });
    console.log(response.data);
  } catch (error) {
    console.error('Error updating todo:', error);
  }
};

updateTodo('672f0dee814c297b16f90093');
Delete Todo (Axios)

Use Axios to delete a Todo item by ID, removing it from the list permanently.

javascript

import axios from 'axios';

const deleteTodo = async (id) => {
  try {
    const response = await axios.delete(`https://shrimo.com/fake-api/todos/${id}`);
    console.log(response.data);
  } catch (error) {
    console.error('Error deleting todo:', error);
  }
};

deleteTodo('672f0dee814c297b16f90093');