-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatePost.ts
53 lines (47 loc) · 1.23 KB
/
createPost.ts
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
"use server"
import { db } from "@/lib/db";
import { auth } from "../auth";
import { getUserByEmail } from "./user";
type Post = {
title: string
description: string
imgURL: string
}
export const createpost = async (
post: Post
) => {
try {
// Autenticação para obter a sessão e o ID do usuário
const session = await auth();
const authorId = session?.user?.email;
// Obtenção do usuário pelo e-mail
const user = await getUserByEmail(authorId as any);
if (!user) {
throw new Error('Usuário não encontrado.');
}
await db.post.create({
data: {
authorId: user.id,
title : post.title,
description : post.description,
imageUrl : post.imgURL
},
});
// console.log(res);
const updatedPosts = await db.post.findMany({
include : {
author : {
select : {
name: true,
username : true,
profileImageUrl : true,
}
}
}
})
return { updatedPosts, success: "Post criado com sucesso!", error: null };
} catch (error) {
console.error('Erro ao atualizar as informações:', error);
return { success: null, error: "Erro ao atualizar as informações." };
}
};