Skip to content

Commit

Permalink
fix : find saved posts
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedrinvits committed Nov 12, 2024
1 parent 7ee24ff commit e15a54d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
39 changes: 30 additions & 9 deletions data/save-post/getSavePost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import { db } from "@/lib/db";
import { auth } from "../../auth";
import { getUserByEmail } from "../user";


export const GetsavedPosts = async (
) => {
export const GetsavedPosts = async () => {
try {

// Obter a sessão do usuário atual
const session = await auth();
const authorId = session?.user?.email;

// Buscar o usuário pelo e-mail
const user = await getUserByEmail(authorId as any);

if (!user) {
throw new Error('Usuário não encontrado.');
}

// Buscar os posts salvos pelo usuário
const savedPosts = await db.savedPost.findMany({
where: {
userId: user.id,
Expand Down Expand Up @@ -46,7 +46,7 @@ export const GetsavedPosts = async (
},
author: {
select: {
id : true,
id: true,
username: true,
profileImageUrl: true,
},
Expand All @@ -64,17 +64,38 @@ export const GetsavedPosts = async (
},
_count: {
select: {
coments: true,
postLikes: true,
coments: true, // Contagem de comentários
postLikes: true, // Contagem de likes
},
},
},
},
},
distinct: ['postId'], // Aplica DISTINCT com base no postId
});

return { savedPosts, error: null };

// Modificar os dados para que a estrutura fique mais parecida com o retorno de findPosts
const formattedPosts = savedPosts.map((savedPost) => {
const post = savedPost.post;

return {
id: post.id,
title: post.title,
description: post.description,
imageUrl: post.imageUrl || '',
created_at: post.created_at,
updated_at: post.updated_at,
author: post.author, // Autor do post
postLikes: post.postLikes,
savedByUsers: post.savedByUsers,
coments: post.coments,
_count: post._count, // Contagem de comentários e likes
likes: post._count.postLikes,
commentsCount: post._count.coments,
};
});

return { savedPosts: formattedPosts, error: null };
} catch (error) {
return { success: null, error: "Erro ao atualizar as informações." };
}
Expand Down
28 changes: 3 additions & 25 deletions src/app/(app)/posts-saves/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,9 @@ export default function SavedPosts() {
const fetchSavedPosts = async () => {
try {
const res = await GetsavedPosts();
// console.log(res);

// if(res.success){
const transformedPosts = res?.savedPosts?.map((e: any) => {

return {
id: e.postId,
title: e.post.title,
description: e.post.description,
author: {
id : e.post.author.id,
username: e.post.author.username,
profileImageUrl: e.post.author.profileImageUrl,
},
commentsCount: e.post._count.coments,
likesCount: e.post._count.postLikes,
created_at: e.post.created_at,
};
});
// console.log(transformedPosts);


setPosts(transformedPosts as any);
// }

if(res.success){
setPosts(res.savedPosts);
}
} catch (error) {
console.error("Error fetching saved posts", error);
}
Expand Down

0 comments on commit e15a54d

Please sign in to comment.