☀️ BE TIL Day 17 0405

JB·2022년 4월 5일
0

CodeCamp BE 02

목록 보기
16/30

⬇️ 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

profile
두비두밥밥

0개의 댓글