Skip to content

Commit

Permalink
list all and by id projects
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Oct 27, 2024
1 parent 67690e3 commit 98d6296
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
2 changes: 2 additions & 0 deletions api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { DataSource } from 'typeorm';
import * as fs from 'fs';
import * as path from 'path';
import { CountriesModule } from '@api/modules/countries/countries.module';
import { ProjectsModule } from '@api/modules/projects/projects.module';

@Module({
imports: [
Expand All @@ -32,6 +33,7 @@ import { CountriesModule } from '@api/modules/countries/countries.module';
ImportModule,
CountriesModule,
UsersModule,
ProjectsModule,
],
controllers: [AppController],
providers: [
Expand Down
30 changes: 27 additions & 3 deletions api/src/modules/projects/projects.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
import { Controller } from '@nestjs/common';
import { Controller, HttpStatus } from '@nestjs/common';
import { tsRestHandler, TsRestHandler } from '@ts-rest/nest';
import { ControllerResponse } from '@api/types/controller-response.type';
import { projectsContract } from '@shared/contracts/projects.contract';
import { ProjectsService } from '@api/modules/projects/projects.service';

@Controller('projects')
export class ProjectsController {}
@Controller()
export class ProjectsController {
constructor(private readonly projectsService: ProjectsService) {}

@TsRestHandler(projectsContract.getProjects)
async getProjects(): ControllerResponse {
return tsRestHandler(projectsContract.getProjects, async ({ query }) => {
const data = await this.projectsService.findAllPaginated(query);
return { body: data, status: HttpStatus.OK };
});
}
@TsRestHandler(projectsContract.getProject)
async getProject(): ControllerResponse {
return tsRestHandler(
projectsContract.getProject,
async ({ params: { id }, query }) => {
const data = await this.projectsService.getById(id, query);
return { body: { data }, status: HttpStatus.OK };
},
);
}
}
18 changes: 17 additions & 1 deletion api/src/modules/projects/projects.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
import { Injectable } from '@nestjs/common';
import { AppBaseService } from '@api/utils/app-base.service';
import { Project } from '@shared/entities/users/projects.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

@Injectable()
export class ProjectsService {}
export class ProjectsService extends AppBaseService<
Project,
unknown,
unknown,
unknown
> {
constructor(
@InjectRepository(Project)
private readonly projectRepository: Repository<Project>,
) {
super(projectRepository, 'project', 'projects');
}
}
2 changes: 2 additions & 0 deletions shared/contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { authContract } from "@shared/contracts/auth.contract";
import { usersContract } from "@shared/contracts/users.contract";
import { JSONAPIError } from "@shared/dtos/json-api.error";
import { mapContract } from "@shared/contracts/map.contract";
import { projectsContract } from "@shared/contracts/projects.contract";

const contract = initContract();

Expand All @@ -13,6 +14,7 @@ export const router = contract.router(
admin: adminContract,
user: usersContract,
map: mapContract,
projects: projectsContract,
},
{
commonResponses: {
Expand Down
31 changes: 31 additions & 0 deletions shared/contracts/projects.contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { initContract } from "@ts-rest/core";
import { generateEntityQuerySchema } from "@shared/schemas/query-param.schema";
import { z } from "zod";
import {
ApiPaginationResponse,
ApiResponse,
} from "@shared/dtos/global/api-response.dto";
import { Project } from "@shared/entities/users/projects.entity";

const contract = initContract();
export const projectsContract = contract.router({
getProjects: {
method: "GET",
path: "/projects",
responses: {
200: contract.type<ApiPaginationResponse<Project>>(),
},
query: generateEntityQuerySchema(Project),
},
getProject: {
method: "GET",
path: "/projects/:id",
pathParams: z.object({
id: z.coerce.string().uuid(),
}),
responses: {
200: contract.type<ApiResponse<Project>>(),
},
query: generateEntityQuerySchema(Project),
},
});

0 comments on commit 98d6296

Please sign in to comment.