28.3.23 확인하기 쉬울줄 알았는데 아니네요…

우리 팀의 테이블은 매우 커서 여기 저기 오기가 쉽지 않습니다.

방법을 잘 몰라서 하나씩 찾아보고 있는데 너무 어렵네요…

우리 팀 지구 주소…

https://www.erdcloud.com/p/ac2jrg24oF3N5Bq2A

ERDCloud

www.erdcloud.com

필터링할게 너무 많아서 너무 힘드네요…

//카테고리별 조회
  videosByCategory = async (genre, viewLimit) => {
    const findGenre = await CommonCodes.findOne({
      where: { codeUseColum: "genre", codeValue: genre },
      attributes: ("codename", "codeValue"),
    });
    console.log(findGenre.dataValues.codename, findGenre.dataValues.codeValue);

    if (!findGenre) {
      throw new Error(`No matching genre: ${genre}`);
    }

    const findCategory = await Content.findAll({
      raw: true,
      attributes: (
        "name",
        "videoUrl",
        "videoThumUrl",
        "contentIdx",
        "viewLimit",
      ),
      include: (
        {
          model: Category,
          as: "Categories",
          attributes: ("genre"),
          where: {
            genre: findGenre.dataValues.codeValue,
          },
        },
      ),
    });

    const filteredVideos = findCategory.filter((movie) => {
      return (
        movie.viewLimit === viewLimit ||
        (viewLimit === "VL000001" && movie.viewLimit === "VL000002")
      );
    });

    return { filteredVideos };
  };

오늘 3 Layered Architecture로 구현되며 저장소 부분의 코드입니다.

먼저 res.locals.profile에서 얻은 viewLimit과 params에서 얻은 Genre를 비교합니다.

Genre와 ViewLimit가 일치하면 VL000001입니다. VL000001,VL000002 모두 반환

매개변수의 viewLimit VL000002인 경우 콘텐츠 VL000002의 내용만 볼 수 있도록 코드를 생성했습니다.

const findGenre = await CommonCodes.findOne({
      where: { codeUseColum: "genre", codeValue: genre },
      attributes: ("codename", "codeValue"),
    });

CommonCodes 테이블에서 codeUseColumn이 “genre”이고 codeValue가 파라미터로 받은 Genre와 동일한 경우

이 코드는 Codename 및 CodeValue를 반환합니다.

const findCategory = await Content.findAll({
      raw: true,
      attributes: (
        "name",
        "videoUrl",
        "videoThumUrl",
        "contentIdx",
        "viewLimit",
      ),
      include: (
        {
          model: Category,
          as: "Categories",
          attributes: ("genre"),
          where: {
            genre: findGenre.dataValues.codeValue,
          },
        },
      ),
    });

그런 다음 모든 콘텐츠를 여기로 가져오세요.

include를 사용하여 Content 및 Category를 모델로 설정

범주에 대한 링크로.

그리고 속성으로 장르 찾기

where와 findGenre.dataValues.codeValue와 같은 장르의 값만 찾습니다.

const filteredVideos = findCategory.filter((movie) => {
      return (
        movie.viewLimit === viewLimit ||
        (viewLimit === "VL000001" && movie.viewLimit === "VL000002")
      );
    });

동영상에 대한 필터로 findCategory 지정

movie.viewLimit는 res.locals.profile에서 얻은 viewLimit과 동일합니다.

viewLimit이 VL000001이면 VL000001과 VL000002를 모두 반환합니다.

viewLimit이 VL000002일 때 VL000002만 반환하는 코드를 만들었습니다.