☀️ BE TIL Day 17 0405

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


🌼 Building CRUD API

🌿 Type settings

export class Product {
  @PrimaryGeneratedColumn('uuid')
  @Field(() => String)
  id: string;

  @Column()
  @Field(() => String)
  name: string;

  @Column()
  @Field(() => Int)
  price: number;

  @Column({ default: false }) // mysql에 들어가는 부분
  @Field(() => Boolean)
  isSoldout: boolean;
}
  • isSoldout is in a boolean type, which the default value is written as false.
  • When isSoldout changes into true, then the user cannot edit the product.

🌿 Entity Query Settings


🌼 Try-Catch

async checkSoldout({ productId }) { // checking with the productId that was sent from resolver
     try {
       const product = await this.productRepository.findOne({
         where: { id: productId },
       });
       console.log('logic check');
     } catch (error) {
       throw error.message;
     } finally {
       // logic here is anyway executed whatever there is an error or not
    }

    if (product.isSoldout) // ===if (product.isSoldout === true)
      throw new UnprocessableEntityException('이미 판매 완료된 상품입니다');
  
  	// ===> condensed version of...
  	// if (product.isSoldout) {
    //   throw new HttpException(
    //     '이미 판매가 완료된 상품입니다',
    //     HttpStatus.UNPROCESSABLE_ENTITY, // 422 error
    //   );
    // }
}

⬇️ command shortcut
command + shift + L => select all the identical words

좋은 웹페이지 즐겨찾기