post.db.ts 1.21 KB
Newer Older
Lee Soobeom's avatar
Lee Soobeom committed
1
import { Post, PostType } from "../models";
Lee Soobeom's avatar
Lee Soobeom committed
2

Lee Soobeom's avatar
Lee Soobeom committed
3
4
5
6
7
8
9
10
export const createPost = async (post: PostType) => {
  const newPosting = await Post.create({
    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
11
    counts: 0,
Lee Soobeom's avatar
Lee Soobeom committed
12
13
14
  });
  return newPosting;
};
Lee Soobeom's avatar
Lee Soobeom committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

export const getPosts = async () => {
  const posts = await Post.find({});
  return posts;
};

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

  return newCounts;
};

export const getPost = async (_id: string) => {
  const post = await Post.findOne({ _id: _id });
  return post;
};
Lee Soobeom's avatar
Lee Soobeom committed
36
37
38
39
40
41
42
43
44

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
45
46
47
48
49
50
51
52
53
    {
      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
54
55
56
57
    { new: true }
  );
  return newPost;
};