β¬οΈ Main Note
https://docs.google.com/document/d/1IQfJPSjE7EDob5GXWJAtMe1uWW8ZpABc3r7-ieIaLDo/edit
async checkSoldout({ productId }) {
// try {
// const product = await this.productRepository.findOne({
// where: { id: productId },
// });
// console.log('logic check');
// } catch (error) {
// throw error.message;
// } finally {
// // νΉμ λ‘μ§
// // => μλ¬κ° λ¨μ΄μ§λ λ§λ 무쑰건 μ€νμμΌμ€μΌν λ‘μ§
// }
const product = await this.productRepository.findOne({
where: { id: productId },
});
// if (product.isSoldout) {
// throw new HttpException(
// 'μ΄λ―Έ νλ§€κ° μλ£λ μνμ
λλ€',
// HttpStatus.UNPROCESSABLE_ENTITY, // 422 error
// );
// }
// ===
if (product.isSoldout)
throw new UnprocessableEntityException('μ΄λ―Έ ν맀 μλ£λ μνμ
λλ€');
}
β¬οΈ However we can just make a filter file so that we don't have to write try/catch every single time.
import { Catch, ExceptionFilter, HttpException } from '@nestjs/common';
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
catch(exception: HttpException) {
// μλ¬κ° λλ©΄ nest.jsμμ errorλ₯Ό λμ Έμ£Όλκ±°μ
const status = exception.getStatus();
const message = exception.message;
console.log('=================================');
console.log('Error has been detected');
console.log('Error Code: ', status);
console.log('Error Message: ', message);
console.log('=================================');
}
}
main.ts
file and enter app.useGlobalFilters();
http-exception.filter.ts
file and create extends
and implements
.import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { HttpExceptionFilter } from './commons/filter/http-exception.filter';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalFilters(new HttpExceptionFilter()); // μλ¬κ° λ¬μλ 리ν΄ν κ°
// class λΌμ new λ‘ declare
await app.listen(3000);
}
bootstrap();
const result = await this.productRepository.softDelete({ id: productId });
return result.affected ? true : false;
softDelete itself is a function that is included in node modules.
soft delete is different from actually deleting the data.
isDeleted
column, which saves the data of deletion. If the data is soft-deleted, there occurs a date, which is the deletion date. fetchProductsWithDeleted
API, it's able to see product lists with the deleted ones.// Many to one Joining
async findOne({ productId }) {
return await this.productRepository.findOne({
where: { id: productId },
relations: ['productSaleslocation', 'productCategory'],
});
}
relations
should be delcared to tell the computer to join those tables into one.