Skip to content

Commit

Permalink
feat(api): implement fetch annotation options api without db integration
Browse files Browse the repository at this point in the history
  • Loading branch information
swalahamani committed Nov 11, 2023
1 parent 08cdfb0 commit 6032c3f
Show file tree
Hide file tree
Showing 9 changed files with 191 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 annotationsRoute from "./routes/annotationsRoute";

const getRouter = (): Router => {
const apiRouter = Router();
Expand All @@ -11,6 +12,7 @@ const getRouter = (): Router => {
authRoute(apiRouter);
userRoute(apiRouter);
verificationRoute(apiRouter);
annotationsRoute(apiRouter);

return apiRouter;
};
Expand Down
69 changes: 69 additions & 0 deletions src/api/routes/annotationsRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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 {MultiAnnotation} from "@customTypes/appDataTypes/annotationTypes";
import AnnotationService from "@services/AnnotationService";

const route = Router();
const annotationService = new AnnotationService();

const annotationsRoute: RouteType = (apiRouter) => {
apiRouter.use("/annotations", route);

/*
Registering isAuthorized middleware to the entire /users route
as all the endpoint in this route needs authorization.
*/
// route.use(middlewares.isAuthorized);

route.get(
"/",
async (
req: iRequest,
res: iResponse<MultiAnnotation>,
next: NextFunction
) => {
const uniqueRequestId = expressUtil.parseUniqueRequestId(req);

logger.debug(
uniqueRequestId,
"Calling GET:/annotations endpoint with body:",
null,
{
requestBody: req.body,
}
);

try {
const result = await annotationService.getAnnotationOptions(
uniqueRequestId
);

logger.debug(
uniqueRequestId,
"GET:/annotations:: Completed annotationsService.getAnnotationOptions & sending result to client:",
null,
{
result,
}
);

const {httpStatusCode} = result;

return res.status(httpStatusCode).json(result);
} catch (error) {
logger.error(uniqueRequestId, "Error on GET:/annotations:", error);

return next(error);
}
}
);
};

export default annotationsRoute;
16 changes: 16 additions & 0 deletions src/customTypes/appDataTypes/annotationTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {StringArray} from "customTypes/commonTypes";

interface iAnnotation {
id: string;
name: string;
}

type MultiAnnotation = {
ids: StringArray;

items: {
[key in string]: iAnnotation;
};
};

export type {iAnnotation, MultiAnnotation};
5 changes: 5 additions & 0 deletions src/db/models/annotations.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface iAnnotationModel {
id: string;
name: string;
created_at: string;
}
44 changes: 44 additions & 0 deletions src/db/repositories/AnnotationsRepository.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";

import {annotations as sql} from "@db/sql";

/*
This repository mixes hard-coded and dynamic SQL, just to show how to use both.
*/
export default class AnnotationsRepository {
/**
* @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 annotations table.
*
* @returns null
*/
async create(): Promise<null> {
return this.db.none(sql.create);
}

// Returns all annotations records;
async all(): Promise<iAnnotationModel[]> {
return 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 @@ -4,6 +4,7 @@ import UsersRepository from "./UsersRepository";
import EmailLogsRepository from "./EmailLogsRepository";
import ResetPasswordRepository from "./ResetPasswordRepository";
import EmailVerificationRequestLogsRepository from "./EmailVerificationRequestLogsRepository";
import AnnotationsRepository from "./AnnotationsRepository";

/**
* Database Interface Extensions:
Expand All @@ -13,6 +14,7 @@ interface iDBInterfaceExtensions {
emailLogs: EmailLogsRepository;
resetPasswordLogs: ResetPasswordRepository;
emailVerificationRequestLogs: EmailVerificationRequestLogsRepository;
annotations: AnnotationsRepository;
}

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

export type {DBTaskType, NullableDBTaskType};
9 changes: 9 additions & 0 deletions src/db/sql/annotations/create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Creates table annotations.
*/
CREATE TABLE annotations
(
id UUID PRIMARY KEY,
name VARCHAR(225) NOT NULL,
created_at TIMESTAMPTZ
)
4 changes: 4 additions & 0 deletions src/db/sql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ export const emailVerificationRequestLogs = {
create: sql("emailVerificationRequestLogs/create.sql"),
add: sql("emailVerificationRequestLogs/add.sql"),
};

export const annotations = {
create: sql("annotations/create.sql"),
};
39 changes: 39 additions & 0 deletions src/services/AnnotationService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 {MultiAnnotation} from "customTypes/appDataTypes/annotationTypes";

export default class AnnotationService {
public async getAnnotationOptions(
uniqueRequestId: NullableString
): Promise<iGenericServiceResult<MultiAnnotation | null>> {
// return db.task("get-annotation-options", async (task) => {
logger.silly("Retrieving the annotations");
// const annotationRecords = await task.annotations.all();

const multiAnnotations: MultiAnnotation = {
ids: [],

items: {},
};

// if (annotationRecords) {
// }

return serviceUtil.buildResult(
true,
httpStatusCodes.SUCCESS_OK,
uniqueRequestId,
null,
multiAnnotations
);
// });
}
}

0 comments on commit 6032c3f

Please sign in to comment.