generated from PluteoJS/pluteojs-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): implement annotate news api without db integration
- Loading branch information
Showing
7 changed files
with
154 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
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,68 @@ | ||
import {NextFunction, Router} from "express"; | ||
|
||
import logger from "@loaders/logger"; | ||
|
||
// import middlewares from "@api/middlewares"; | ||
|
||
import expressUtil from "@util/expressUtil"; | ||
|
||
import {iRequest, iResponse, RouteType} from "@customTypes/expressTypes"; | ||
import {iNewsAnnotationsInputDTO} from "@customTypes/appDataTypes/newsTypes"; | ||
import NewsService from "@services/NewsService"; | ||
|
||
const route = Router(); | ||
const newsService = new NewsService(); | ||
|
||
const newsRoute: RouteType = (apiRouter) => { | ||
apiRouter.use("/news", route); | ||
|
||
/* | ||
Registering isAuthorized middleware to the entire /users route | ||
as all the endpoint in this route needs authorization. | ||
*/ | ||
// route.use(middlewares.isAuthorized); | ||
|
||
route.post( | ||
"/annotate", | ||
async ( | ||
req: iRequest<iNewsAnnotationsInputDTO>, | ||
res: iResponse<null>, | ||
next: NextFunction | ||
) => { | ||
const uniqueRequestId = expressUtil.parseUniqueRequestId(req); | ||
|
||
logger.debug( | ||
uniqueRequestId, | ||
"Calling POST:news/annotate endpoint with body:", | ||
null, | ||
{ | ||
requestBody: req.body, | ||
} | ||
); | ||
|
||
try { | ||
const result = await newsService.annotateNews(uniqueRequestId); | ||
|
||
logger.debug( | ||
uniqueRequestId, | ||
"POST:news/annotate:: Completed newsService.annotateNews & sending result to client:", | ||
null, | ||
{ | ||
result, | ||
} | ||
); | ||
|
||
const {httpStatusCode} = result; | ||
|
||
return res.status(httpStatusCode).json(result); | ||
} catch (error) { | ||
console.log("🚀 ~ file: newsRoute.ts:61 ~ error:", error); | ||
logger.error(uniqueRequestId, "Error on POST:news/annotate:", error); | ||
|
||
return next(error); | ||
} | ||
} | ||
); | ||
}; | ||
|
||
export default newsRoute; |
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,4 @@ | ||
export interface iNewsAnnotationsInputDTO { | ||
newsId: string; | ||
annotations: Array<string>; | ||
} |
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 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,44 @@ | ||
import {IDatabase, IMain} from "pg-promise"; | ||
|
||
// import {iAnnotationModel} from "db/models/annotations.model"; -- to be uncommented with correct model | ||
|
||
// import {annotations as sql} from "@db/sql"; -- to be uncommented with correct sql | ||
|
||
/* | ||
This repository mixes hard-coded and dynamic SQL, just to show how to use both. | ||
*/ | ||
export default class NewsAnnotationRepository { | ||
/** | ||
* @param db | ||
* Automated database connection context/interface. | ||
* | ||
* If you ever need to access other repositories from this one, | ||
* you will have to replace type 'IDatabase<any>' with 'any'. | ||
* | ||
* @param pgp | ||
* Library's root, if ever needed, like to access 'helpers' | ||
* or other namespaces available from the root. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
constructor(private db: IDatabase<any>, private pgp: IMain) { | ||
/* | ||
If your repository needs to use helpers like ColumnSet, | ||
you should create it conditionally, inside the constructor, | ||
i.e. only once, as a singleton. | ||
*/ | ||
} | ||
|
||
/** | ||
* Creates the news annotations table. | ||
* | ||
* @returns null | ||
*/ | ||
// async create(): Promise<null> { -- uncomment with correct sql | ||
// return this.db.none(sql.create); | ||
// } | ||
|
||
// Annotates news; | ||
async annotate(): Promise<void> { | ||
this.db.any("SELECT * FROM annotations"); | ||
} | ||
} |
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 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,31 @@ | ||
import logger from "@loaders/logger"; | ||
|
||
import {db} from "@db/index"; | ||
|
||
import serviceUtil from "@util/serviceUtil"; | ||
|
||
import {iGenericServiceResult} from "@customTypes/commonServiceTypes"; | ||
import {httpStatusCodes} from "@customTypes/networkTypes"; | ||
|
||
import {NullableString} from "@customTypes/commonTypes"; | ||
// import {iNewsAnnotationsInputDTO} from "customTypes/appDataTypes/newsTypes"; | ||
|
||
export default class NewsService { | ||
public async annotateNews( | ||
uniqueRequestId: NullableString | ||
): Promise<iGenericServiceResult<null>> { | ||
return db.task("annotate-news", async (task) => { | ||
logger.silly("Annotating the news"); | ||
|
||
await task.newsAnnotations.annotate(); | ||
console.log("hello"); | ||
|
||
return serviceUtil.buildResult( | ||
true, | ||
httpStatusCodes.SUCCESS_CREATED, | ||
uniqueRequestId, | ||
null | ||
); | ||
}); | ||
} | ||
} |