Skip to content

Commit

Permalink
[Implement][Category] View categories and create category API
Browse files Browse the repository at this point in the history
  • Loading branch information
nghiavohuynhdai authored and hideonbush106 committed Jan 28, 2024
1 parent 4ee1ef6 commit f638744
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/category/category.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { CategoryService } from '@category/services/category.service'
import { CategoryRepository } from '@category/repositories/category.repository'
import { MongooseModule } from '@nestjs/mongoose'
import { Category, CategorySchema } from '@category/schemas/category.schema'
import { CategoryCustomerController } from '@category/controllers/customer.controller'

@Module({
imports: [MongooseModule.forFeature([{ name: Category.name, schema: CategorySchema }])],
controllers: [CategoryProviderController],
controllers: [CategoryProviderController, CategoryCustomerController],
providers: [CategoryService, CategoryRepository],
exports: [CategoryRepository]
})
Expand Down
20 changes: 20 additions & 0 deletions src/category/controllers/customer.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Controller, Get } from '@nestjs/common'
import { ApiOkResponse, ApiQuery, ApiTags } from '@nestjs/swagger'
import { PaginateResponse } from '@common/contracts/openapi-builder'
import { PaginationQuery } from '@src/common/contracts/dto'
import { CategoryService } from '@category/services/category.service'
import { Pagination, PaginationParams } from '@src/common/decorators/pagination.decorator'
import { PublicCategory } from '@category/dto/category.dto'

@ApiTags('Category - Customer')
@Controller('customer')
export class CategoryCustomerController {
constructor(private readonly categoryService: CategoryService) {}

@Get()
@ApiOkResponse({ type: PaginateResponse(PublicCategory) })
@ApiQuery({ type: PaginationQuery })
async getAllCategories(@Pagination() paginationParams: PaginationParams) {
return await this.categoryService.getAllPublicCategories(paginationParams)
}
}
11 changes: 9 additions & 2 deletions src/category/controllers/provider.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Controller, Get, UseGuards } from '@nestjs/common'
import { ApiBearerAuth, ApiOkResponse, ApiQuery, ApiTags } from '@nestjs/swagger'
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common'
import { ApiBearerAuth, ApiCreatedResponse, ApiOkResponse, ApiQuery, ApiTags } from '@nestjs/swagger'
import { JwtAuthGuard } from '@auth/guards/jwt-auth.guard'
import { RolesGuard } from '@auth/guards/roles.guard'
import { UserRole } from '@common/contracts/constant'
Expand All @@ -9,6 +9,7 @@ import { PaginateResponse } from '@common/contracts/openapi-builder'
import { PaginationQuery } from '@src/common/contracts/dto'
import { CategoryService } from '@category/services/category.service'
import { Pagination, PaginationParams } from '@src/common/decorators/pagination.decorator'
import { CreateCategoryDto } from '@category/dto/category.dto'

@ApiTags('Category - Provider')
@ApiBearerAuth()
Expand All @@ -24,4 +25,10 @@ export class CategoryProviderController {
async getAllCategories(@Pagination() paginationParams: PaginationParams) {
return await this.categoryService.getAllCategories(paginationParams)
}

@Post()
@ApiCreatedResponse({ type: Category })
async createCategory(@Body() createCategoryDto: CreateCategoryDto) {
return await this.categoryService.createCategory(createCategoryDto)
}
}
32 changes: 32 additions & 0 deletions src/category/dto/category.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ApiProperty } from '@nestjs/swagger'
import { IsNotEmpty, IsUrl, MaxLength } from 'class-validator'

export class PublicCategory {
@ApiProperty()
_id: string

@ApiProperty()
name: string

@ApiProperty()
description: string

@ApiProperty()
image: string
}

export class CreateCategoryDto {
@ApiProperty()
@IsNotEmpty()
@MaxLength(30)
name: string

@ApiProperty()
@MaxLength(100)
description: string

@ApiProperty()
@IsNotEmpty()
@IsUrl()
image: string
}
28 changes: 27 additions & 1 deletion src/category/services/category.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Injectable } from '@nestjs/common'
import { BadRequestException, Injectable } from '@nestjs/common'
import { CategoryRepository } from '@category/repositories/category.repository'
import { PaginationParams } from '@common/decorators/pagination.decorator'
import { Status } from '@common/contracts/constant'
import { CreateCategoryDto } from '@category/dto/category.dto'
import { Errors } from '@common/contracts/error'

@Injectable()
export class CategoryService {
Expand All @@ -17,4 +19,28 @@ export class CategoryService {
paginationParams
)
}

async getAllPublicCategories(paginationParams: PaginationParams) {
return await this.categoryRepository.paginate(
{
status: Status.ACTIVE
},
{ ...paginationParams, projection: { status: 0 } }
)
}

async createCategory(createCategoryDto: CreateCategoryDto) {
let category = await this.categoryRepository.findOne({
conditions: {
name: createCategoryDto.name,
status: { $ne: Status.DELETED }
}
})

if (category) {
throw new BadRequestException(Errors.CATEGORY_ALREADY_EXIST)
}

return await this.categoryRepository.create(createCategoryDto)
}
}
5 changes: 5 additions & 0 deletions src/common/contracts/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@ export const Errors: Record<string, ErrorResponse> = {
error: 'NOT_ENOUGH_QUANTITY_IN_STOCK',
message: 'Không đủ số lượng trong kho.',
httpStatus: HttpStatus.BAD_REQUEST
},
CATEGORY_ALREADY_EXIST: {
error: 'CATEGORY_ALREADY_EXIST',
message: 'Danh mục đã tồn tại',
httpStatus: HttpStatus.BAD_REQUEST
}
}
2 changes: 1 addition & 1 deletion src/product/schemas/product.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class Product {
})
variants: Variant[]

@ApiProperty({ type: Types.ObjectId, isArray: true })
@ApiProperty({ type: String, isArray: true })
@Prop({
type: [{ type: Types.ObjectId, ref: 'Category' }]
})
Expand Down

0 comments on commit f638744

Please sign in to comment.