Skip to content

Commit 4dd4394

Browse files
authored
�feat(BE): 의과 약품 대분류 수정/삭제 API 구현 (#37)
* feat: 의과 약품 대분류 수정 DTO 추가 * feat: 의과 약품 대분류 수정 API 구현 * feat: 의과 약품 대분류 삭제 API 구현 * docs: Swagger m/medicine-categories ApiResponse 추가
1 parent 647fb46 commit 4dd4394

File tree

3 files changed

+131
-4
lines changed

3 files changed

+131
-4
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ApiProperty, PickType } from '@nestjs/swagger';
2+
import { M_Medicine_Categories } from '../entity/m_medicine_categories.entity';
3+
import { IsString, Length } from 'class-validator';
4+
import { stringValidationMessage } from 'src/common/validation-message/string-validation.message';
5+
import { lengthValidationMessage } from 'src/common/validation-message/length-validation.message';
6+
7+
export class UpdateMMedicineMainCategoryDto extends PickType(
8+
M_Medicine_Categories,
9+
['mainCategory'],
10+
) {
11+
@ApiProperty({
12+
description: '대분류',
13+
example: '해열, 진통, 소염제',
14+
})
15+
@IsString({ message: stringValidationMessage })
16+
@Length(2, 30, { message: lengthValidationMessage })
17+
mainCategory: string;
18+
}

backend/src/m-medicine-categories/m-medicine-categories.controller.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import {
88
ParseIntPipe,
99
Patch,
1010
Post,
11+
Query,
1112
} from '@nestjs/common';
1213
import { MMedicineCategoriesService } from './m-medicine-categories.service';
1314
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
1415
import { CreateMMedicineCategoryDto } from './dto/create-m-medicine-category.dto';
1516
import { UpdateMMedicineSubCategoryDto } from './dto/update-m-medicine-sub-category.dto';
17+
import { UpdateMMedicineMainCategoryDto } from './dto/update-m-medicine-main-category.dto';
1618

1719
@ApiTags('의과')
1820
@Controller('m/medicine-categories')
@@ -45,10 +47,49 @@ export class MMedicineCategoriesController {
4547
);
4648
}
4749

50+
@Patch('main-category')
51+
@ApiOperation({
52+
summary: '약품 대분류 수정',
53+
})
54+
@ApiResponse({
55+
status: HttpStatus.BAD_REQUEST,
56+
description: '이미 존재하는 대분류입니다.',
57+
})
58+
@ApiResponse({
59+
status: HttpStatus.NOT_FOUND,
60+
})
61+
async patchMMedicineMainCategory(
62+
@Query('category') category: string,
63+
@Body() updateMMedicineMainCategoryDto: UpdateMMedicineMainCategoryDto,
64+
) {
65+
return this.mMedicineCategoriesService.updateMainCategory(
66+
category,
67+
updateMMedicineMainCategoryDto,
68+
);
69+
}
70+
71+
@Delete('main-category')
72+
@ApiOperation({
73+
summary: '약품 대분류 삭제',
74+
})
75+
@ApiResponse({
76+
status: HttpStatus.BAD_REQUEST,
77+
description: '삭제되지 않은 소분류가 존재합니다.',
78+
})
79+
@ApiResponse({
80+
status: HttpStatus.NOT_FOUND,
81+
})
82+
async deleteMMedicineMainCategory(@Query('category') category: string) {
83+
return this.mMedicineCategoriesService.deleteMainCategory(category);
84+
}
85+
4886
@Patch('sub-category/:categoryId')
4987
@ApiOperation({
5088
summary: '약품 소분류 수정',
5189
})
90+
@ApiResponse({
91+
status: HttpStatus.NOT_FOUND,
92+
})
5293
async patchMMedicineSubCategory(
5394
@Param('categoryId', ParseIntPipe) categoryId: number,
5495
@Body() updateMMedicineSubCategoryDto: UpdateMMedicineSubCategoryDto,
@@ -63,6 +104,13 @@ export class MMedicineCategoriesController {
63104
@ApiOperation({
64105
summary: '약품 소분류 삭제',
65106
})
107+
@ApiResponse({
108+
status: HttpStatus.BAD_REQUEST,
109+
description: '삭제되지 않은 약품이 존재합니다.',
110+
})
111+
@ApiResponse({
112+
status: HttpStatus.NOT_FOUND,
113+
})
66114
async deleteMMedicineSubCategory(
67115
@Param('categoryId', ParseIntPipe) categoryId: number,
68116
) {

backend/src/m-medicine-categories/m-medicine-categories.service.ts

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Repository } from 'typeorm';
99
import { CreateMMedicineCategoryDto } from './dto/create-m-medicine-category.dto';
1010
import { UpdateMMedicineSubCategoryDto } from './dto/update-m-medicine-sub-category.dto';
1111
import { MMedicinesService } from 'src/m-medicines/m-medicines.service';
12+
import { UpdateMMedicineMainCategoryDto } from './dto/update-m-medicine-main-category.dto';
1213

1314
@Injectable()
1415
export class MMedicineCategoriesService {
@@ -19,9 +20,7 @@ export class MMedicineCategoriesService {
1920
) {}
2021

2122
async getCategories() {
22-
const categories = await this.mMedicineCategoriesRepository.find({
23-
withDeleted: false,
24-
});
23+
const categories = await this.mMedicineCategoriesRepository.find();
2524

2625
return categories.reduce(
2726
(acc, cur) =>
@@ -66,10 +65,72 @@ export class MMedicineCategoriesService {
6665
mainCategory,
6766
subCategory,
6867
},
69-
withDeleted: false,
7068
});
7169
}
7270

71+
async updateMainCategory(
72+
category: string,
73+
mainCategoryDto: UpdateMMedicineMainCategoryDto,
74+
) {
75+
const categories = await this.mMedicineCategoriesRepository.find({
76+
where: {
77+
mainCategory: category,
78+
},
79+
});
80+
81+
if (categories.length === 0) {
82+
throw new NotFoundException();
83+
}
84+
85+
const existingCategories = await this.mMedicineCategoriesRepository.find({
86+
where: {
87+
mainCategory: mainCategoryDto.mainCategory,
88+
},
89+
});
90+
91+
if (existingCategories.length > 0) {
92+
throw new BadRequestException('이미 존재하는 대분류입니다.');
93+
}
94+
95+
for (const category of categories) {
96+
await this.mMedicineCategoriesRepository.save({
97+
...category,
98+
mainCategory: mainCategoryDto.mainCategory,
99+
});
100+
}
101+
102+
return mainCategoryDto.mainCategory;
103+
}
104+
105+
async deleteMainCategory(category: string) {
106+
const categories = await this.mMedicineCategoriesRepository.find({
107+
where: {
108+
mainCategory: category,
109+
},
110+
withDeleted: true,
111+
});
112+
113+
if (categories.length === 0) {
114+
throw new NotFoundException();
115+
}
116+
117+
const existingCategories = await this.mMedicineCategoriesRepository.find({
118+
where: {
119+
mainCategory: category,
120+
},
121+
});
122+
123+
if (existingCategories.length > 0) {
124+
throw new BadRequestException('삭제되지 않은 소분류가 존재합니다.');
125+
}
126+
127+
await this.mMedicineCategoriesRepository.delete({
128+
mainCategory: category,
129+
});
130+
131+
return category;
132+
}
133+
73134
async updateSubCategory(
74135
categoryId: number,
75136
subCategoryDto: UpdateMMedicineSubCategoryDto,

0 commit comments

Comments
 (0)