🌼 BE TIL Day 19 0407

j00b33Β·2022λ…„ 4μ›” 7일
0

CodeCamp BE 02

λͺ©λ‘ 보기
18/30

⬇️ Main Note
https://docs.google.com/document/d/1IQfJPSjE7EDob5GXWJAtMe1uWW8ZpABc3r7-ieIaLDo/edit


🌿 Catch()

  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('=================================');
  }
}

☁️ Implements

  1. To set this filter, go to main.ts file and enter app.useGlobalFilters();
  2. Then, go to commons file and create filter folder.
  3. Create http-exception.filter.ts file and create extends and implements.
  • Extends : inheritance (상속)
  • Implements : (κ΅¬ν˜„)
    : This one's different from inheritance. Since it isn't being extended, there can be diverse implements.
  • Don't have to be in the format of typescript.
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();

🌿 soft delete

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.

  • soft delete is not completely removing the data from the database. To frontend, it seems like the data is gone, but the data is actually hidden inside the database.
  • Inside the database, there is isDeleted column, which saves the data of deletion. If the data is soft-deleted, there occurs a date, which is the deletion date.
  • If frontend tries to use fetchProductsWithDeleted API, it's able to see product lists with the deleted ones.
  • So basically it's just pretending to be deleted.

🌿 Join

// 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.

profile
μ½±γ…†l

0개의 λŒ“κΈ€