Skip to content

Commit

Permalink
setup swagger, seeder and GET endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoleamber committed Aug 30, 2023
1 parent 942f478 commit 0a25300
Show file tree
Hide file tree
Showing 22 changed files with 692 additions and 38 deletions.
25 changes: 20 additions & 5 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.1.10",
"@prisma/client": "^5.1.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"jest-junit": "^16.0.0",
"meilisearch": "^0.34.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
},
Expand All @@ -36,7 +39,7 @@
"@nestjs/testing": "^10.0.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@types/node": "^20.5.7",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^5.59.11",
"@typescript-eslint/parser": "^5.59.11",
Expand All @@ -52,7 +55,7 @@
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
"typescript": "^5.2.2"
},
"jest": {
"moduleFileExtensions": [
Expand All @@ -70,14 +73,26 @@
"**/*.{js,ts}"
],
"coverageDirectory": "../test/coverage",
"coverageReporters": ["clover", "json", "lcov", "text"],
"coverageReporters": [
"clover",
"json",
"lcov",
"text"
],
"testEnvironment": "node",
"reporters": [
"default",
["jest-junit", {"outputDirectory": "./test/result", "outputName": "report.xml"}]
[
"jest-junit",
{
"outputDirectory": "./test/result",
"outputName": "report.xml"
}
]
]
},
"prisma": {
"schema": "./src/models/schema.prisma"
"schema": "./src/models/schema.prisma",
"seed": "ts-node src/models/seed.ts"
}
}
5 changes: 5 additions & 0 deletions server/src/api/jobs/job.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PartialType } from '@nestjs/mapped-types';

export class CreateJobDto {}

export class UpdateJobDto extends PartialType(CreateJobDto) {}
37 changes: 37 additions & 0 deletions server/src/api/jobs/job.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ApiProperty } from '@nestjs/swagger';
import { Job, PaymentMethod, Schedule, Tag } from '@prisma/client';

export class JobEntity implements Job {
@ApiProperty()
id: number;

@ApiProperty()
title: string;

@ApiProperty()
type: string;

@ApiProperty({ nullable: true })
remarks: string;

@ApiProperty()
personInChargeId: number;

@ApiProperty()
customerId: number;

@ApiProperty()
paymentMethod: PaymentMethod;

@ApiProperty({ nullable: true })
tags: Tag[]

@ApiProperty({ nullable: true })
schedules: Schedule[]

@ApiProperty()
createdAt: Date;

@ApiProperty()
updatedAt: Date;
}
20 changes: 20 additions & 0 deletions server/src/api/jobs/jobs.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { JobsController } from './jobs.controller';
import { JobsService } from './jobs.service';

describe('JobsController', () => {
let controller: JobsController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [JobsController],
providers: [JobsService],
}).compile();

controller = module.get<JobsController>(JobsController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
20 changes: 20 additions & 0 deletions server/src/api/jobs/jobs.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Controller, Get, Query } from '@nestjs/common';
import { ApiQuery, ApiTags } from '@nestjs/swagger';
import { JobsService } from './jobs.service';

@ApiTags('jobs')
@Controller('jobs')
export class JobsController {
constructor(private readonly jobsService: JobsService) {}

@ApiQuery({
name: 'search',
required: false,
type: String,
description: "Optional search keyword"
})
@Get()
async findAll(@Query('search') search: string) {
return await this.jobsService.findAll(search);
}
}
11 changes: 11 additions & 0 deletions server/src/api/jobs/jobs.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { PrismaService } from 'src/database/connection.service';
import { SearchService } from 'src/services/externalService/search.service';
import { JobsController } from './jobs.controller';
import { JobsService } from './jobs.service';

@Module({
controllers: [JobsController],
providers: [JobsService, PrismaService, SearchService],
})
export class JobsModule {}
18 changes: 18 additions & 0 deletions server/src/api/jobs/jobs.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { JobsService } from './jobs.service';

describe('JobsService', () => {
let service: JobsService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [JobsService],
}).compile();

