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

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

Lee Soobeom's avatar
Lee Soobeom committed
38
39
40
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
};

export const addOneCount = async (_id: string, counts: number) => {
  const newCounts = await Post.findOneAndUpdate(
    { _id: _id },
    { counts: counts },
    { new: true }
  );
  return newCounts;
};

export const getPost = async (_id: string) => {
  const post = await Post.findOne({ _id: _id });
  return post;
};
Lee Soobeom's avatar
Lee Soobeom committed
56
57
58
59
60
61
62
63
64

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

export const updateOnePost = async (post: PostType, _id: string) => {
  const newPost = await Post.findOneAndUpdate(
    { _id: _id },
Lee Soobeom's avatar
Lee Soobeom committed
65
66
67
68
69
70
71
72
73
    {
      title: post.title,
      text: post.text,
      theme: post.theme,
      city: post.city,
      date: post.date,
      counts: post.counts,
      user: post.user,
    },
Lee Soobeom's avatar
Lee Soobeom committed
74
75
76
77
    { new: true }
  );
  return newPost;
};