Skip to content

Commit

Permalink
Merge pull request #4 from 0xTxbi/news-data-service
Browse files Browse the repository at this point in the history
news data service
  • Loading branch information
0xTxbi authored Dec 10, 2023
2 parents 96df2c5 + 20f8371 commit 3d16e68
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 19 deletions.
4 changes: 2 additions & 2 deletions data-source.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { DataSource } from "typeorm";

import { User } from "./authentication-service/entities/User";
import { NewsData } from "./news-data-service/entities/NewsData";
import { SentimentData } from "./sentiment-analysis-service/entities/SentimentData";
import { configDotenv } from "dotenv";
import { Watchlist } from "./authentication-service/entities/Watchlist";
import { StockInfo } from "./stock-info-service/entities/StockInfo";
import { News } from "./news-service/entities/News";

// load environment variables from .env file
configDotenv();
Expand All @@ -19,7 +19,7 @@ const AppDataSource = new DataSource({
database: process.env.DB_DATABASE || "",
synchronize: true,
logging: true,
entities: [User, Watchlist, NewsData, SentimentData, StockInfo],
entities: [User, Watchlist, News, SentimentData, StockInfo],
migrations: [],
ssl: {
rejectUnauthorized: false,
Expand Down
16 changes: 0 additions & 16 deletions news-data-service/entities/NewsData.ts

This file was deleted.

28 changes: 28 additions & 0 deletions news-service/entities/News.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";

@Entity()
export class News {
@PrimaryGeneratedColumn()
id: number;

@Column()
title: string;

@Column()
link: string;

@Column("simple-array")
publisher: string[];

@Column()
description: string;

@Column()
imageUrl: string;

@Column()
content: string;

@Column()
timestamp: Date;
}
14 changes: 14 additions & 0 deletions news-service/migrations/1702231539579-NewsEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class NewsEntity1702231539579 implements MigrationInterface {
name = 'NewsEntity1702231539579'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE "news" ("id" SERIAL NOT NULL, "title" character varying NOT NULL, "link" character varying NOT NULL, "publisher" text NOT NULL, "description" character varying NOT NULL, "imageUrl" character varying NOT NULL, "content" character varying NOT NULL, "timestamp" TIMESTAMP NOT NULL, CONSTRAINT "PK_39a43dfcb6007180f04aff2357e" PRIMARY KEY ("id"))`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP TABLE "news"`);
}

}
42 changes: 42 additions & 0 deletions news-service/src/controllers/NewsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// controllers/NewsController.ts
import { JsonController, Param, Get } from "routing-controllers";
import axios from "axios";
import { News } from "../../entities/News";
import * as dotenv from "dotenv";

dotenv.config();

@JsonController("/news")
export class NewsController {
@Get("/:stockSymbol")
async getNewsByStockSymbol(
@Param("stockSymbol") stockSymbol: string
): Promise<News> {
try {
const newsApiUrl = `${process.env.ND_NEWS_API_URL}/news?apikey=${process.env.ND_NEWS_API_KEY}&q=${stockSymbol}&language=en&category=business`;
const response = await axios.get(newsApiUrl);

const extractedNewsProps = response.data.results.map(
(article: any) => ({
article_id: article.article_id,
title: article.title,
link: article.link,
creator: article.creator
? article.creator.join(", ")
: "",
description: article.description,
content: article.content,
pubDate: article.pubDate,
})
);

return extractedNewsProps;
} catch (error) {
console.error(
"Error fetching news articles:",
error.message
);
throw new Error("Failed to fetch news articles.");
}
}
}
11 changes: 11 additions & 0 deletions news-service/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import "reflect-metadata";
import { createExpressServer } from "routing-controllers";
import { NewsController } from "./controllers/NewsController";

const app = createExpressServer({
controllers: [NewsController],
});

app.listen(3002, () => {
console.log("the News Service is running on port 3002");
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"scripts": {
"typeorm": "typeorm-ts-node-commonjs",
"auth-start": "nodemon --watch . --ext ts --exec ts-node authentication-service/src/index.ts",
"stock-info-start": "nodemon --watch . --ext ts --exec ts-node stock-info-service/src/index.ts"
"stock-info-start": "nodemon --watch . --ext ts --exec ts-node stock-info-service/src/index.ts",
"news-service-start": "nodemon --watch . --ext ts --exec ts-node news-service/src/index.ts"
},
"devDependencies": {
"nodemon": "^3.0.2",
Expand Down

0 comments on commit 3d16e68

Please sign in to comment.