service = module.get<JobsService>(JobsService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
30 changes: 30 additions & 0 deletions server/src/api/jobs/jobs.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Injectable } from '@nestjs/common';
import { Job } from '@prisma/client';
import { PrismaService } from 'src/database/connection.service';
import { SearchService } from 'src/services/externalService/search.service';

@Injectable()
export class JobsService {
constructor(
private prisma: PrismaService,
private searchService: SearchService
){}

async findAll(search?: string): Promise<Job[] | null> {
let jobs: Job[] = [];

jobs = await this.prisma.job.findMany();

if (search) {
await this.searchService.addDocuments(jobs);
const results = await this.searchService.search(search);

console.log(results);

const jobIds = results.hits.map(hit => parseInt(hit.id));
jobs = jobs.filter(job => jobIds.includes(job.id));
}

return jobs;
}
}
12 changes: 6 additions & 6 deletions server/src/api/sample/sample.entities.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SampleUser } from '@prisma/client';
// import { SampleUser } from '@prisma/client';

export class SampleUserEntity implements SampleUser {
id: number;
name: string;
email: string;
}
// export class SampleUserEntity implements SampleUser {
// id: number;
// name: string;
// email: string;
// }
11 changes: 7 additions & 4 deletions server/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Module } from '@nestjs/common';
import { JobsController } from './api/jobs/jobs.controller';
import { JobsModule } from './api/jobs/jobs.module';
import { JobsService } from './api/jobs/jobs.service';
import { DatabaseModule } from './database/database.module';
import { SampleController } from './api/sample/sample.controller';
import { SearchService } from './services/externalService/search.service';

@Module({
imports: [DatabaseModule],
controllers: [SampleController],
providers: [],
imports: [DatabaseModule, JobsModule],
controllers: [JobsController],
providers: [JobsService, SearchService],
})
export class AppModule {}
11 changes: 11 additions & 0 deletions server/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);

const config = new DocumentBuilder()
.setTitle('Sim-JMS')
.setDescription('This is the API documentation for sim-jms')
.setVersion('1.0')
.build();

const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);

await app.listen(4000);
}
bootstrap();
82 changes: 82 additions & 0 deletions server/src/models/data/customers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
export const customers = [
{
id: 1,
firstName: "John",
lastName: "Doe",
email: "johndoe@gmail.com",
contact: "+639876543210",
address: "8741 VonRueden Highway, West Virginia",
},
{
id: 2,
firstName: "Jane",
lastName: "Smith",
email: "janesmith@gmail.com",
contact: "+630123546788",
address: "King Apt 906, Tennessee",
},
{
id: 3,
firstName: "Michael",
lastName: "Smith",
email: "michaelsmith@gmail.com",
contact: "+63444543210",
address: "986 Senger Drive, Lake Aliza",
},
{
id: 4,
firstName: "Ana",
lastName: "Rogers",
email: "anarogers@gmail.com",
contact: "+639277543210",
address: "580 Stroman Heights, Davismouth",
},
{
id: 5,
firstName: "Kelly",
lastName: "Nier",
email: "kellynier@gmail.com",
contact: "+639123746890",
address: "55934 Veronica Knoll, South Dakota",
},
{
id: 6,
firstName: "Curtis",
lastName: "Hegmann",
email: "curtishegmann@gmail.com",
contact: "+639277543321",
address: "123 Murray Village, Nebraska",
},
{
id: 7,
firstName: "Hassan",
lastName: "Connel",
email: "hassanconnel@gmail.com",
contact: "+639274343321",
address: "389 Larkin Circle, Abbotport",
},
{
id: 8,
firstName: "Ray",
lastName: "Summers",
email: "raysummers@gmail.com",
contact: "+639200343321",
address: "5327 Constance Harbors, West Marina",
},
{
id: 9,
firstName: "Alice",
lastName: "Hills",
email: "alicehills@gmail.com",
contact: "+63231567891",
address: "544 Bertrand Row, Kansas",
},
{
id: 10,
firstName: "Sheila",
lastName: "Comb",
email: "sheilacomb@gmail.com",
contact: "+63231567800",
address: "2029 Kuhlman Expressway, West Virginia",
},
]
Loading

0 comments on commit 0a25300

Please sign in to comment.