post.db.ts 2.8 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
  const posts = await Post.find().populate("file").sort({ date: -1 });
  console.log(posts);
Lee Soobeom's avatar
Lee Soobeom committed
39
  return posts;
Lee Soobeom's avatar
Lee Soobeom committed
40
};
Lee Soobeom's avatar
Lee Soobeom committed
41

Lee Soobeom's avatar
Lee Soobeom committed
42
//Update
Lee Soobeom's avatar
Lee Soobeom committed
43
44
45
46
47
48
49
50
51
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
52
export const updatePostRow = async (post: PostType, postId: string) => {
Lee Soobeom's avatar
Lee Soobeom committed
53
  const newPost = await Post.findOneAndUpdate(
Lee Soobeom's avatar
Lee Soobeom committed
54
    { _id: postId },
Lee Soobeom's avatar
Lee Soobeom committed
55
56
57
58
59
60
    {
      title: post.title,
      text: post.text,
      theme: post.theme,
      city: post.city,
      date: post.date,
Lee Soobeom's avatar
Lee Soobeom committed
61
      file: post.file,
Lee Soobeom's avatar
Lee Soobeom committed
62
    },
Lee Soobeom's avatar
Lee Soobeom committed
63
64
65
66
    { new: true }
  );
  return newPost;
};
Lee Soobeom's avatar
Lee Soobeom committed
67

Lee Soobeom's avatar
Lee Soobeom committed
68
69
70
71
72
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
73
74
75
76
77
78
79
80
81
82
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
83
84
85
export const findByName = async (originalfilename: string) => {
  const fileId = await FileInfo.find({ originalfilename });
  return fileId;
Lee Soobeom's avatar
Lee Soobeom committed
86
87
};

Lee Soobeom's avatar
Lee Soobeom committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//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
103
104
105
106
107
108
};

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