|
| 1 | +const express = require('express'); |
| 2 | +const request = require('request-promise'); |
| 3 | +const _ = require('lodash'); |
| 4 | + |
| 5 | +const app = express(); |
| 6 | +const port = 3000; |
| 7 | + |
| 8 | +// Function to fetch blog data from the API |
| 9 | +async function fetchBlogData() { |
| 10 | + const apiUrl = 'https://intent-kit-16.hasura.app/api/rest/blogs'; |
| 11 | + const adminSecret = '32qR4KmXOIpsGPQKMqEJHGJS27G5s7HdSKO3gdtQd2kv5e852SiYwWNfxkZOBuQ6'; |
| 12 | + |
| 13 | + const options = { |
| 14 | + uri: apiUrl, |
| 15 | + headers: { |
| 16 | + 'x-hasura-admin-secret': adminSecret, |
| 17 | + }, |
| 18 | + json: true, |
| 19 | + }; |
| 20 | + |
| 21 | + const response = await request(options); |
| 22 | + return response; |
| 23 | +} |
| 24 | + |
| 25 | +// Function to analyze blog data |
| 26 | +function analyzeBlogData(blogs) { |
| 27 | + const totalBlogs = blogs.length; |
| 28 | + |
| 29 | + // Find the blog with the longest title |
| 30 | + const longestBlog = _.maxBy(blogs, (blog) => blog.title.length); |
| 31 | + |
| 32 | + // Determine the number of blogs with "privacy" in the title |
| 33 | + const privacyBlogs = _.filter(blogs, (blog) => |
| 34 | + blog.title.toLowerCase().includes('privacy') |
| 35 | + ); |
| 36 | + const numPrivacyBlogs = privacyBlogs.length; |
| 37 | + |
| 38 | + // Create an array of unique blog titles |
| 39 | + const uniqueTitles = _.uniqBy(blogs, 'title'); |
| 40 | + |
| 41 | + return { |
| 42 | + totalBlogs, |
| 43 | + longestBlog: longestBlog.title, |
| 44 | + numPrivacyBlogs, |
| 45 | + uniqueTitles: uniqueTitles.map((blog) => blog.title), |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +// Function to search blogs based on the query |
| 50 | +function searchBlogs(blogs, query) { |
| 51 | + const lowercaseQuery = query.toLowerCase(); |
| 52 | + |
| 53 | + // Filter blogs based on the query (case-insensitive) |
| 54 | + const filteredBlogs = _.filter(blogs, (blog) => |
| 55 | + blog.title.toLowerCase().includes(lowercaseQuery) |
| 56 | + ); |
| 57 | + |
| 58 | + return filteredBlogs; |
| 59 | +} |
| 60 | + |
| 61 | +// Memoize the fetchBlogData function to cache results for 1 minute (60000 milliseconds) |
| 62 | +const memoizedFetchBlogData = _.memoize(fetchBlogData, () => 'blogData', 60000); |
| 63 | + |
| 64 | +// Middleware to fetch and analyze blog data with caching |
| 65 | +app.get('/api/blog-stats', async (req, res) => { |
| 66 | + try { |
| 67 | + // Use memoizedFetchBlogData to fetch blog data (with caching) |
| 68 | + const blogs = await memoizedFetchBlogData(); |
| 69 | + |
| 70 | + // Analyze the data and send the response |
| 71 | + const analytics = analyzeBlogData(blogs); |
| 72 | + res.json(analytics); |
| 73 | + } catch (error) { |
| 74 | + console.error(error); |
| 75 | + res.status(500).json({ error: 'An error occurred while fetching and analyzing blog data.' }); |
| 76 | + } |
| 77 | +}); |
| 78 | + |
| 79 | +// Middleware for blog search |
| 80 | +app.get('/api/blog-search', async (req, res) => { |
| 81 | + const query = req.query.query; |
| 82 | + |
| 83 | + // Ensure query parameter is provided |
| 84 | + if (!query) { |
| 85 | + return res.status(400).json({ error: 'Query parameter "query" is required.' }); |
| 86 | + } |
| 87 | + |
| 88 | + try { |
| 89 | + // Use memoizedFetchBlogData to fetch blog data (with caching) |
| 90 | + const blogs = await memoizedFetchBlogData(); |
| 91 | + |
| 92 | + // Filter blogs based on the query |
| 93 | + const filteredBlogs = searchBlogs(blogs, query); |
| 94 | + res.json(filteredBlogs); |
| 95 | + } catch (error) { |
| 96 | + console.error(error); |
| 97 | + res.status(500).json({ error: 'An error occurred while fetching and analyzing blog data.' }); |
| 98 | + } |
| 99 | +}); |
| 100 | + |
| 101 | +// Error handling middleware |
| 102 | +app.use((err, req, res, next) => { |
| 103 | + console.error(err); |
| 104 | + res.status(500).json({ error: 'An internal server error occurred.' }); |
| 105 | +}); |
| 106 | + |
| 107 | +// Start the server |
| 108 | +app.listen(port, () => { |
| 109 | + console.log(`Server is running on port ${port}`); |
| 110 | +}); |
0 commit comments