-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from Some1Uknow/mybranch
Quote-API added
- Loading branch information
Showing
6 changed files
with
1,585 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
# Motivational Quotes API | ||
|
||
The Motivational Quotes API provides motivational quotes categorized by genre. You can fetch quotes based on genre, search for quotes, submit new quotes, rate quotes, and retrieve a random quote. | ||
|
||
## Base URL | ||
|
||
`http://localhost:3000` | ||
|
||
## Endpoints | ||
|
||
### 1. Get Quotes by Genre | ||
|
||
**GET /quotes/:genre** | ||
|
||
Fetch quotes from a specified genre. | ||
|
||
#### URL Parameters | ||
|
||
- `genre` (required): The genre of the quotes. Available genres: | ||
- `happiness` | ||
- `motivation` | ||
- `hardwork` | ||
- `peace` | ||
|
||
#### Query Parameters | ||
|
||
- `limit` (optional): The number of quotes to retrieve. Defaults to all quotes in the specified genre. | ||
- Example: `1`, `5` | ||
|
||
#### Example Requests | ||
|
||
1. **Fetch all quotes from the motivation genre** | ||
|
||
GET /quotes/motivation | ||
|
||
**Example Response** | ||
|
||
```json | ||
[ | ||
{ | ||
"quote": "The best way to get started is to quit talking and begin doing.", | ||
"author": "Walt Disney" | ||
}, | ||
{ | ||
"quote": "The pessimist sees difficulty in every opportunity. The optimist sees opportunity in every difficulty.", | ||
"author": "Winston Churchill" | ||
} | ||
] | ||
|
||
- Fetch 1 quote from the happiness genre | ||
|
||
GET /quotes/happiness?limit=1 | ||
|
||
Example Response | ||
|
||
[ | ||
{ | ||
"quote": "Happiness is not something ready made. It comes from your own actions.", | ||
"author": "Dalai Lama" | ||
} | ||
] | ||
|
||
2. Search Quotes | ||
|
||
GET /quotes/search | ||
Search for quotes based on a query. | ||
|
||
Query Parameters | ||
q (required): The search query. | ||
|
||
Example Request | ||
GET /quotes/search?q=success | ||
|
||
Example response | ||
[ | ||
{ | ||
"quote": "There are no secrets to success. It is the result of preparation, hard work, and learning from failure.", | ||
"author": "Colin Powell", | ||
"genre": "hardwork" | ||
}, | ||
{ | ||
"quote": "Success is not the key to happiness. Happiness is the key to success. If you love what you are doing, you will be successful.", | ||
"author": "Albert Schweitzer", | ||
"genre": "hardwork" | ||
} | ||
] | ||
|
||
- POST /quotes | ||
Submit a new quote to the API. | ||
|
||
Request Body | ||
|
||
genre (required): The genre of the quote. | ||
quote (required): The text of the quote. | ||
author (required): The author of the quote. | ||
|
||
{ | ||
"genre": "motivation", | ||
"quote": "Success is not the key to happiness. Happiness is the key to success.", | ||
"author": "Albert Schweitzer" | ||
} | ||
|
||
Example response | ||
|
||
{ | ||
"quote": "Success is not the key to happiness. Happiness is the key to success.", | ||
"author": "Albert Schweitzer" | ||
} | ||
|
||
- Rate a Quote | ||
PUT /quotes/:genre/:index/rate | ||
Rate a quote in the specified genre and index. | ||
URL Parameters | ||
|
||
genre (required): The genre of the quote. | ||
index (required): The index of the quote within the genre. | ||
|
||
Request Body | ||
|
||
rating (required): The rating for the quote. | ||
|
||
Example request | ||
|
||
{ | ||
"rating": 5 | ||
} | ||
|
||
Example response | ||
|
||
{ | ||
"quote": "The best way to get started is to quit talking and begin doing.", | ||
"author": "Walt Disney", | ||
"rating": 5 | ||
} | ||
|
||
5. Get a Random Quote | ||
GET /quotes/random | ||
Fetch a random quote from the API. | ||
Example Request | ||
|
||
Example response | ||
|
||
{ | ||
"quote": "Peace begins with a smile.", | ||
"author": "Mother Teresa", | ||
"genre": "peace" | ||
} | ||
|
||
#### Using JavaScript fetch API | ||
|
||
// Fetch quotes by genre | ||
fetch('http://localhost:3000/quotes/motivation?limit=1') | ||
.then(response => response.json()) | ||
.then(data => console.log(data)) | ||
.catch(error => console.error('Error:', error)); | ||
|
||
// Search for quotes | ||
fetch('http://localhost:3000/quotes/search?q=success') | ||
.then(response => response.json()) | ||
.then(data => console.log(data)) | ||
.catch(error => console.error('Error:', error)); | ||
|
||
// Submit a new quote | ||
fetch('http://localhost:3000/quotes', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
genre: 'motivation', | ||
quote: 'Success is not the key to happiness. Happiness is the key to success.', | ||
author: 'Albert Schweitzer' | ||
}) | ||
}) | ||
.then(response => response.json()) | ||
.then(data => console.log(data)) | ||
.catch(error => console.error('Error:', error)); | ||
|
||
// Rate a quote | ||
fetch('http://localhost:3000/quotes/motivation/0/rate', { | ||
method: 'PUT', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
rating: 5 | ||
}) | ||
}) | ||
.then(response => response.json()) | ||
.then(data => console.log(data)) | ||
.catch(error => console.error('Error:', error)); | ||
|
||
// Get a random quote | ||
fetch('http://localhost:3000/quotes/random') | ||
.then(response => response.json()) | ||
.then(data => console.log(data)) | ||
.catch(error => console.error('Error:', error)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import express from 'express'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
import cors from 'cors'; | ||
import 'dotenv/config'; | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
|
||
const app = express(); | ||
const port = process.env.PORT || 3000; | ||
|
||
app.use(cors()); | ||
app.use(express.json()); | ||
|
||
const quotesPath = path.join(__dirname, 'quotes.json'); | ||
let quotesData = JSON.parse(fs.readFileSync(quotesPath, 'utf8')); | ||
|
||
// Helper function to save quotes to the file | ||
const saveQuotes = () => { | ||
fs.writeFileSync(quotesPath, JSON.stringify(quotesData, null, 2), 'utf8'); | ||
}; | ||
|
||
// Basic route to check if the server is running | ||
app.get("/", (req, res) => { | ||
res.send("Hello"); | ||
}); | ||
|
||
// Get quotes by genre | ||
app.get('/quotes/:genre', (req, res) => { | ||
const genre = req.params.genre.toLowerCase(); | ||
const limit = parseInt(req.query.limit) || quotesData[genre].length; | ||
|
||
const quotes = quotesData[genre]?.slice(0, limit) || []; | ||
if (quotes.length === 0) { | ||
res.status(404).json({ message: 'No quotes found for this genre' }); | ||
} else { | ||
res.json(quotes); | ||
} | ||
}); | ||
|
||
// Search quotes | ||
app.get('/quotes/search', (req, res) => { | ||
const query = req.query.q.toLowerCase(); | ||
const results = []; | ||
|
||
for (const genre in quotesData) { | ||
quotesData[genre].forEach(quote => { | ||
if (quote.quote.toLowerCase().includes(query) || quote.author.toLowerCase().includes(query)) { | ||
results.push({ ...quote, genre }); | ||
} | ||
}); | ||
} | ||
|
||
if (results.length === 0) { | ||
res.status(404).json({ message: 'No quotes found for this search query' }); | ||
} else { | ||
res.json(results); | ||
} | ||
}); | ||
|
||
// Submit a new quote | ||
app.post('/quotes', (req, res) => { | ||
const { genre, quote, author } = req.body; | ||
|
||
if (!genre || !quote || !author) { | ||
return res.status(400).json({ message: 'Genre, quote, and author are required' }); | ||
} | ||
|
||
if (!quotesData[genre]) { | ||
quotesData[genre] = []; | ||
} | ||
|
||
const newQuote = { quote, author }; | ||
quotesData[genre].push(newQuote); | ||
saveQuotes(); | ||
|
||
res.status(201).json(newQuote); | ||
}); | ||
|
||
// Rate a quote | ||
app.put('/quotes/:genre/:index/rate', (req, res) => { | ||
const { genre, index } = req.params; | ||
const { rating } = req.body; | ||
|
||
if (!quotesData[genre] || !quotesData[genre][index]) { | ||
return res.status(404).json({ message: 'Quote not found' }); | ||
} | ||
|
||
quotesData[genre][index].rating = rating; | ||
saveQuotes(); | ||
|
||
res.json(quotesData[genre][index]); | ||
}); | ||
|
||
// Get a random quote | ||
app.get('/quotes/random', (req, res) => { | ||
const allQuotes = Object.values(quotesData).flat(); | ||
const randomQuote = allQuotes[Math.floor(Math.random() * allQuotes.length)]; | ||
res.json(randomQuote); | ||
}); | ||
|
||
// Mock user authentication | ||
const users = { | ||
"user1": { password: "password1" }, | ||
"user2": { password: "password2" } | ||
}; | ||
|
||
app.post('/login', (req, res) => { | ||
const { username, password } = req.body; | ||
|
||
if (users[username] && users[username].password === password) { | ||
res.json({ message: 'Login successful', username }); | ||
} else { | ||
res.status(401).json({ message: 'Invalid credentials' }); | ||
} | ||
}); | ||
|
||
app.listen(port, () => { | ||
console.log(`Server running on http://localhost:${port}`); | ||
}); |
Oops, something went wrong.