mainimg.db.ts 1.91 KB
Newer Older
백승민's avatar
백승민 committed
1
2
3
4
5
6
7
8
9
import { Avatar, IAvatar, Mainimg, MainimgType } from "../models";
import { ObjectId } from "mongoose";

export const createMainimg = async (mainimg: MainimgType, pic: IAvatar) => {
  const newPic = await Avatar.create({
    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 () => {
백승민's avatar
백승민 committed
21
22
23
24
  const img = await Mainimg.find({}).populate("pic");

  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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
export const updateOneMainimg = async (
  _Id: string,
  theme: string,
  city: string,
  title: string,
  originalfilename?: string | null,
  newfilename?: string,
) => {
  const newMainimg = await Mainimg.findById(_Id);
console.log("error2",_Id)
  if (!(newMainimg?.pic === undefined)) {
    if (originalfilename === undefined) {
      await Mainimg.findByIdAndUpdate(newMainimg._id, {
        theme: theme,
        city: city,
        title: title,
      })
      console.log("errrror4")
    }
   else if(!(originalfilename === undefined)&&(!(theme === undefined)||!(city === undefined)||!(title === undefined))){
      await Mainimg.findByIdAndUpdate(newMainimg._id, {
        theme: theme,
        city: city,
        title: title,
      })
      await Avatar.findByIdAndUpdate(newMainimg.pic._id, {
        originalfilename: originalfilename,
        newfilename: newfilename,
      })
      console.log("error6")
    }
    else {
      await Avatar.findByIdAndUpdate(newMainimg.pic._id, {
        originalfilename: originalfilename,
        newfilename: newfilename,
      })
      console.log("error5",newfilename,originalfilename,theme,city,title)}
  }else(console.log("error3",newMainimg))
}