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

json_placeholder api implementation added #164

Merged
merged 3 commits into from
Jun 2, 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
24 changes: 24 additions & 0 deletions Existing_API_Collection/JSON_Placeholder_API/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# JSONPlaceholder CRUD App

This is a simple CRUD (Create, Read, Update, Delete) application built using HTML, CSS, and JavaScript. It allows you to interact with the JSONPlaceholder API to perform CRUD operations on posts.

## Features

- View a list of posts fetched from JSONPlaceholder API.
- Create new posts with a title and body.
- Update existing posts.
- Delete posts.

## Usage

1. Clone the repository:

```bash
git clone <repository-url>
```

2. Open `index.html` in your web browser.

3. Interact with the application by creating, updating, and deleting posts.


30 changes: 30 additions & 0 deletions Existing_API_Collection/JSON_Placeholder_API/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSONPlaceholder CRUD</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
<div id="container" class="container py-4">
<h1 class="text-center mb-4">JSONPlaceholder CRUD</h1>
<form id="postForm" class="mb-4">
<div class="form-group">
<input type="text" id="title" placeholder="Title" class="form-control mb-2">
</div>
<div class="form-group">
<textarea id="body" placeholder="Body" class="form-control mb-2" rows="4"></textarea>
</div>
<button type="submit" class="btn btn-primary btn-lg btn-block">Create Post</button>
</form>
<hr>
<h2 class="text-center mb-4">Posts</h2>
<div id="posts" class="list-group"></div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script src="script.js"></script>
</body>
</html>
111 changes: 111 additions & 0 deletions Existing_API_Collection/JSON_Placeholder_API/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const postForm = document.getElementById('postForm');
const postsContainer = document.getElementById('posts');
let posts = [];

async function fetchAndRenderPosts() {
posts = await fetchPosts();
renderPosts(posts);
}

async function fetchPosts() {
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
const data = await response.json();
return data.slice(0, 50);
}

// Function to render posts
function renderPosts(posts) {
postsContainer.innerHTML = '';
posts.forEach((post, index) => {
const postElement = document.createElement('div');
postElement.classList.add('list-group-item');
postElement.innerHTML = `
<h4 class="mb-1">${index + 1}. ${post.title}</h4>
<p class="mb-1">${post.body}</p>
<button type="button" class="btn btn-primary btn-sm mr-2" onclick="updatePost(${post.id})">Update</button>
<button type="button" class="btn btn-danger btn-sm" onclick="deletePost(${post.id})">Delete</button>
`;
postsContainer.appendChild(postElement);
if (index > 0) {
postsContainer.insertBefore(document.createElement('hr'), postElement);
}
$(postElement.querySelector('h4')).popover({
content: `<strong>Title:</strong> ${post.title}<br><strong>Body:</strong> ${post.body}`,
trigger: 'click',
html: true,
});
});
}

// Function to create a new post
async function createPost(title, body) {
await fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: JSON.stringify({
title,
body,
userId: 1
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
});
fetchAndRenderPosts();
}

// Function to delete a post
async function deletePost(id) {
await fetch(`https://jsonplaceholder.typicode.com/posts/${id}`, {
method: 'DELETE',
});
posts = posts.filter(post => post.id !== id);
renderPosts(posts);
}

// Function to update a post
async function updatePost(id) {
const newTitle = prompt('Enter the new title:');
const newBody = prompt('Enter the new body:');
if (newTitle !== null && newBody !== null) {
await fetch(`https://jsonplaceholder.typicode.com/posts/${id}`, {
method: 'PUT',
body: JSON.stringify({
id,
title: newTitle,
body: newBody,
userId: 1
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
});
const updatedPostIndex = posts.findIndex(post => post.id === id);
if (updatedPostIndex !== -1) {
posts[updatedPostIndex].title = newTitle;
posts[updatedPostIndex].body = newBody;
renderPosts(posts);
}
}
}

// Event listener for form submission
postForm.addEventListener('submit', async function (event) {
event.preventDefault();
const title = document.getElementById('title').value;
const body = document.getElementById('body').value;
if (posts.length >= 500) {
await createPost(title, body);
} else {
const newPost = {
id: posts.length + 1,
title,
body
};
posts.push(newPost);
renderPosts(posts);
}
postForm.reset();
});


fetchAndRenderPosts();
1 change: 1 addition & 0 deletions Existing_API_Collection/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
|[Bored API](./BoredAPI/)|Bored API is a versatile tool designed to provide users with random activity suggestions when they're feeling bored. With this API, users can access a wide range of activities to spark inspiration and alleviate boredom. From creative hobbies to outdoor adventures, Bored API offers something for everyone.|
|[Unsplash API](./unsplashApi/)| An API that enables users to retrieve high quality and copyright free Images from Unsplash and also get random Images |
|[NewsBuster](./news-buster-api/)|This API helps you gain worldly knowledge with a better frontend by fetching API |
|[JSON_Placeholder_API](./JSON_Plaeholder_API/)| this api is used to test basic crud operations on fake data posts |
|[Gold Silver Price](./Gold,silver_price_API/)|This API helps you check realtime price of gold and silver for each categories |
|[GeoAPI](./GeoAPI/)| GeoAPI is a simple RESTful API that allows you to convert addresses to geographic coordinates (latitude and longitude) and vice versa. This API is built using Node.js and Express.|
|[Currency Converter API](./Currency_Converter_API/)| Currency_Converter is a simple RESTful API that allows you to convert currency values to different currency|
Loading