-
Notifications
You must be signed in to change notification settings - Fork 0
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 #4 from 0xTxbi/news-data-service
news data service
- Loading branch information
Showing
7 changed files
with
99 additions
and
19 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
This file was deleted.
Oops, something went wrong.
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,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; | ||
} |
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,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"`); | ||
} | ||
|
||
} |
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,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."); | ||
} | ||
} | ||
} |
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,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"); | ||
}); |
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