This repository has been archived by the owner on Sep 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(products): create crud for products
- NestJS CRUD (https://github.com/nestjsx/crud) to generate the RESTful CRUD for the object. - Swagger (https://docs.nestjs.com/recipes/swagger) to generate API documentation, listening at `/api/docs`. - Typegroose (https://github.com/typegoose/typegoose) to map Typescript objects to MongoDB/Mongoose. - https://www.npmjs.com/package/nest-crud-mongoose Took example from https://github.com/9173860/nest-crud-mongoose-demo.
- Loading branch information
Showing
10 changed files
with
446 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
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 | ||
} | ||
}) | ||
@ApiTags(Product.name) | ||
@Controller('products') | ||
export class ProductsController { | ||
constructor(private readonly service: ProductsService) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.