Compare commits
2 Commits
714beb92c3
...
4b193476a9
| Author | SHA1 | Date | |
|---|---|---|---|
| 4b193476a9 | |||
| 0bd059457d |
@@ -19,6 +19,7 @@ import 'winston-mongodb';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { OrderModule } from './order/order.module';
|
||||
import { IngredientUsageModule } from './ingredient-usage/ingredient-usage.module';
|
||||
import { CategoryModule } from './category/category.module';
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({ isGlobal: true }),
|
||||
@@ -37,6 +38,7 @@ import { IngredientUsageModule } from './ingredient-usage/ingredient-usage.modul
|
||||
),
|
||||
OrderModule,
|
||||
IngredientUsageModule,
|
||||
CategoryModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
|
||||
45
src/category/category.controller.ts
Normal file
45
src/category/category.controller.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
} from '@nestjs/common';
|
||||
import { CategoryService } from './category.service';
|
||||
import { CreateCategoryDto } from './dto/create-category.dto';
|
||||
import { UpdateCategoryDto } from './dto/update-category.dto';
|
||||
|
||||
@Controller('category')
|
||||
export class CategoryController {
|
||||
constructor(private readonly categoryService: CategoryService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() createCategoryDto: CreateCategoryDto) {
|
||||
return this.categoryService.create(createCategoryDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.categoryService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.categoryService.findOne(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(
|
||||
@Param('id') id: string,
|
||||
@Body() updateCategoryDto: UpdateCategoryDto,
|
||||
) {
|
||||
return this.categoryService.update(id, updateCategoryDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.categoryService.remove(id);
|
||||
}
|
||||
}
|
||||
13
src/category/category.module.ts
Normal file
13
src/category/category.module.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { CategoryService } from './category.service';
|
||||
import { CategoryController } from './category.controller';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Category } from './entities/category.entity';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Category])],
|
||||
controllers: [CategoryController],
|
||||
providers: [CategoryService],
|
||||
exports: [CategoryService],
|
||||
})
|
||||
export class CategoryModule {}
|
||||
51
src/category/category.service.ts
Normal file
51
src/category/category.service.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { CreateCategoryDto } from './dto/create-category.dto';
|
||||
import { UpdateCategoryDto } from './dto/update-category.dto';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Category } from './entities/category.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class CategoryService {
|
||||
constructor(
|
||||
@InjectRepository(Category)
|
||||
private readonly categoryRepository: Repository<Category>,
|
||||
) {}
|
||||
async create(createCategoryDto: CreateCategoryDto) {
|
||||
return await this.categoryRepository.save(createCategoryDto);
|
||||
}
|
||||
|
||||
async findAll() {
|
||||
return await this.categoryRepository.find({
|
||||
order: {
|
||||
created_at: 'DESC',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
return await this.categoryRepository.findOne({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async update(id: string, updateCategoryDto: UpdateCategoryDto) {
|
||||
const entity = await this.findOne(id);
|
||||
if (!entity) {
|
||||
throw new BadRequestException('Category not found.');
|
||||
}
|
||||
|
||||
const updated = this.categoryRepository.merge(entity, updateCategoryDto);
|
||||
await this.categoryRepository.save(updated);
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
const entity = await this.findOne(id);
|
||||
if (!entity) {
|
||||
throw new BadRequestException('Category not found.');
|
||||
}
|
||||
await this.categoryRepository.remove(entity);
|
||||
}
|
||||
}
|
||||
6
src/category/dto/create-category.dto.ts
Normal file
6
src/category/dto/create-category.dto.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { IsString } from 'class-validator';
|
||||
|
||||
export class CreateCategoryDto {
|
||||
@IsString()
|
||||
name: string;
|
||||
}
|
||||
4
src/category/dto/update-category.dto.ts
Normal file
4
src/category/dto/update-category.dto.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateCategoryDto } from './create-category.dto';
|
||||
|
||||
export class UpdateCategoryDto extends PartialType(CreateCategoryDto) {}
|
||||
8
src/category/entities/category.entity.ts
Normal file
8
src/category/entities/category.entity.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { BaseEntity } from 'src/common/base_entity';
|
||||
import { Column, Entity } from 'typeorm';
|
||||
|
||||
@Entity({ name: 'categories' })
|
||||
export class Category extends BaseEntity {
|
||||
@Column({ unique: true })
|
||||
name: string;
|
||||
}
|
||||
Reference in New Issue
Block a user