post.service.ts
1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { Injectable } from '@nestjs/common';
import { PrismaService } from 'nestjs-prisma';
import { AuthService } from 'src/auth/auth.service';
@Injectable()
export class PostService {
constructor(
private readonly auth: AuthService,
private readonly prisma: PrismaService,
) {}
async createPost(
token: string,
title: string,
privat: boolean,
content: string,
) {
const user = await this.auth.validateUser(token);
const post = await this.prisma.post.create({
data: {
author: {
connect: {
id: user.id,
},
},
title: title,
private: privat,
content: content,
},
});
return post;
}
async getPosts(take: number) {
const posts = await this.prisma.post.findMany({
take: take,
});
return posts;
}
async getPostsByUser(token: string, userId: string, take: number) {
const user = await this.auth.validateUser(token);
const posts = await this.prisma.post.findMany({
where: {
authorId: userId,
},
take: take,
});
return posts;
}
async getPost(id: string) {
const post = await this.prisma.post.findUnique({
where: {
id: id,
},
});
return post;
}
async updatePost(
token: string,
id: string,
title: string,
privat: boolean,
content: string,
) {
const user = await this.auth.validateUser(token);
const post = await this.prisma.post.update({
where: {
id: id,
},
data: {
title: title,
private: privat,
content: content,
},
});
return post;
}
async deletePost(token: string, id: string) {
const user = await this.auth.validateUser(token);
const post = await this.prisma.post.delete({
where: {
id: id,
},
});
return post;
}
}