💜 Key Point

order

💜 Today I Learned

[최종 프로젝트]

  • 판매글 상세 조회 시 뷰 카운트
    판매글을 findOne할 때마다 view가 1씩 증가하도록 하였다.
    view가 number로 설정되어 있어서 count 부분에 그냥 view + 1을 했는데 number 타입에 연산이 아닌 string타입 연산이 이루어져서 '11111' 이런식으로 값이 나왔다..

    그래서 콘솔에 찍어봤는데 number가 아닌 string으로 타입이 나왔다.
    혹시 몰라서 저장할 때의 값 view 타입은 number로 찍히는데 find한 값의 타입은 string으로 나온다.
    이것은 다시 해봐야겠다.
    우선 view 카운트를 하고 판매글을 update를 해주었다.
  async findOneMarket(marketId: number) {
    const market = await this.marketsRepository.findOne({
      where: { id: marketId },
    });
    if (!market) {
      throw new NotFoundException({ message: '판매글이 존재하지 않습니다.' });
    }
    await this.marketsRepository.update(
      { id: market.id },
      { view: +market.view + 1 },
    );
    const updateMarket = await this.marketsRepository.findOne({
      where: { id: marketId },
    });
    return updateMarket;
  }
  • 판매글 정렬
    판매글 전체 조회를 할 때 판매상태와 조회수를 기준으로 정렬하였다.
    view는 높은 순으로 판매상태는 enum 0값을 우선으로 조회되도록 정렬해주었다.
async findAllMarket(shoesId: number) {
    const shoes = await this.shoesRepository.findOne({
      where: { id: shoesId },
      select: ['name', 'brand', 'shoeCode', 'imgUrl'],
    });

    const shoesInfo = {
      name: shoes.name,
      brand: shoes.brand,
      shoeCode: shoes.shoeCode,
      imageUrl: shoes.imgUrl['imageUrl'],
    };
    const posts = await this.marketsRepository.find({
      where: { shoesId },
      order: {
        saleStatus: 'ASC',
        view: 'DESC',
      },
    });
    return { shoesInfo, posts };
  }

0개의 댓글