Skip to content
This repository has been archived by the owner on Sep 22, 2020. It is now read-only.

feat(products): create crud for products #10

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion apps/api/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import { Module } from '@nestjs/common';

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ProductsModule } from './products/products.module';
import { TypegooseModule } from 'nestjs-typegoose';

@Module({
imports: [],
imports: [
ProductsModule,
TypegooseModule.forRoot('mongodb://localhost:27017/price-search')
],
controllers: [AppController],
providers: [AppService]
})
Expand Down
8 changes: 8 additions & 0 deletions apps/api/src/app/products/product.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { prop } from '@typegoose/typegoose';
import { ApiProperty } from '@nestjs/swagger';

export class Product {
@ApiProperty()
@prop({ required: true })
name: string;
}
18 changes: 18 additions & 0 deletions apps/api/src/app/products/products.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ProductsController } from './products.controller';

describe('Products Controller', () => {
let controller: ProductsController;

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

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

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
23 changes: 23 additions & 0 deletions apps/api/src/app/products/products.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Controller } from '@nestjs/common';
import { Crud } from '@nestjsx/crud';
import { ApiTags } from '@nestjs/swagger';
import { Product } from './product.model';
import { ProductsService } from './products.service';

@Crud({
model: {
type: Product
},
params: {
id: {
type: 'uuid',
primary: true,
field: 'id'
}
}
})
@ApiTags(Product.name)
@Controller('products')
export class ProductsController {
constructor(private readonly service: ProductsService) {}
}
13 changes: 13 additions & 0 deletions apps/api/src/app/products/products.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common';
import { ProductsController } from './products.controller';
import { ProductsService } from './products.service';
import { TypegooseModule } from 'nestjs-typegoose';
import { Product } from './product.model';

@Module({
imports: [TypegooseModule.forFeature([Product])],
controllers: [ProductsController],
exports: [ProductsService],
providers: [ProductsService]
})
export class ProductsModule {}
18 changes: 18 additions & 0 deletions apps/api/src/app/products/products.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ProductsService } from './products.service';

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

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

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

it('should be defined', () => {
expect(service).toBeDefined();
});
});
14 changes: 14 additions & 0 deletions apps/api/src/app/products/products.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Injectable } from '@nestjs/common';
import { Product } from './product.model';
import { ModelType, DocumentType } from '@typegoose/typegoose/lib/types';
import { MongooseCrudService } from 'nest-crud-mongoose';
import { InjectModel } from 'nestjs-typegoose';

@Injectable()
export class ProductsService extends MongooseCrudService<
DocumentType<Product>
> {
constructor(@InjectModel(Product) public repo: ModelType<Product>) {
super(repo);
}
}
18 changes: 17 additions & 1 deletion apps/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,30 @@
import { NestFactory } from '@nestjs/core';

import { AppModule } from './app/app.module';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { mongoose, setLogLevel, LogLevels } from '@typegoose/typegoose';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const globalPrefix = 'api';
app.setGlobalPrefix(globalPrefix);

const options = new DocumentBuilder()
.setTitle('Price Search')
.setDescription('The Price Search API description')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup(`${globalPrefix}/docs`, app, document);

mongoose.set('debug', true);
setLogLevel(LogLevels.DEBUG);

const port = process.env.port || 3333;
await app.listen(port, () => {
console.log('Listening at http://localhost:' + port + '/' + globalPrefix);
const baseUrl = `http://localhost:${port}/${globalPrefix}`;
console.log(`Listening at ${baseUrl}`);
console.log(`Swagger UI listening at ${baseUrl}/docs`);
});
}

Expand Down
10 changes: 9 additions & 1 deletion apps/api/tslint.json
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
{ "extends": "../../tslint.json", "rules": {} }
{
"extends": "../../tslint.json",
"rules": {},
"linterOptions": {
"exclude": [
"!**/*"
]
}
}
10 changes: 9 additions & 1 deletion apps/price-search-e2e/tslint.json
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
{ "extends": "../../tslint.json", "rules": {} }
{
"extends": "../../tslint.json",
"rules": {},
"linterOptions": {
"exclude": [
"!**/*"
]
}
}
19 changes: 17 additions & 2 deletions apps/price-search/tslint.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
{
"extends": "../../tslint.json",
"rules": {
"directive-selector": [true, "attribute", "priceSearch", "camelCase"],
"component-selector": [true, "element", "price-search", "kebab-case"]
"directive-selector": [
true,
"attribute",
"priceSearch",
"camelCase"
],
"component-selector": [
true,
"element",
"price-search",
"kebab-case"
]
},
"linterOptions": {
"exclude": [
"!**/*"
]
}
}
10 changes: 9 additions & 1 deletion libs/api-interfaces/tslint.json
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
{ "extends": "../../tslint.json", "rules": {} }
{
"extends": "../../tslint.json",
"rules": {},
"linterOptions": {
"exclude": [
"!**/*"
]
}
}
Loading