Compare commits

..

2 Commits

Author SHA1 Message Date
Guillaume Dorce 3e1a2f6b5b delete post picture if exist 2022-11-03 10:08:57 +01:00
Guillaume Dorce f01baf4c2e add edited info on message 2022-11-03 10:05:27 +01:00
6 changed files with 44 additions and 15 deletions

View File

@ -10,17 +10,25 @@ const Avatar = ({ user }: any) => {
const [firstLoad, setFirstLoad] = useState(true);
if (firstLoad) {
fetch(gravatarUrl).then((response) => {
if (response.status === 200) {
setAvatar(gravatarUrl);
}
setFirstLoad(false);
});
fetch(gravatarUrl)
.then((response) => {
if (response.ok) {
setAvatar(gravatarUrl);
}
setFirstLoad(false);
})
.catch((e) => {
setFirstLoad(false);
});
}
return (
<div className="avatar shrink-0">
<Image src={avatar} alt="avatar" className="rounded-full w-12 h-12 md:w-16 md:h-16 transition-all cursor-pointer" />
<Image
src={avatar}
alt="avatar"
className="rounded-full w-12 h-12 md:w-16 md:h-16 transition-all cursor-pointer"
/>
</div>
);
};

View File

@ -40,14 +40,17 @@ const Message = ({ message }: any) => {
</div>
<Text text={message.content} />
{message.image && <Image src={message.image} alt="image" className="w-fit rounded-lg cursor-pointer" />}
<div className="text-grey-light date">
{new Date(message.createdAt).toLocaleDateString(undefined, {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
})}
<div className="flex justify-between">
<div className="text-grey-light date">
{new Date(message.createdAt).toLocaleDateString(undefined, {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
})}
</div>
{message.edited && <div className="text-grey-light italic">Modifié</div>}
</div>
{me.data?.id === message.author.id ? null : (
<Like

View File

@ -37,6 +37,7 @@ model Post {
updatedAt DateTime @updatedAt
likes Int @default(0)
likedBy Like[]
edited Boolean @default(false)
}
model Like {

View File

@ -1,6 +1,7 @@
import multer from 'multer';
import path from 'path';
import {v4 as uuidv4} from 'uuid';
import fs from 'fs';
const storage = multer.diskStorage({
destination: path.join(__dirname, '../../public/uploads'),
@ -10,3 +11,12 @@ const storage = multer.diskStorage({
});
export const upload = multer({ storage });
export const deleteFile = (filename: string) => {
const filePath = path.join(__dirname, '../../public/uploads', filename);
fs.unlink(filePath, (err) => {
if (err) {
console.log(err);
}
});
}

View File

@ -2,6 +2,7 @@ import { PrismaClient, Post as PrismaPost, Like } from '@prisma/client';
import { Post } from '@/models/PostModel';
import { exclude } from '@/lib/utils';
import { getUserById } from './UserController';
import { deleteFile } from './FileController';
const prisma = new PrismaClient();
@ -66,6 +67,7 @@ const editPost = async (post: Post): Promise<PrismaPost | null | Error> => {
data: {
content: post.content,
image: post.image,
edited: true,
},
});
@ -94,6 +96,9 @@ const deletePost = async (id: number, userId: number): Promise<PrismaPost | Erro
if (post.authorId !== userId && user.role === 'USER') {
return new Error('User is not the author of this post');
}
if (post.image) {
deleteFile(post.image);
}
return prisma.post.delete({
where: {
id,

View File

@ -8,6 +8,7 @@ interface Post {
authorId: number;
likes?: number | undefined;
likedBy?: Like[] | undefined;
edited?: boolean | undefined;
}
const Post: z.ZodType<Post> = z.object({
@ -17,6 +18,7 @@ const Post: z.ZodType<Post> = z.object({
authorId: z.number(),
likes: z.number().optional(),
likedBy: z.array(Like).optional(),
edited: z.boolean().optional(),
});
export { Post };