A
A
Alexander2021-10-20 20:58:55
ORM
Alexander, 2021-10-20 20:58:55

How to check req query if there are more than 5?

Please tell me how the req query check is done if there are a lot of them, are there options with a short code, because doing an if for each element from the req query is not an option.
I will be grateful for the answer

async getAll(req: Request, res: Response, next: NextFunction) {
    let { typeId, categoryId, pageLimit, page, colorway, sortBy } = req.query

    page = page || (1 as any)
    pageLimit = pageLimit || (10 as any)
    //@ts-ignore
    let offset = page * pageLimit - pageLimit

    let products

    if (!typeId && !categoryId && !colorway && !sortBy) {
      //@ts-ignore
      products = await Product.findAndCountAll({
        distinct: true,
        include: [
          {
            model: ProductVariant,
            as: 'variant',
            include: [
              {
                model: Image,
                as: 'images',
                limit: 1,
              },
            ],
          },
        ],
      })
 
    }
    if (typeId && !categoryId && !colorway && !sortBy) {
      //@ts-ignore
      products = await Product.findAndCountAll({ where: { typeId }, limit: pageLimit, offset })
    }

    if (!typeId && categoryId && !sortBy && !colorway) {
      //@ts-ignore
      products = await Product.findAndCountAll({ where: { categoryId }, limit: pageLimit, offset })
    }
    if (typeId && categoryId && !sortBy && !colorway) {
      //@ts-ignore
      products = await Product.findAndCountAll({ where: { typeId, categoryId }, limit: pageLimit, offset })
    }
    if (!typeId && !categoryId && !sortBy && colorway) {
      products = await Product.findAndCountAll({
        // limit: pageLimit,
        // offset,
        where: {
          //@ts-ignore
          '$variant.color$': { [Op.eq]: colorway },
        },
        include: [
          {
            model: ProductVariant,
            as: 'variant',
            // limit: 4,
            // order: ,
            // attributes: [],
            // required: false,
          },
        ],
      })
    }
    if (!typeId && !categoryId && !colorway && sortBy) {
      console.log(sortBy)
      switch (sortBy) {
        case 'DESC':
          products = await Product.findAndCountAll({
            order: ,
            include: [
              {
                model: ProductVariant,
                as: 'variant',
              },
            ],
          })
          break
        case 'priceDesc':
          products = await Product.findAndCountAll({
            order: Sequelize.literal('max(variant.price) DESC'),
            include: [
              {
                model: ProductVariant,
                as: 'variant',
              },
            ],
            group: ['product.id', 'variant.id'],
          })
          break

        case 'priceAsc':
          products = await Product.findAndCountAll({
            order: Sequelize.literal('max(variant.price) ASC'),
            distinct: true,
            include: [
              {
                model: ProductVariant,
                as: 'variant',
              },
            ],
            group: ['product.id', 'variant.id'],
          })
          break

        default:
          break
      }
    }

    return res.json(products)
  }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question