diff --git a/src/api/controllers/DomainsController.ts b/src/api/controllers/DomainsController.ts new file mode 100644 index 0000000..6166aae --- /dev/null +++ b/src/api/controllers/DomainsController.ts @@ -0,0 +1,30 @@ +/* +The Licensed Work is (c) 2024 Sygma +SPDX-License-Identifier: LGPL-3.0-only +*/ +import type { FastifyReply, FastifyRequest } from "fastify"; +import type { DataSource } from "typeorm"; + +import { logger } from "../../utils/logger"; +import { DomainsService } from "../services/dataAccess/domains.service"; + +export class DomainsController { + private domainsService: DomainsService; + + constructor(dataSource: DataSource) { + this.domainsService = new DomainsService(dataSource); + } + + public async getDomains( + request: FastifyRequest, + reply: FastifyReply, + ): Promise { + try { + const domainsResult = await this.domainsService.findDomains({}); + await reply.status(200).send(domainsResult); + } catch (error) { + logger.error("Error occurred when fetching domains", error); + await reply.status(500).send({ error: "Internal server error" }); + } + } +} diff --git a/src/api/routes/domains.routes.ts b/src/api/routes/domains.routes.ts new file mode 100644 index 0000000..990160f --- /dev/null +++ b/src/api/routes/domains.routes.ts @@ -0,0 +1,18 @@ +/* +The Licensed Work is (c) 2024 Sygma +SPDX-License-Identifier: LGPL-3.0-only +*/ +import type { FastifyInstance } from "fastify"; + +import { DomainsController } from "../controllers/DomainsController"; +import { domainsSchema } from "../schemas/domains.schema"; + +export async function domainRoutes(server: FastifyInstance): Promise { + const domainsController = new DomainsController(server.db); + server.get( + "/domains", + { schema: domainsSchema }, + domainsController.getDomains.bind(domainsController), + ); + return Promise.resolve(); +} diff --git a/src/api/routes/index.ts b/src/api/routes/index.ts index f523559..e0bf4b7 100644 --- a/src/api/routes/index.ts +++ b/src/api/routes/index.ts @@ -4,8 +4,10 @@ SPDX-License-Identifier: LGPL-3.0-only */ import type { FastifyInstance } from "fastify"; +import { domainRoutes } from "./domains.routes"; import { transferRoutes } from "./transfers.routes"; export async function registerRoutes(server: FastifyInstance): Promise { await server.register(transferRoutes, { prefix: "/api" }); + await server.register(domainRoutes, { prefix: "/api" }); } diff --git a/src/api/schemas/domains.schema.ts b/src/api/schemas/domains.schema.ts new file mode 100644 index 0000000..5a2207e --- /dev/null +++ b/src/api/schemas/domains.schema.ts @@ -0,0 +1,24 @@ +/* +The Licensed Work is (c) 2024 Sygma +SPDX-License-Identifier: LGPL-3.0-only +*/ +import { domainSchema } from "."; + +export const domainsSchema = { + summary: "Get domains", + response: { + 200: { + description: "List of domains", + content: { + "application/json": { + schema: { + type: "array", + items: { + ...domainSchema, + }, + }, + }, + }, + }, + }, +}; diff --git a/src/api/services/dataAccess/domains.service.ts b/src/api/services/dataAccess/domains.service.ts new file mode 100644 index 0000000..c08f104 --- /dev/null +++ b/src/api/services/dataAccess/domains.service.ts @@ -0,0 +1,24 @@ +/* +The Licensed Work is (c) 2024 Sygma +SPDX-License-Identifier: LGPL-3.0-only +*/ +import type { DataSource, FindOptionsWhere, Repository } from "typeorm"; + +import { Domain } from "../../../model"; + +export class DomainsService { + private domainRepository: Repository; + constructor(dataSource: DataSource) { + this.domainRepository = dataSource.getRepository(Domain); + } + + public async findDomains(where: FindOptionsWhere): Promise { + const domains = await this.domainRepository.find({ + where, + order: { + id: "ASC", + }, + }); + return domains; + } +}