post.db.ts 2.82 KB
Newer Older
Lee Soobeom's avatar
Lee Soobeom committed
1
import { Types, ObjectId } from "mongoose";
Lee Soobeom's avatar
Lee Soobeom committed
2
import fs from "fs/promises";
Lee Soobeom's avatar
Lee Soobeom committed
3
import { Post, PostType } from "../models";
Lee Soobeom's avatar
Lee Soobeom committed
4
import { FileInfo, IFileInfo } from "../models";
Lee Soobeom's avatar
Lee Soobeom committed
5

Lee Soobeom's avatar
Lee Soobeom committed
6
//Create
Lee Soobeom's avatar
Lee Soobeom committed
7
8
export const createPostRow = async (post: PostType) => {
  const newPostRow = await Post.create({
Lee Soobeom's avatar
Lee Soobeom committed
9
10
11
12
13
14
    title: post.title,
    text: post.text,
    theme: post.theme,
    city: post.city,
    user: post.user,
    date: post.date,
Lee Soobeom's avatar
Lee Soobeom committed
15
    counts: 0,
Lee Soobeom's avatar
Lee Soobeom committed
16
    file: post.file,
Lee Soobeom's avatar
Lee Soobeom committed
17
  });
Lee Soobeom's avatar
Lee Soobeom committed
18
19
20
21
22
23
24
25
26
27
28
29
30
  return newPostRow;
};

export const createFilesRow = async (
  originalfilename?: string | null,
  newfilename?: string,
  picturepath?: string
) => {
  const newFileRow = await FileInfo.create({
    originalfilename: originalfilename,
    newfilename: newfilename,
    picturepath: picturepath,
  });
Lee Soobeom's avatar
Lee Soobeom committed
31
32
  // console.log("check", newFileRow);
  return newFileRow._id;
Lee Soobeom's avatar
Lee Soobeom committed
33
34
};

Lee Soobeom's avatar
Lee Soobeom committed
35
//Read
Lee Soobeom's avatar
Lee Soobeom committed
36
export const getPosts = async () => {
Lee Soobeom's avatar
Lee Soobeom committed
37
38
39
40
  const posts = await Post.find()
    .populate("file")
    .populate("user")
    .sort({ date: -1 });
Lee Soobeom's avatar
Lee Soobeom committed
41
  return posts;
Lee Soobeom's avatar
Lee Soobeom committed
42
};
Lee Soobeom's avatar
Lee Soobeom committed
43

Lee Soobeom's avatar
Lee Soobeom committed
44
//Update
Lee Soobeom's avatar
Lee Soobeom committed
45
46
47
48
49
50
51
52
53
export const addOneCount = async (_id: string, counts: number) => {
  const newCounts = await Post.findOneAndUpdate(
    { _id: _id },
    { counts: counts },
    { new: true }
  );
  return newCounts;
};

Lee Soobeom's avatar
Lee Soobeom committed
54
export const updatePostRow = async (post: PostType, postId: string) => {
Lee Soobeom's avatar
Lee Soobeom committed
55
  const newPost = await Post.findOneAndUpdate(
Lee Soobeom's avatar
Lee Soobeom committed
56
    { _id: postId },
Lee Soobeom's avatar
Lee Soobeom committed
57
58
59
60
61
62
    {
      title: post.title,
      text: post.text,
      theme: post.theme,
      city: post.city,
      date: post.date,
Lee Soobeom's avatar
Lee Soobeom committed
63
      file: post.file,
Lee Soobeom's avatar
Lee Soobeom committed
64
    },
Lee Soobeom's avatar
Lee Soobeom committed
65
    { new: true }
Lee Soobeom's avatar
Lee Soobeom committed
66
  ).populate("file");
Lee Soobeom's avatar
Lee Soobeom committed
67
68
  return newPost;
};
Lee Soobeom's avatar
Lee Soobeom committed
69

Lee Soobeom's avatar
Lee Soobeom committed
70
71
72
73
74
export const getFilesByPostId = async (postId: string) => {
  const files = await Post.findOne({ _id: postId }).populate("file");
  return files?.file; //file Types.ObjectId[]
};

Lee Soobeom's avatar
Lee Soobeom committed
75
76
77
78
79
80
81
82
83
84
export const getOriginalFileName = async (_id: Types.ObjectId) => {
  const file = await FileInfo.findOne({ _id: _id });
  return file?.originalfilename;
};

export const updatedFileId = async (_id: string) => {
  const updatedFile = await Post.findOne({ _id: _id }).populate("file");
  return updatedFile?.file;
};

Lee Soobeom's avatar
Lee Soobeom committed
85
86
87
export const findByName = async (originalfilename: string) => {
  const fileId = await FileInfo.find({ originalfilename });
  return fileId;
Lee Soobeom's avatar
Lee Soobeom committed
88
89
};

Lee Soobeom's avatar
Lee Soobeom committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//Delete
export const deletePost = async (_id: string) => {
  const post = await Post.findById(_id);
  if (!(post?.file === undefined)) {
    for (var i = 0; i < post.file.length; i++) {
      const fileId = post.file[i];
      const ref = await FileInfo.findById(fileId);
      if (!(ref?.newfilename === undefined)) {
        await fs.unlink("../travel/uploads/" + ref?.newfilename);
      }
      await FileInfo.deleteOne({ _id: fileId });
    }
    const res = await Post.deleteOne({ _id: _id });
    return res;
  }
Lee Soobeom's avatar
Lee Soobeom committed
105
106
107
108
109
110
};

export const deleteFileByName = async (originalfilename: string) => {
  const deleteFile = await FileInfo.deleteOne({ originalfilename });
  return deleteFile;
};