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

added: Backend for IndustryTrends Feature #875

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions backend/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PORT = "your_server_port"
MONGO_URI = "your_mongo_uri"
DB_NAME = "your_db_name"
54 changes: 54 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
bin
obj
csx
.vs
edge
Publish

*.user
*.suo
*.cscfg
*.Cache
project-lock.json


/packages
/TestResults

/tools/NuGet.exe
/App_Data
/secrets
/data
.secrets
appsettings.json
local.settings.json

node_modules
dist

# Local python packages
.python_packages/

# Python Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Azurite artifacts
__blobstorage__
__queuestorage__
__azurite_db*__.json

.vscode

test

143 changes: 143 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@

# 🚀 DevDisplay API: Your Gateway to Developer Trends

## 🌟 Project Overview

DevDisplay API is a powerful backend service that aggregates and serves trending developer content from GitHub and Dev.to. It provides developers and tech enthusiasts with scraped insights into the most exciting developments in the tech world.

## ✨ Key Features

- **Trending Insights**: Discover top developers, repositories, and posts
- **Regularly scraped data**: Latest data scraped from authentic sources
- **Flexible Time Frames**: Choose from daily, weekly, or monthly trending data
- **Multi-Source Aggregation**: Pulls data from GitHub and Dev.to
- **Newsletter Subscription**: Stay updated with the latest tech trends

## 🛠 Tech Stack

- **Backend**: Node.js & Express.js
- **Database**: MongoDB with Mongoose ORM
- **Data Sources**: GitHub.com, Dev.to

## 📂 Project Structure

```
DevDisplay-API/
├── controllers/
│ ├── dev.controllers.js
│ ├── github.controllers.js
│ └── subscribers.controllers.js
├── db/
│ └── index.js
|
├── cron/
│ └── dev.cron.js
│ └── github.cron.js
│ └── test.cron.js
├── models/
│ ├── developers.models.js
│ ├── post.models.js
│ ├── repositories.models.js
│ └── subscribers.models.js
├── routes/
│ ├── dev.routes.js
│ ├── github.routes.js
│ └── subscribers.routes.js
└── utils/
├── randomPeriod.utils.js
├── response.utils.js
└── urlBuilder.utils.js

```

## 🌐 API Endpoints

### Dev.to Endpoints

- `GET /devdisplay/v1/trending/dev/posts/:since`
- `GET /devdisplay/v1/trending/dev/getRandomPost`

### GitHub Endpoints

- `GET /devdisplay/v1/trending/github/repositories/:since`
- `GET /devdisplay/v1/trending/github/developers/:since`
- `GET /devdisplay/v1/trending/github/getRandomDev`
- `GET /devdisplay/v1/trending/github/getRandomRepo`

### Subscription Endpoints

- `POST /devdisplay/v1/subscribers`

## 🚀 Quick Start Guide

### Prerequisites

- Node.js
- npm
- MongoDB

### Installation Steps

1. Clone the repository

```bash
git clone https://github.com/Thakar-Advait/DevDisplay-API.git
cd DevDisplay-API

```

2. Install Dependencies

```bash
npm install

```

3. Configure Environment Variables Create a `.env` file with the following variables:

```
PORT=3000
MONGO_URI=your_mongodb_connection_string
DB_NAME=devdisplay

```

4. Prepare MongoDB Collections Ensure your MongoDB database has these collections:

- `daily_trending_devs`
- `daily_trending_posts`
- `daily_trending_repos`
- `weekly_trending_devs`
- `weekly_trending_posts`
- `weekly_trending_repos`
- `monthly_trending_devs`
- `monthly_trending_posts`
- `monthly_trending_repos`
- `tests`
5. Start the Server

```bash
npm run dev

```

## 🔒 Environment Configuration

Refer to `.env.sample` for detailed environment variable setup.

## 📄 License

[Insert Your License Here]

## 🙌 Acknowledgments

Special thanks to GitHub and Dev.to for providing the incredible developer data that powers this API.

----------

