Skip to content

Commit

Permalink
feat(api): implement annotate news api without db integration
Browse files Browse the repository at this point in the history
  • Loading branch information
me-nkr committed Nov 12, 2023
1 parent ef3cee1 commit 866258d
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Router} from "express";
import authRoute from "@api/routes/authRoute";
import userRoute from "@api/routes/usersRoute";
import verificationRoute from "@api/routes/verificationRoute";
import newsRoute from "@api/routes/newsRoute";
import annotationsRoute from "./routes/annotationsRoute";

const getRouter = (): Router => {
Expand All @@ -13,6 +14,7 @@ const getRouter = (): Router => {
userRoute(apiRouter);
verificationRoute(apiRouter);
annotationsRoute(apiRouter);
newsRoute(apiRouter);

return apiRouter;
};
Expand Down
68 changes: 68 additions & 0 deletions src/api/routes/newsRoute.ts
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;
4 changes: 4 additions & 0 deletions src/customTypes/appDataTypes/newsTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface iNewsAnnotationsInputDTO {
newsId: string;
annotations: Array<string>;
}
2 changes: 2 additions & 0 deletions src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ResetPasswordRepository,
EmailVerificationRequestLogsRepository,
AnnotationsRepository,
NewsAnnotationRepository,
} from "@db/repositories/index";

import {Diagnostics} from "@db/diagnostics"; // optional diagnostics
Expand Down Expand Up @@ -57,6 +58,7 @@ const initOptions: IInitOptions<iDBInterfaceExtensions> = {
obj.emailVerificationRequestLogs =
new EmailVerificationRequestLogsRepository(obj, pgp);
obj.annotations = new AnnotationsRepository(obj, pgp);
obj.newsAnnotations = new NewsAnnotationRepository(obj, pgp);
},
};

Expand Down
44 changes: 44 additions & 0 deletions src/db/repositories/NewsAnnotationRepository.ts
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");
}
}
3 changes: 3 additions & 0 deletions src/db/repositories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import EmailLogsRepository from "./EmailLogsRepository";
import ResetPasswordRepository from "./ResetPasswordRepository";
import EmailVerificationRequestLogsRepository from "./EmailVerificationRequestLogsRepository";
import AnnotationsRepository from "./AnnotationsRepository";
import NewsAnnotationRepository from "./NewsAnnotationRepository";

/**
* Database Interface Extensions:
Expand All @@ -15,6 +16,7 @@ interface iDBInterfaceExtensions {
resetPasswordLogs: ResetPasswordRepository;
emailVerificationRequestLogs: EmailVerificationRequestLogsRepository;
annotations: AnnotationsRepository;
newsAnnotations: NewsAnnotationRepository;
}

type DBTaskType = pgPromise.ITask<iDBInterfaceExtensions> &
Expand All @@ -29,6 +31,7 @@ export {
ResetPasswordRepository,
EmailVerificationRequestLogsRepository,
AnnotationsRepository,
NewsAnnotationRepository,
};

export type {DBTaskType, NullableDBTaskType};
31 changes: 31 additions & 0 deletions src/services/NewsService.ts
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
);
});
}
}

0 comments on commit 866258d

Please sign in to comment.