Skip to content

Commit

Permalink
refactor to use contract
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Nov 2, 2024
1 parent 3e2e56c commit 657afc2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
50 changes: 39 additions & 11 deletions api/src/modules/custom-projects/custom-projects.controller.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,54 @@
import { Controller, Get } from '@nestjs/common';
import { Controller, HttpStatus } from '@nestjs/common';
import { CountriesService } from '@api/modules/countries/countries.service';
import { DataSource } from 'typeorm';
import { ModelAssumptions } from '@shared/entities/model-assumptions.entity';
import { tsRestHandler, TsRestHandler } from '@ts-rest/nest';
import { ControllerResponse } from '@api/types/controller-response.type';
import { customProjectContract } from '@shared/contracts/custom-projects.contract';

@Controller('custom-projects')
@Controller()
export class CustomProjectsController {
constructor(
private readonly countries: CountriesService,
private readonly dataSource: DataSource,
) {}

@Get('available-countries')
async getAvailableCountriesToCreateACustomProject() {
const data =
await this.countries.getAvailableCountriesToCreateACustomProject();
return { data };
@TsRestHandler(customProjectContract.getAvailableCountries)
async getAvailableCountriesToCreateACustomProject(): Promise<ControllerResponse> {
return tsRestHandler(
customProjectContract.getAvailableCountries,
async () => {
const data =
await this.countries.getAvailableCountriesToCreateACustomProject();
return { body: { data }, status: HttpStatus.OK };
},
);
}

// TODO: This should go in another controller, probably methodology controller. according to the design
@Get('/assumptions')
async getAssumptions() {
const data = await this.dataSource.getRepository(ModelAssumptions).find();
return { data };
@TsRestHandler(customProjectContract.getDefaultAssumptions)
async getAssumptions(): Promise<ControllerResponse> {
return tsRestHandler(
customProjectContract.getDefaultAssumptions,
async () => {
const data = await this.dataSource
.getRepository(ModelAssumptions)
.find();
return { body: { data }, status: HttpStatus.OK };
},
);
}

// @TsRestHandler(customProjectContract.createCustomProject)
// async create(): Promise<ControllerResponse> {
// return tsRestHandler(
// customProjectContract.createCustomProject,
// async ({ body }) => {
// // return {
// // status: 201,
// // body: null,
// // };
// },
// );
// }
}
10 changes: 10 additions & 0 deletions shared/contracts/custom-projects.contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { initContract } from "@ts-rest/core";
import { ApiResponse } from "@shared/dtos/global/api-response.dto";
import { Country } from "@shared/entities/country.entity";
import { ModelAssumptions } from "@shared/entities/model-assumptions.entity";
import { CustomProject } from "@shared/entities/custom-project.entity";
import { CreateCustomProjectSchema } from "@shared/schemas/custom-projects/create-custom-project.schema";

// TODO: This is a scaffold. We need to define types for responses, zod schemas for body and query param validation etc.

Expand All @@ -24,4 +26,12 @@ export const customProjectContract = contract.router({
},
summary: "Get default model assumptions",
},
createCustomProject: {
method: "POST",
path: "/custom-projects",
responses: {
201: contract.type<ApiResponse<CustomProject>>(),
},
body: CreateCustomProjectSchema,
},
});

0 comments on commit 657afc2

Please sign in to comment.