-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditPost.ts
42 lines (36 loc) · 1.1 KB
/
editPost.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
"use server"
import { db } from "@/lib/db";
import { auth } from "../auth";
import { getUserByEmail } from "./user";
export const editpost = async (
post_id: number,
title : string,
description : string,
imageUrl : string
) => {
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.');
}
const updatePost = await db.post.update({
where : {
id : post_id
},data : {
title,
description,
imageUrl
}
});
// cria o post e depois manda os posts atualizados
const updatedPosts = await db.post.findMany()
return { updatedPosts, success: "Informações atualizadas 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." };
}
};