mainimg.db.ts 2.42 KB
Newer Older
백승민's avatar
백승민 committed
1
import { ObjectId } from "mongoose";
Kim, MinGyu's avatar
Kim, MinGyu committed
2
import { FileInfo, IFileInfo, Mainimg, MainimgType } from "../models";
백승민's avatar
백승민 committed
3
4
import fs from "fs/promises";
import { fileInfoCtrl } from "../controllers";
백승민's avatar
백승민 committed
5

Kim, MinGyu's avatar
Kim, MinGyu committed
6
7
export const createMainimg = async (mainimg: MainimgType, pic: IFileInfo) => {
  const newPic = await FileInfo.create({
백승민's avatar
백승민 committed
8
9
10
11
    originalfilename: pic.originalfilename,
    newfilename: pic.newfilename,
    pictureauth: pic.picturepath,
  });
백승민's avatar
백승민 committed
12
13
14
15

  const newMainimg = await Mainimg.create({
    theme: mainimg.theme,
    city: mainimg.city,
백승민's avatar
백승민 committed
16
    fileInfo: newPic._id,
백승민's avatar
백승민 committed
17
18
    title: mainimg.title,
  });
백승민's avatar
백승민 committed
19
  return newMainimg.populate("fileInfo");
백승민's avatar
백승민 committed
20
21
22
};

export const getMainimg = async () => {
Kim, MinGyu's avatar
Kim, MinGyu committed
23
  const img = await Mainimg.find({}).populate("fileInfo");
백승민's avatar
백승민 committed
24
25
26

  return img;
};
백승민's avatar
백승민 committed
27
28

export const deleteOneMainimg = async (_id: string) => {
백승민's avatar
백승민 committed
29
30
31
32
33
34
35
  const main = await Mainimg.findById(_id);
  if (!(main?.fileInfo === undefined)) {
    const ref = await FileInfo.findById(main.fileInfo._id);
    if (!(ref?.newfilename === undefined)) {
      await fs.unlink("../travel/uploads/" + ref?.newfilename);
    }
    await FileInfo.deleteOne({ _id: main.fileInfo._id });
36
37
    const res = await Mainimg.deleteOne({ _id: _id });
    return res;
백승민's avatar
백승민 committed
38
  }
백승민's avatar
백승민 committed
39
};
백승민's avatar
백승민 committed
40

백승민's avatar
백승민 committed
41
42
43
44
45
46
export const updateOneMainimg = async (
  _Id: string,
  theme: string,
  city: string,
  title: string,
  originalfilename?: string | null,
47
  newfilename?: string
백승민's avatar
백승민 committed
48
49
) => {
  const newMainimg = await Mainimg.findById(_Id);
50
  console.log("error2", _Id);
Lee Soobeom's avatar
Lee Soobeom committed
51
  if (!(newMainimg?.fileInfo === undefined)) {
백승민's avatar
백승민 committed
52
53
54
55
56
    if (originalfilename === undefined) {
      await Mainimg.findByIdAndUpdate(newMainimg._id, {
        theme: theme,
        city: city,
        title: title,
57
58
59
60
61
62
63
64
      });
      console.log("errrror4");
    } else if (
      !(originalfilename === undefined) &&
      (!(theme === undefined) ||
        !(city === undefined) ||
        !(title === undefined))
    ) {
백승민's avatar
백승민 committed
65
66
67
68
      await Mainimg.findByIdAndUpdate(newMainimg._id, {
        theme: theme,
        city: city,
        title: title,
69
      });
Lee Soobeom's avatar
Lee Soobeom committed
70
      await FileInfo.findByIdAndUpdate(newMainimg.fileInfo._id, {
백승민's avatar
백승민 committed
71
72
        originalfilename: originalfilename,
        newfilename: newfilename,
73
74
75
      });
      console.log("error6");
    } else {
Lee Soobeom's avatar
Lee Soobeom committed
76
      await FileInfo.findByIdAndUpdate(newMainimg.fileInfo._id, {
백승민's avatar
백승민 committed
77
78
        originalfilename: originalfilename,
        newfilename: newfilename,
79
80
81
82
83
      });
      console.log("error5", newfilename, originalfilename, theme, city, title);
    }
  } else console.log("error3", newMainimg);
};