diff --git a/back/prisma/schema.prisma b/back/prisma/schema.prisma index 6f2c662..818ed17 100644 --- a/back/prisma/schema.prisma +++ b/back/prisma/schema.prisma @@ -33,7 +33,7 @@ model Gallery { id String @id @default(uuid()) title String published Boolean @default(false) - image String + media String date DateTime author Users @relation(fields: [authorId], references: [id]) authorId String diff --git a/back/restore.js b/back/restore.js index ac41597..070f2ef 100644 --- a/back/restore.js +++ b/back/restore.js @@ -106,15 +106,15 @@ const restore = async ({ filepath }) => { } console.log("WRITING GALLERY"); - for (const image of dataJSON.gallery) { + for (const media of dataJSON.gallery) { await prisma.gallery.create({ data: { - id: image.id, - title: image.title, - published: image.published, - image: image.image, - date: image.date, - authorId: image.authorId, + id: media.id, + title: media.title, + published: media.published, + media: media.media, + date: media.date, + authorId: media.authorId, }, }); } diff --git a/back/routes/data.js b/back/routes/data.js index d4c5bad..63fc72d 100644 --- a/back/routes/data.js +++ b/back/routes/data.js @@ -173,21 +173,25 @@ router.post( await prisma.users.deleteMany(); console.log("WRITING USERS"); - await dataJSON.users.forEach(async (user) => { - await prisma.users.create({ - data: { - id: user.id, - username: user.username, - name: user.name, - password: user.password, - picture: user.picture, - privilege: user.privilege, - }, - }); - }); + for (const user of dataJSON.users) { + try { + const createdUser = await prisma.users.create({ + data: { + id: user.id, + username: user.username, + name: user.name, + password: user.password, + picture: user.picture, + privilege: user.privilege, + }, + }); + } catch (error) { + console.log("ERROR", error); + } + } console.log("WRITING POSTS"); - await dataJSON.posts.forEach(async (post) => { + for (const post of dataJSON.posts) { await prisma.posts.create({ data: { id: post.id, @@ -198,21 +202,21 @@ router.post( authorId: post.authorId, }, }); - }); + } console.log("WRITING GALLERY"); - await dataJSON.gallery.forEach(async (image) => { + for (const media of dataJSON.gallery) { await prisma.gallery.create({ data: { - id: image.id, - title: image.title, - published: image.published, - image: image.image, - date: image.date, - authorId: image.authorId, + id: media.id, + title: media.title, + published: media.published, + media: media.media, + date: media.date, + authorId: media.authorId, }, }); - }); + } fs.unlinkSync(zipPath); diff --git a/back/routes/gallery.js b/back/routes/gallery.js index 7f9099f..8f11a1c 100644 --- a/back/routes/gallery.js +++ b/back/routes/gallery.js @@ -28,7 +28,7 @@ router.use(bodyParser.urlencoded({ extended: true })); router.use(express.json()); router.get("/", authenticateToken, async function (req, res) { - const images = await prisma.gallery.findMany({ + const medias = await prisma.gallery.findMany({ include: { author: true, }, @@ -37,20 +37,14 @@ router.get("/", authenticateToken, async function (req, res) { }, }); - // modify the key image to media - images.forEach((image) => { - image.media = image.image; - delete image.image; - }); - - res.json(images); + res.json(medias); }); router.get("/page/:page", getUser, async function (req, res) { try { - var imagePerPage = 12; + var mediaPerPage = 12; if (req.query.nb) { - imagePerPage = parseInt(req.query.nb); + mediaPerPage = parseInt(req.query.nb); } const page = req.params.page; @@ -69,8 +63,8 @@ router.get("/page/:page", getUser, async function (req, res) { orderBy: { date: "desc", }, - skip: (page - 1) * imagePerPage, - take: imagePerPage, + skip: (page - 1) * mediaPerPage, + take: mediaPerPage, }); } else { medias = await prisma.gallery.findMany({ @@ -83,19 +77,13 @@ router.get("/page/:page", getUser, async function (req, res) { where: { published: true, }, - skip: (page - 1) * imagePerPage, - take: imagePerPage, + skip: (page - 1) * mediaPerPage, + take: mediaPerPage, }); } const totalMedias = await prisma.gallery.count(); - const totalPages = Math.ceil(totalMedias / imagePerPage); - - // modify the key image to media - medias.forEach((media) => { - media.media = media.image; - delete media.image; - }); + const totalPages = Math.ceil(totalMedias / mediaPerPage); res.json({ medias, totalPages, totalMedias }); } catch (error) { @@ -108,21 +96,17 @@ router.get("/:id", authenticateToken, async function (req, res) { try { const id = req.params.id; - const image = await prisma.gallery.findUnique({ + const media = await prisma.gallery.findUnique({ where: { id: id, }, }); - if (image === null) { - res.status(404).json({ error: "Image not found" }); + if (media === null) { + res.status(404).json({ error: "Media not found" }); return; } - // modify the key image to media - image.media = image.image; - delete image.image; - - res.json(image); + res.json(media); } catch (error) { console.log(error); res.status(500).json({ error: "Internal server error" }); @@ -135,7 +119,7 @@ router.post( upload.single("media"), async function (req, res) { var { title, date, author, published } = req.body; - const image = req.file.filename; + const media = req.file.filename; console.log(published); if (published === "true") { published = true; @@ -143,10 +127,10 @@ router.post( published = false; } - const newImage = await prisma.gallery.create({ + const newMedia = await prisma.gallery.create({ data: { title: title, - image: image, + media: media, published: published, date: date, authorId: author, @@ -162,7 +146,7 @@ router.post( path.join( __dirname, "../uploads/gallery/" + - newImage.id + + newMedia.id + path.extname(req.file.originalname) ), function (err) { @@ -175,14 +159,14 @@ router.post( // change the filename in the database await prisma.gallery.update({ where: { - id: newImage.id, + id: newMedia.id, }, data: { - image: newImage.id + path.extname(req.file.originalname), + media: newMedia.id + path.extname(req.file.originalname), }, }); - res.json(newImage); + res.json(newMedia); } ); @@ -195,32 +179,32 @@ router.put( const id = req.params.id; var { title, author, date, published } = req.body; - const imageData = await prisma.gallery.findUnique({ + const mediaData = await prisma.gallery.findUnique({ where: { id: id, }, }); - if (imageData === null) { - res.status(404).json({ error: "Image not found" }); + if (mediaData === null) { + res.status(404).json({ error: "Media not found" }); return; } - // if the user is not admin or owner or editor, and is not the author of the image, then he can't edit the image + // if the user is not admin or owner or editor, and is not the author of the media, then he can't edit the media if ( req.user.privilege !== "admin" && req.user.privilege !== "owner" && req.user.privilege !== "editor" && - req.user.id !== imageData.authorId + req.user.id !== mediaData.authorId ) { res .status(403) - .json({ error: "You are not allowed to edit this image" }); + .json({ error: "You are not allowed to edit this media" }); return; } // if the user is editor, he can't change the authorId - if (req.user.privilege === "editor" && imageData.authorId !== author) { + if (req.user.privilege === "editor" && mediaData.authorId !== author) { res .status(403) .json({ error: "You are not allowed to put another authorId" }); @@ -231,7 +215,7 @@ router.put( if (req.file) { // delete the old file fs.unlink( - path.join(__dirname, "../uploads/gallery/" + imageData.image), + path.join(__dirname, "../uploads/gallery/" + mediaData.media), function (err) { if (err) { console.log("ERROR: " + err); @@ -256,25 +240,25 @@ router.put( } ); - const updatedImage = await prisma.gallery.update({ + const updatedMedia = await prisma.gallery.update({ where: { id: id, }, data: { title, - image: id + path.extname(req.file.originalname), + media: id + path.extname(req.file.originalname), authorId: author, published, date, }, }); - // modify the key image to media - updatedImage.media = updatedImage.image; - delete updatedImage.image; - res.json(updatedImage); + // modify the key media to media + updatedMedia.media = updatedMedia.media; + delete updatedMedia.media; + res.json(updatedMedia); } else { - const updatedImage = await prisma.gallery.update({ + const updatedMedia = await prisma.gallery.update({ where: { id: id, }, @@ -285,10 +269,7 @@ router.put( date, }, }); - // modify the key image to media - updatedImage.media = updatedImage.image; - delete updatedImage.image; - res.json(updatedImage); + res.json(updatedMedia); } } catch (error) { console.log(error); @@ -300,33 +281,33 @@ router.put( router.delete("/:id", authenticateToken, async function (req, res) { try { const id = req.params.id; - const image = await prisma.gallery.findUnique({ + const media = await prisma.gallery.findUnique({ where: { id: id, }, }); - if (image === null) { - res.status(404).json({ error: "Image not found" }); + if (media === null) { + res.status(404).json({ error: "Media not found" }); return; } - // if the user is not admin or owner or editor, and is not the author of the image, then he can't delete the image + // if the user is not admin or owner or editor, and is not the author of the media, then he can't delete the media if ( req.user.privilege !== "admin" && req.user.privilege !== "owner" && req.user.privilege !== "editor" && - req.user.id !== image.authorId + req.user.id !== media.authorId ) { res .status(403) - .json({ error: "You are not allowed to delete this image" }); + .json({ error: "You are not allowed to delete this media" }); return; } // delete the file fs.unlink( - path.join(__dirname, "../uploads/gallery/" + image.image), + path.join(__dirname, "../uploads/gallery/" + media.media), function (err) { if (err) { console.log("ERROR: " + err); @@ -339,7 +320,7 @@ router.delete("/:id", authenticateToken, async function (req, res) { id: id, }, }); - res.json({ message: "Image deleted" }); + res.json({ message: "Media deleted" }); } catch (error) { console.log(error); res.status(500).json({ error: "Internal server error" }); diff --git a/back/routes/users.js b/back/routes/users.js index 7a6f523..132ca5b 100644 --- a/back/routes/users.js +++ b/back/routes/users.js @@ -148,7 +148,7 @@ router.post( // if the username is already taken, return an error if (user) { - // remove the image + // remove the media fs.unlinkSync( path.join(__dirname, "../uploads/pp/temp" + path.extname(filename)) ); @@ -274,7 +274,7 @@ router.post( ); } - var image = req.file + var media = req.file ? user.username + path.extname(req.file.filename) : user.picture; // update the user information in the database @@ -286,7 +286,7 @@ router.post( name, password: encryptedPassword, privilege, - picture: image, + picture: media, }, }); } else { @@ -295,7 +295,7 @@ router.post( data: { name, privilege, - picture: image, + picture: media, }, }); } @@ -351,7 +351,7 @@ router.delete("/delete/:id", authenticateToken, async (req, res) => { where: { id: user.id }, }); - // delete the user's image + // delete the user's media if (user.picture) { try { fs.unlinkSync(path.join(__dirname, "../uploads/pp", user.picture)); diff --git a/front/src/pages/admin/homeAdmin/Admin.jsx b/front/src/pages/admin/homeAdmin/Admin.jsx index c486636..b8e2864 100644 --- a/front/src/pages/admin/homeAdmin/Admin.jsx +++ b/front/src/pages/admin/homeAdmin/Admin.jsx @@ -83,7 +83,7 @@ const Admin = () => { {/* articles */} - {/* images */} + {/* gallery */} ) : (