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 fetch annotation options api without db integration
- Loading branch information
1 parent
08cdfb0
commit 6032c3f
Showing
9 changed files
with
191 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,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; |
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,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}; |
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,5 @@ | ||
export interface iAnnotationModel { | ||
id: string; | ||
name: string; | ||
created_at: 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
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"); | ||
} | ||
} |
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,9 @@ | ||
/* | ||
Creates table annotations. | ||
*/ | ||
CREATE TABLE annotations | ||
( | ||
id UUID PRIMARY KEY, | ||
name VARCHAR(225) NOT NULL, | ||
created_at TIMESTAMPTZ | ||
) |
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,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 | ||
); | ||
// }); | ||
} | ||
} |