Skip to content

Commit

Permalink
Merge pull request #77 from olasunkanmi-SE/test
Browse files Browse the repository at this point in the history
Test
  • Loading branch information
olasunkanmi-SE authored May 19, 2024
2 parents f9931f7 + 4382efd commit efaa7a9
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 92 deletions.
3 changes: 2 additions & 1 deletion api/controllers/document-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ export class DocumentController {
const data = await documentHandler.handle();
if (data) {
const result = Result.ok(data.getValue());
res.status(200).json(result);
return res.status(200).json(result);
}
return res.status(400).json(Result.fail("Unable to retrieve documents", 400));
} catch (error) {
generateErrorResponse(error, res, next);
next(error);
Expand Down
29 changes: 20 additions & 9 deletions api/controllers/document-type.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as express from "express";
import { DocumentTypeHandler } from "../handlers/document-type";
import { Result } from "../lib/result";
import { docTypeRequestSchema } from "../lib/validation-schemas";
import { generateErrorResponse } from "../utils/utils";
import { DocumentTypeHandler } from "./../handlers/create-document-type";
import { GetDocumentTypeHandler } from "./../handlers/get-document-type";
export class DocmentTypeController {
path = "/document/type";
router = express.Router();
Expand All @@ -12,13 +13,10 @@ export class DocmentTypeController {

initRoutes() {
this.router.post(`${this.path}/create`, this.createDocumentType);
this.router.get(`${this.path}`, this.getDocumentType);
}

async createDocumentType(
req: express.Request,
res: express.Response,
next: express.NextFunction,
) {
async createDocumentType(req: express.Request, res: express.Response, next: express.NextFunction) {
try {
const { name } = docTypeRequestSchema.parse(req.body);
const documentTypeHandler = new DocumentTypeHandler();
Expand All @@ -27,12 +25,25 @@ export class DocmentTypeController {
const result = Result.ok(data.getValue());
res.status(200).json(result);
} else {
res
.status(400)
.json(Result.fail("Unable to create document type", 400));
res.status(400).json(Result.fail("Unable to create document type", 400));
}
} catch (error) {
generateErrorResponse(error, res, next);
}
}

async getDocumentType(req: express.Request, res: any, next: express.NextFunction) {
try {
const getDocumentTypeHandler: GetDocumentTypeHandler = new GetDocumentTypeHandler();
const data = await getDocumentTypeHandler.handle();
if (data) {
const result = Result.ok(data.getValue());
return res.status(200).json(result);
}
return res.status(400).json(Result.fail("Unable to retrieve document types", 400));
} catch (error) {
generateErrorResponse(error, res, next);
next(error);
}
}
}
28 changes: 22 additions & 6 deletions api/controllers/domain.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as express from "express";
import { DomainHandler } from "../handlers/domain.handler";
import { CreateDomainHandler } from "../handlers/create-domain.handler";
import { Result } from "../lib/result";
import { domainRequestSchema } from "../lib/validation-schemas";
import { generateErrorResponse } from "../utils/utils";
import { GetDomainHandler } from "../handlers/get-domain-handler";
export class DomainController {
path = "/domain";
router = express.Router();
Expand All @@ -12,19 +13,34 @@ export class DomainController {

initRoutes() {
this.router.post(`${this.path}/create`, this.createDomain);
this.router.get(`${this.path}`, this.getDocumentDomain);
}

async createDomain(req: express.Request, res: any, next: express.NextFunction): Promise<void> {
try {
const { name } = domainRequestSchema.parse(req.body);
const domainHandler = new DomainHandler();
const domainHandler = new CreateDomainHandler();
const data = await domainHandler.handle({ name });
if (data) {
if (data.isSuccess) {
const result = Result.ok(data.getValue());
res.status(200).json(result);
} else {
res.status(400).json(Result.fail("Unable to create domain", 400));
return res.status(200).json(result);
}
return res.status(400).json(Result.fail("Unable to create domain", 400));
} catch (error) {
generateErrorResponse(error, res, next);
next(error);
}
}

async getDocumentDomain(req: express.Request, res: any, next: express.NextFunction) {
try {
const domainHandler = new GetDomainHandler();
const data = await domainHandler.handle();
if (data.isSuccess) {
const result = Result.ok(data.getValue());
return res.status(200).json(result);
}
return res.status(400).json(Result.fail("Unable to retrieve document domain", 400));
} catch (error) {
generateErrorResponse(error, res, next);
next(error);
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { DomainRepository } from "./../repositories/domain.repository";
import { DomainRepository } from "../repositories/domain.repository";
import { IRequestHandler } from "../interfaces/handler";
import { Result } from "../lib/result";
import { ICreateDomainRequestDTO } from "../repositories/dtos/dtos";
import { IDomainModel } from "../repositories/model";

export class DomainHandler
implements
IRequestHandler<ICreateDomainRequestDTO, Result<IDomainModel | undefined>>
{
async handle(
request: ICreateDomainRequestDTO,
): Promise<Result<IDomainModel | undefined>> {
export class CreateDomainHandler implements IRequestHandler<ICreateDomainRequestDTO, Result<IDomainModel | undefined>> {
async handle(request: ICreateDomainRequestDTO): Promise<Result<IDomainModel | undefined>> {
try {
let response: IDomainModel | undefined;
const { name } = request;
Expand Down
12 changes: 12 additions & 0 deletions api/handlers/get-document-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { DocumentTypeRepository } from "./../repositories/document-type.repository";
import { IRequestHandler } from "../interfaces/handler";
import { IDocumentTypeModel } from "../repositories/model";
import { Result } from "../lib/result";

export class GetDocumentTypeHandler implements IRequestHandler<{}, Result<IDocumentTypeModel[]>> {
async handle(): Promise<Result<IDocumentTypeModel[]>> {
const documentTypeRepository: DocumentTypeRepository = new DocumentTypeRepository();
const response: IDocumentTypeModel[] = await documentTypeRepository.getDocumentType();
return Result.ok(response);
}
}
3 changes: 1 addition & 2 deletions api/handlers/get-documents-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import { IDocumentModel } from "../repositories/model";
export class GetDocumentsHandler implements IRequestHandler<{}, Result<IDocumentModel[]>> {
async handle(): Promise<Result<IDocumentModel[]>> {
try {
let response: IDocumentModel[];
const documentRespository: DocumentRepository = new DocumentRepository();
response = await documentRespository.getDocuments();
const response: IDocumentModel[] = await documentRespository.getDocuments();
return Result.ok(response);
} catch (error) {
console.error(error);
Expand Down
13 changes: 13 additions & 0 deletions api/handlers/get-domain-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IRequestHandler } from "../interfaces/handler";
import { Result } from "../lib/result";
import { DomainRepository } from "../repositories/domain.repository";
import { IDomainModel } from "../repositories/model";

export class GetDomainHandler implements IRequestHandler<{}, Result<IDomainModel[]>> {
async handle(): Promise<Result<IDomainModel[]>> {
let response: IDomainModel[];
const domainRepository: DomainRepository = new DomainRepository();
response = await domainRepository.getDocumentDomain();
return Result.ok(response);
}
}
13 changes: 9 additions & 4 deletions api/repositories/document-type.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ export class DocumentTypeRepository extends Database {
try {
const exists: IDocumentTypeModel = await this.findOne(name);
if (exists) {
throw new HttpException(
HTTP_RESPONSE_CODE.BAD_REQUEST,
"document type already exists",
);
throw new HttpException(HTTP_RESPONSE_CODE.BAD_REQUEST, "document type already exists");
}
return await this.prisma.documentTypes.create({
data: {
Expand Down Expand Up @@ -49,4 +46,12 @@ export class DocumentTypeRepository extends Database {
console.error("unable to insert many docs", error);
}
}

async getDocumentType(): Promise<IDocumentTypeModel[]> {
try {
return await this.prisma.documentTypes.findMany();
} catch (error) {
console.error(error);
}
}
}
13 changes: 9 additions & 4 deletions api/repositories/domain.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ export class DomainRepository extends Database {
try {
const exists: IDomainModel = await this.findOne(name);
if (exists) {
throw new HttpException(
HTTP_RESPONSE_CODE.BAD_REQUEST,
"domain already exists",
);
throw new HttpException(HTTP_RESPONSE_CODE.BAD_REQUEST, "domain already exists");
}
return await this.prisma.domains.create({
data: {
Expand Down Expand Up @@ -48,4 +45,12 @@ export class DomainRepository extends Database {
console.error("unable to insert many docs", error);
}
}

async getDocumentDomain(): Promise<IDomainModel[]> {
try {
return await this.prisma.domains.findMany();
} catch (error) {
console.error(error);
}
}
}
56 changes: 0 additions & 56 deletions presentation/src/components/Books.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion presentation/src/components/ChatForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function Thread() {
<div className="p-2"></div>
<div className="p-2 ms-auto">
<div>
<Books onDataItemSelect={handleBookSelect} model="documents" />
<Books onDataItemSelect={handleBookSelect} model="document" />
</div>
</div>
<div className="p-2">
Expand Down
16 changes: 15 additions & 1 deletion presentation/src/components/DropDown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Dropdown from "react-bootstrap/Dropdown";
import useAxiosPrivate from "../hooks/useAxiosPrivate";
import { IDataItem } from "../interfaces/document.interface";
import { getLocalStorageData, setLocalStorageData } from "../utils";
import { MODEL_URLS, MODELS } from "../constants";

interface IBookProps {
onDataItemSelect: (data: IDataItem) => void;
Expand Down Expand Up @@ -34,7 +35,20 @@ function Books({ onDataItemSelect, model }: Readonly<IBookProps>) {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getDataItems = async (model: string): Promise<any> => {
const response = await axiosPrivate.get(`/${model}`);
let url = "";
const models = Object.keys(MODEL_URLS);
if (models.includes(model)) {
if (model === MODELS.document) {
url = MODEL_URLS.document;
}
if (model === MODELS.domain) {
url = MODEL_URLS.domain;
}
if (model === MODELS.documentType) {
url = MODEL_URLS.documentType;
}
}
const response = await axiosPrivate.get(url);
return response ? response.data.data : [];
};

Expand Down
12 changes: 12 additions & 0 deletions presentation/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,15 @@ export enum CHAT_PARAMS {
MATCH_COUNT = 3,
SIMILARITY_THRESHOLD = 0.7,
}

export enum MODEL_URLS {
document = "/documents",
domain = "/domain",
documentType = "/docum",
}

export enum MODELS {
document = "document",
domain = "domain",
documentType = "/documentType",
}

0 comments on commit efaa7a9

Please sign in to comment.