**Built with ❤️ by Developers, for Developers**
41 changes: 41 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import express from "express";
import { devRouter } from "./routes/dev.routes.js";
import { githubRouter } from "./routes/github.routes.js";
import { subscribersRouter } from "./routes/subscribers.routes.js";
import { getSubscribers } from "./controllers/subscribers.controllers.js";
import Subscribers from "./models/subscribers.models.js";
const app = express();

// Middleware to parse incoming JSON requests
app.use(express.json());

// Use the devRouter for the "/api/v1/trending/dev" route
app.use("/devdisplay/v1/trending/dev", devRouter);

// Use the githubRouter for the "/api/v1/trending/github" route
app.use("/devdisplay/v1/trending/github", githubRouter);

app.use("/devdisplay/v1/subscribers", getSubscribers);

app.get("/", (req, res) => {
res.status(200).json({
message: "Welcome to the DevDisplay API!"
});
})

app.get("/health", (req, res) => {
res.status(200).json({
message: "DevDisplay API is healthy!"
});
})

// Global error handling middleware (optional)
app.use((err, req, res, next) => {
console.error(err.stack); // Log the error stack for debugging
res.status(500).json({
message: "Something went wrong!",
error: err.message
});
});

export default app;
97 changes: 97 additions & 0 deletions backend/controllers/dev.controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import * as Dev_models from "../models/post.models.js";
import * as responseClass from "../utils/response.utils.js";
import { pickRandomPeriod } from "../utils/randomPeriod.utils.js"

// Helper function to fetch posts based on time period
const fetchPostsForPeriod = async (timePeriod) => {
let model;
switch (timePeriod) {
case "daily":
model = Dev_models.Post_daily;
break;
case "weekly":
model = Dev_models.Post_weekly;
break;
case "monthly":
model = Dev_models.Post_monthly;
break;
default:
throw new responseClass.ApiError(400, "Invalid time period specified.");
}

// Fetching posts for the chosen period
const response = await model.find();
if (!response || response.length === 0) {
throw new responseClass.ApiError(404, `No trending posts found for time period: '${timePeriod}'`);
}
return response;
};

const fetchTrendingPosts = async (req, res) => {
// Extract the "since" parameter from the URL, defaulting to "daily" if not provided
const { since = 'daily' } = req.params;

try {
// Fetch posts based on the time period
const posts = await fetchPostsForPeriod(since);

// Return a consistent response with the fetched posts
return res.status(200).json(new responseClass.ApiResponse(
200,
`Fetched trending posts for time period: '${since}'`,
posts
));

} catch (error) {
// Generic error handling for unexpected errors
console.error(error); // Optional: Add logging to track errors
return res.status(500).json(new responseClass.ApiResponse(
500,
`Something went wrong while fetching trending posts for time period: '${since}' --> \n${error.message || error}`
));
}
}

const fetchRandomPost = async (req, res) => {
const since = pickRandomPeriod();
try {
let posts;
switch (since) {
case "daily":
posts = await Dev_models.Post_daily.aggregate([
{ $sample: { size: 1 } }
])
break;
case "weekly":
posts = await Dev_models.Post_weekly.aggregate([
{ $sample: { size: 1 } }
])
break;
case "monthly":
posts = await Dev_models.Post_monthly.aggregate([
{ $sample: { size: 1 } }
])
break;
default:
posts = await Dev_models.Post_daily.aggregate([
{ $sample: { size: 1 } }
])
break;
}
if (posts.length > 0) {
console.log('Random Document:', posts[0]);
return res.status(200).json(new responseClass.ApiResponse(200, `Fetched random post for time period: '${since}'`, posts[0]));
} else {
console.log('No documents found.');
return res.status(404).json(new responseClass.ApiResponse(404, `No random posts found for time period: '${since}'`));
}
} catch (error) {
console.error(error);
return res.status(500).json(new responseClass.ApiResponse(500, `Something went wrong while fetching random post for time period: '${since}' --> \n${error.message || error}`));
}
}

export {
fetchTrendingPosts,
fetchRandomPost
};
Loading