Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(BE): 한의과 약품 수정 API 구현 #94

Merged
merged 4 commits into from
Sep 5, 2024
Merged
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
74 changes: 74 additions & 0 deletions backend/src/km-medicines/dto/update-km-medicine.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {
IsInt,
IsOptional,
IsString,
Length,
MaxLength,
Min,
} from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { CreateKMMedicineDto } from './create-km-medicine.dto';
import { PartialType } from '@nestjs/mapped-types';

export class UpdateKMMedicineDto extends PartialType(CreateKMMedicineDto) {
@ApiProperty({
description: '약품명',
example: '수정한 갈근탕연조엑스(단미엑스혼합제) S02',
})
@IsString()
@Length(2, 40)
name: string;

@ApiProperty({
description: '적응증',
example:
'감기, 몸살, 뒷목과 등이 뻣뻣하게 아픔, 머리와 얼굴이 아픈 증상, 갈증, 설사, 피부 발진, 비염, 부비동염, 급성 기관지염, 급성 후두염, 성홍열, 대장염',
required: false,
})
@IsString()
@MaxLength(1000)
indication: string;

@ApiProperty({
description: '사무실 재고',
example: 5,
required: false,
})
@IsInt()
@Min(0)
stationQuantity: number;

@ApiProperty({
description: '서울역 재고',
example: 20,
})
@IsInt()
@Min(0)
officeQuantity: number;

@ApiProperty({
description: '포장단위',
example: 10,
})
@IsInt()
@Min(0)
packaging: number;

@ApiProperty({
description: '총량',
example: 200,
})
@IsInt()
@Min(0)
totalAmount: number;

@ApiProperty({
description:
"사진 - <em>image 속성 없으면 사진 변경 안 함, image='' 빈 값으로 보내면 사진 삭제</em>",
type: 'string',
format: 'binary',
required: false,
})
@IsOptional()
image?: any;
}
23 changes: 23 additions & 0 deletions backend/src/km-medicines/km-medicines.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
HttpStatus,
Param,
ParseIntPipe,
Patch,
Post,
Query,
UploadedFile,
Expand All @@ -21,6 +22,7 @@ import {
} from '@nestjs/swagger';
import { CreateKMMedicineDto } from './dto/create-km-medicine.dto';
import { PaginateKMMedicineDto } from './dto/paginate-km-medicine.dto';
import { UpdateKMMedicineDto } from './dto/update-km-medicine.dto';

@ApiTags('한의과')
@Controller('km/medicines')
Expand Down Expand Up @@ -60,6 +62,27 @@ export class KmMedicinesController {
return await this.medicinesService.createMedicine(createMedicineDto, image);
}

@Patch(':medicineId')
@UseInterceptors(FileInterceptor('image'))
@ApiConsumes('multipart/form-data')
@ApiOperation({
summary: '약품 수정',
})
@ApiResponse({
status: HttpStatus.NOT_FOUND,
})
async patchMedicine(
@Param('medicineId', ParseIntPipe) medicineId: number,
@Body() updateMedicineDto: UpdateKMMedicineDto,
@UploadedFile() image: Express.Multer.File,
) {
return this.medicinesService.updateMedicine(
medicineId,
updateMedicineDto,
image,
);
}

