post.db.ts 2.34 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 { Post, PostType } from "../models";
Lee Soobeom's avatar
Lee Soobeom committed
3
import { FileInfo, IFileInfo } from "../models";
Lee Soobeom's avatar
Lee Soobeom committed
4

Lee Soobeom's avatar
Lee Soobeom committed
5
//Create
Lee Soobeom's avatar
Lee Soobeom committed
6
7
export const createPostRow = async (post: PostType) => {
  const newPostRow = await Post.create({
Lee Soobeom's avatar
Lee Soobeom committed
8
9
10
11
12
13
    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
14
    counts: 0,
Lee Soobeom's avatar
Lee Soobeom committed
15
    file: post.file,
Lee Soobeom's avatar
Lee Soobeom committed
16
  });
Lee Soobeom's avatar
Lee Soobeom committed
17
18
19
20
21
22
23
24
25
26
27
28
29
  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
30
31
  // console.log("check", newFileRow);
  return newFileRow._id;
Lee Soobeom's avatar
Lee Soobeom committed
32
33
};

Lee Soobeom's avatar
Lee Soobeom committed
34
//Read
Lee Soobeom's avatar
Lee Soobeom committed
35
36
export const getPosts = async () => {
  const posts = await Post.find({}).sort({ date: -1 });
Lee Soobeom's avatar
Lee Soobeom committed
37
  return posts;
Lee Soobeom's avatar
Lee Soobeom committed
38
};
Lee Soobeom's avatar
Lee Soobeom committed
39

Lee Soobeom's avatar
Lee Soobeom committed
40
41
42
export const getFilesByPostId = async (postId: string) => {
  const files = await Post.findOne({ _id: postId }).populate("file");
  return files?.file;
Lee Soobeom's avatar
Lee Soobeom committed
43
44
};

Lee Soobeom's avatar
Lee Soobeom committed
45
//Update
Lee Soobeom's avatar
Lee Soobeom committed
46
47
48
49
50
51
52
53
54
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
55
export const updatePostRow = async (post: PostType, postId: string) => {
Lee Soobeom's avatar
Lee Soobeom committed
56
  const newPost = await Post.findOneAndUpdate(
Lee Soobeom's avatar
Lee Soobeom committed
57
    { _id: postId },
Lee Soobeom's avatar
Lee Soobeom committed
58
59
60
61
62
63
64
    {
      title: post.title,
      text: post.text,
      theme: post.theme,
      city: post.city,
      date: post.date,
      user: post.user,
Lee Soobeom's avatar
Lee Soobeom committed
65
      file: post.file,
Lee Soobeom's avatar
Lee Soobeom committed
66
    },
Lee Soobeom's avatar
Lee Soobeom committed
67
68
69
70
    { new: true }
  );
  return newPost;
};
Lee Soobeom's avatar
Lee Soobeom committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

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;
};

//Delete
export const deletePost = async (_id: string) => {
  const res = await Post.deleteOne({ _id: _id });
  return res;
};

export const deleteFile = async (_id: Types.ObjectId) => {
  const deleteOne = await FileInfo.deleteOne({ _id: _id });
  return deleteOne;
};

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