TODAY I LEARN ๐ŸŽ

TODAY I LEARN PROBELM ๐Ÿฆ 

๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ์™€ ๋ฆฌ์•„๋‹˜์ด ์ง  ์ฝ”๋“œ์˜ ์ฐจ์ด์ ์— ๋Œ€ํ•ด์„œ

๋ฐฐ์šด์ 

1.์ฝ”๋“œ์˜ ๋ช…ํ™•์„ฑ - ๋ณ€์ˆ˜๋ช…์„ ๋ˆ„๊ตฐ๊ฐ€ ๋‚ด ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ์ดํ•ดํ•  ์ˆ˜ ์žˆ๊ฒŒ๋” ์งฐ๋Š”๊ฐ€? โ‡’ ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง€๋‹ค๋ณด๋ฉด ํ˜ผ์ž๋งŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋ช…์„ ์“ด๋‹ค๋“ ์ง€ ๋‚˜๋งŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ๋กœ์ง์„ ์‚ฌ์šฉํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์งœ๊ฒŒ๋œ๋‹ค. ๋ˆ„๊ตฐ๊ฐ€์™€ ํ˜‘์—…ํ•˜์—ฌ ์ž‘์—…ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ช…์‹ฌํ•˜๊ณ  ์ž‘์—…ํ•˜์ž. 2.์ค‘๋ณต์„ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ์ง€ ๊ณ ๋ฏผํ•ด๋ณด์•˜๋Š”๊ฐ€? โ‡’ ๋‚ด๊ฐ€ ์ง  ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์ค‘๋ณต๋˜๋Š” ๋ถ€๋ถ„๋“ค์ด ๋„ˆ๋ฌด๋งŽ๋‹ค. ์ด๋ ‡๊ฒŒ ์ค‘๋ณต ๋˜๋Š” ๋ถ€๋ถ„๋“ค์ด ๋งŽ๋‹ค๋Š” ๊ฑด ์ค‘๋ณต์„ ์ค„ ์ผ ์ˆ˜ ์žˆ๋Š” ์—ฌ์ง€๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

  1. ์˜๋ฏธ์—†๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ํ• ๋‹นํ•˜์—ฌ ํšจ์œจ์„ฑ์„ ๊ณ ๋ คํ•˜์˜€๋Š”๊ฐ€? โ‡’ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ๋นˆ๋ฐฐ์—ด์„ 4๊ฐœ์”ฉ์ด๋‚˜ ์„ ์–ธํ•ด์„œ ๋ฉ”๋ชจ๋ฆฌ์— ๊ณต๊ฐ„์„ ๋งŒ๋“ค์–ด์„œ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๋Š”๋ฐ ์ด ๋ถ€๋ถ„์ด ๊ผญ ํ•„์š”ํ•œ์ง€ ํ™•์‹คํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์งœ๋ฉด์„œ ๊นŠ์ด ์ƒ๊ฐํ•ด๋ณด์ง€ ๋ชปํ•œ ๋“ฏ ์‹ถ๋‹ค.

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

  async createLabel(@Args() { labels }: CreateProductInput): Promise<String> {
    // input์— ์ž…๋ ฅ๋œ labelName, ๋ผ๋ฒจ isVisible, ์ƒˆ๋กœ ์ž…๋ ฅ๋œ labelName๋งŒ์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋นˆ ๋ฐฐ์—ด
    const labelName = [];
    const isVisible = [];

    for (const inputLabel in labels) {
      // ์ž…๋ ฅ๋œ ๋ผ๋ฒจ์„ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋Œ๋ฉด์„œ ๋ฐฐ์—ด labelName๊ณผ isVisible์— ๋”ฐ๋กœ ๋„ฃ์–ด์ค€๋‹ค.
      const inputLabelValue = Object.values(labels[inputLabel]);
      isVisible.push(inputLabelValue[0]);
      labelName.push(inputLabelValue[1]);
    }
    // ์ž…๋ ฅ๋ฐ›์€ labelName๊ณผ isVisible ์—ฌ๋ถ€๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด๋“ค 
		//console.log(labelName, isVisible)

    // 1. ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ผ๋ฒจ ์—ฌ๋ถ€ ํ™•์ธ
    const existedLabel = await this.prisma.label.findMany({
      where: {
        name: {
          in: labelName
        },
      },
      select: {
        name: true,
        id: true,
      },
    });
    const existedLabelWithId = existedLabel.map((item) => {
      return { labelId: item.id, labelName: item.name, isVisible: true };
    });

    // 2. ์ƒˆ๋กœ ๋“ฑ๋ก๋œ ๋ผ๋ฒจ์˜ Id์™€ isVisible์„ ๋งคํ•‘
    const mappedExistedLabel = existedLabelWithId.map((e, i) => {
      let temp = labels.find((element) => element.labelName === e.labelName);
      if (temp.labelName) {
        e.isVisible = temp.isVisible;
      }
      delete e.labelName;
      return e;
    });

    console.log(mappedExistedLabel);
    // labelId์™€ isVisible๋กœ ๋งคํ•‘๋œ ํ˜„์กดํ•˜๋Š” ๋ผ๋ฒจ

    // 3. ์ƒˆ๋กœ์šด ์ž…๋ ฅ๋œ ๋ผ๋ฒจ์ด ์žˆ์„ ๊ฒฝ์šฐ
    if (existedLabel.length !== 0) {
      // 3_1. ์ƒˆ๋กœ ๋ฐœ๊ฒฌ๋œ ๋ผ๋ฒจ๋งŒ filter๋กœ ๊ฑธ๋Ÿฌ ์„œ ๋ฐฐ์—ด์— ๋‹ด์•„์ค€๋‹ค.
			// ์ž…๋ ฅ๋œ labels๊ณผ existedLabel, ํ˜„์กดํ•˜๋Š” ๋ผ๋ฒจ์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ๋งŒ ํ•„ํ„ฐ๋กœ ๊ฑธ๋Ÿฌ์„œ ๋ฐฐ์—ด์— ๋‹ด์•„์ค€๋‹ค.
      const foundNewLabel = labels.filter(
        (inputLabel) =>
          !existedLabel.some(
            (inputExistedLabel) => inputLabel.labelName === inputExistedLabel.name,
          ),
      );

      // 3_2. ์ƒˆ๋กœ ๋ฐœ๊ฒฌ๋œ ๋ผ๋ฒจ์„ prisma๋กœ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด labelName => name์œผ๋กœ ๋ณ€๊ฒฝ
      const res = foundNewLabel.map((item) => {
        return { name: item.labelName };
      });

      // 3_3. ์ƒˆ๋กœ์šด ๋ผ๋ฒจ๋“ค์„ createMany๋ฅผ ํ†ตํ•ด label๋กœ ๋“ฑ๋ก
      const newLabel = await this.prisma.label.createMany({
        data: res,
      });

      const newLabelName = [];
      const newLabelIsVisible = [];

      // 3_4. ์ƒˆ๋กœ ๋ฐœ๊ฒฌ๋œ ๋ผ๋ฒจ์„ prisma๋กœ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด labelName => name์œผ๋กœ ๋ณ€๊ฒฝ
      const result = foundNewLabel.map((item) => {
        return { name: item.labelName, isVisible: item.isVisible };
      });

      // 3_5. ์ƒˆ๋กœ ์ž…๋ ฅ๋œ newLabelName๊ณผ newLabelIsVisible์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋นˆ ๋ฐฐ์—ด
      for (const inputLabel in result) {
        // ์ƒˆ๋กœ์šด ๋ผ๋ฒจ๋“ค์„ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉด์„œ ์ƒˆ๋กœ ์ž…๋ ฅ๋œ labelName๊ณผ isVisible์„ ๋ฐฐ์—ด ์•ˆ์— ๋„ฃ๋Š”๋‹ค.
        const inputLabelValue = Object.values(result[inputLabel]);
        newLabelName.push(inputLabelValue[0]);
        newLabelIsVisible.push(inputLabelValue[1]);
      }

      // 4. ์ƒˆ๋กœ ๋“ฑ๋ก๋œ ๋ผ๋ฒจ์„ ์กฐํšŒํ•œ๋‹ค.
      const registeredNewLabel = await this.prisma.label.findMany({
        where: {
          name: {
            in: newLabelName,
          },
        },
        select: {
          name: true,
          id: true,
        },
      });

      // 5. ์ƒˆ๋กœ ๋“ฑ๋ก๋œ ๋ผ๋ฒจ์˜ Id๋ฅผ labelId๋กœ ๋ณ€๊ฒฝ isVisible์„ ์ถ”๊ฐ€ ํ•˜๊ธฐ ์œ„ํ•ด m
      const registeredNewLabelWithId = registeredNewLabel.map((item) => {
        return { labelId: item.id, labelName: item.name, isVisible: true };
      });

      // 6. ์ƒˆ๋กœ ๋“ฑ๋ก๋œ ๋ผ๋ฒจ์˜ Id์™€ isVisible์„ ๋งคํ•‘
      const mappedNewLabel = registeredNewLabelWithId.map((e) => {
        let temp = foundNewLabel.find((element) => element.labelName === e.labelName);
        if (temp.labelName) {
          e.isVisible = temp.isVisible;
        }
        delete e.labelName;
        return e;
      });

      console.log(mappedNewLabel);
      // labelId์™€ isVisible๋กœ ๋งคํ•‘๋œ ์ƒˆ๋กœ ๋“ฑ๋ก๋œ ๋ผ๋ฒจ

			//ํ˜„์กดํ•˜๋Š” ๋ผ๋ฒจ๊ณผ ์ƒˆ๋กœ ๋“ฑ๋ก๋œ ๋ผ๋ฒจ์„ ํ•ฉ์ณ์„œ ๋ฐฐ์—ด์— ๋‹ด๋Š”๋‹ค.
		const res= mappedExistedLabel.concat(mappedNewLabel)
      // ์ดํ›„ has_labelํ…Œ์ด๋ธ”์— reviewTarget์„ ์ƒ์„ฑํ•  ๋•Œ ํ•œ ๋ฒˆ์— ์ƒ์„ฑํ•˜์—ฌ has_label ํ…Œ์ด๋ธ”์— ๋“ฑ๋กํ•œ๋‹ค.
    
    return res;
  }

HOW TO FIX MY PROBELM ๐Ÿ’Š

ERROR๋ฅผ ํ•ด๊ฒฐํ•œ ๐Ÿ”‘ Set map๊ณผ set์— ๋Œ€ํ•ด ์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์€ ๊ธ€ : https://medium.com/@hongkevin/js-5-es6-map-set-2a9ebf40f96b

// Array ๊ฐ์ฒด์™€์˜ ๊ด€๊ณ„
var myArray = ['value1', 'value2', 'value3'];

// Array๋ฅผ Set์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ •๊ทœ Set ์ƒ์„ฑ์ž ์‚ฌ์šฉ
var mySet = new Set(myArray);

mySet.has('value1'); // true ๋ฐ˜ํ™˜

// set์„ Array๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ์ „๊ฐœ ์—ฐ์‚ฐ์ž ์‚ฌ์šฉํ•จ.
console.log([...mySet]); // myArray์™€ ์ •ํ™•ํžˆ ๊ฐ™์€ ๋ฐฐ์—ด์„ ๋ณด์—ฌ์คŒ
const set1 = new Set([1, 2, 3, 4, 5]);

console.log(set1.has(1));
// expected output: true

console.log(set1.has(5));
// expected output: true

console.log(set1.has(6));
// expected output: false

์งˆ๋ฌธ)๋ผ๋ฒจ ๋“ฑ๋ก ๋ถ€๋ถ„์— ์žˆ์–ด์„œย  Set์ด๋ผ๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ์“ฐ๋Š” ์ด์œ ์— ๋Œ€ํ•ด์„œ

Q. Set์„ ํ™œ์šฉํ•˜์—ฌ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด ์“ฐ์ด๋Š”๊ฑด์ง€?

A: Set ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋Š”๋ฐ ์žˆ์–ด ์‹ค๋ฌด์—์„œ ๋งŽ์ด ์“ฐ์ธ๋‹ค.

EX> [{ isVisible: true, labelid: '๋‚˜์ดํ‚ค์กฐ๋˜' }, { isVisible: false, label: '๋‚˜์ดํ‚ค์กฐ๋˜' }]

โ‡’ Set์„ ํ™œ์šฉํ•œ๋‹ค๋ฉด ์ด๋Ÿฌํ•œ ํ˜•ํƒœ์˜ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค.

Q. Set์ด ์ œ๊ณตํ•˜๋Š” has์™€ Array์—์„œ ์“ธ ์ˆ˜ ์žˆ๋Š” includes์˜ ์„ฑ๋Šฅ ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€?