@Delete(':medicineId')
@ApiOperation({
summary: '약품 삭제',
Expand Down
37 changes: 37 additions & 0 deletions backend/src/km-medicines/km-medicines.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '@aws-sdk/client-s3';
import { PaginateKMMedicineDto } from './dto/paginate-km-medicine.dto';
import { CommonService } from 'src/common/common.service';
import { UpdateKMMedicineDto } from './dto/update-km-medicine.dto';

@Injectable()
export class KmMedicinesService {
Expand Down Expand Up @@ -61,6 +62,42 @@ export class KmMedicinesService {
});
}

async updateMedicine(
medicineId: number,
medicineDto: UpdateKMMedicineDto,
image: Express.Multer.File,
) {
const medicine = await this.medicinesRepository.findOne({
where: { id: medicineId },
});

if (!medicine) {
throw new NotFoundException();
}

// 사진 변경 (이미 있으면 삭제 후 업로드)
if (!medicineDto.hasOwnProperty('image') && image) {
if (medicine.image) {
await this.deleteUploadedImage(medicine.image);
}
const imagePath = await this.uploadImage(image, medicineDto.name);
medicineDto.image = imagePath;
}

// 사진 삭제
if (medicineDto.hasOwnProperty('image') && !image && medicine.image) {
await this.deleteUploadedImage(medicine.image);
medicineDto.image = null;
}

const newMedicine = await this.medicinesRepository.preload({
id: medicineId,
...medicineDto,
});

return await this.medicinesRepository.save(newMedicine);
}

async deleteMedicine(medicineId: number) {
const medicine = await this.medicinesRepository.findOne({
where: { id: medicineId },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
BadRequestException,
Body,
Controller,
Delete,
Expand All @@ -15,20 +16,22 @@ import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { CreateMMedicineCategoryDto } from './dto/create-m-medicine-category.dto';
import { UpdateMMedicineSubCategoryDto } from './dto/update-m-medicine-sub-category.dto';
import { UpdateMMedicineMainCategoryDto } from './dto/update-m-medicine-main-category.dto';
import { MMedicinesService } from 'src/m-medicines/m-medicines.service';

@ApiTags('의과')
@Controller('m/medicine-categories')
export class MMedicineCategoriesController {
constructor(
private readonly mMedicineCategoriesService: MMedicineCategoriesService,
private readonly medicineCategoriesService: MMedicineCategoriesService,
private readonly medicinesService: MMedicinesService,
) {}

@Get()
@ApiOperation({
summary: '약품 분류 조회',
})
async getMMedicineCategories() {
return this.mMedicineCategoriesService.getCategories();
async getMedicineCategories() {
return this.medicineCategoriesService.getCategories();
}

@Post()
Expand All @@ -39,11 +42,11 @@ export class MMedicineCategoriesController {
status: HttpStatus.BAD_REQUEST,
description: '이미 존재하는 분류입니다.',
})
async postMMedicineCategory(
@Body() createMMedicineCategoryDto: CreateMMedicineCategoryDto,
async postMedicineCategory(
@Body() createMedicineCategoryDto: CreateMMedicineCategoryDto,
) {
return this.mMedicineCategoriesService.createCategory(
createMMedicineCategoryDto,
return this.medicineCategoriesService.createCategory(
createMedicineCategoryDto,
);
}

Expand All @@ -58,13 +61,13 @@ export class MMedicineCategoriesController {
@ApiResponse({
status: HttpStatus.NOT_FOUND,
})
async patchMMedicineMainCategory(
async patchMedicineMainCategory(
@Query('category') category: string,
@Body() updateMMedicineMainCategoryDto: UpdateMMedicineMainCategoryDto,
@Body() updateMedicineMainCategoryDto: UpdateMMedicineMainCategoryDto,
) {
return this.mMedicineCategoriesService.updateMainCategory(
return this.medicineCategoriesService.updateMainCategory(
category,
updateMMedicineMainCategoryDto,
updateMedicineMainCategoryDto,
);
}

Expand All @@ -79,8 +82,8 @@ export class MMedicineCategoriesController {
@ApiResponse({
status: HttpStatus.NOT_FOUND,
})
async deleteMMedicineMainCategory(@Query('category') category: string) {
return this.mMedicineCategoriesService.deleteMainCategory(category);
async deleteMedicineMainCategory(@Query('category') category: string) {
return this.medicineCategoriesService.deleteMainCategory(category);
}

@Patch('sub-category/:categoryId')
Expand All @@ -90,13 +93,13 @@ export class MMedicineCategoriesController {
@ApiResponse({
status: HttpStatus.NOT_FOUND,
})
async patchMMedicineSubCategory(
async patchMedicineSubCategory(
@Param('categoryId', ParseIntPipe) categoryId: number,
@Body() updateMMedicineSubCategoryDto: UpdateMMedicineSubCategoryDto,
@Body() updateMedicineSubCategoryDto: UpdateMMedicineSubCategoryDto,
) {
return this.mMedicineCategoriesService.updateSubCategory(
return this.medicineCategoriesService.updateSubCategory(
categoryId,
updateMMedicineSubCategoryDto,
updateMedicineSubCategoryDto,
);
}

Expand All @@ -111,9 +114,16 @@ export class MMedicineCategoriesController {
@ApiResponse({
status: HttpStatus.NOT_FOUND,
})
async deleteMMedicineSubCategory(
async deleteMedicineSubCategory(
@Param('categoryId', ParseIntPipe) categoryId: number,
) {
return this.mMedicineCategoriesService.deleteSubCategory(categoryId);
const idDeleted =
await this.medicinesService.checkDeletedMedicineByCategoryId(categoryId);

if (!idDeleted) {
throw new BadRequestException('삭제되지 않은 약품이 존재합니다.');
}

return this.medicineCategoriesService.deleteSubCategory(categoryId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import { MMedicineCategoriesController } from './m-medicine-categories.controlle
import { M_Medicine_Categories } from './entity/m_medicine_categories.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import { M_Medicines } from 'src/m-medicines/entity/m-medicines.entity';
import { MMedicinesService } from 'src/m-medicines/m-medicines.service';
import { CommonModule } from 'src/common/common.module';

@Module({
imports: [TypeOrmModule.forFeature([M_Medicine_Categories, M_Medicines])],
imports: [
TypeOrmModule.forFeature([M_Medicine_Categories, M_Medicines]),
CommonModule,
],
controllers: [MMedicineCategoriesController],
providers: [MMedicineCategoriesService],
providers: [MMedicineCategoriesService, MMedicinesService],
})
export class MMedicineCategoriesModule {}
Loading
Loading