mainimg.db.ts 2 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

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

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

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

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

export const deleteOneMainimg = async (_id: string) => {
  const res = await Mainimg.deleteOne({ _id: _id });
  return res;
};
백승민's avatar
백승민 committed
30

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