Skip to content

Commit 364ee26

Browse files
committed
Add button to delete uploaded file
1 parent 2ddecbb commit 364ee26

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed

backend/internal/handlers/courses.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,44 @@ func EditContent(c *fiber.Ctx) error {
364364
"message": "Successfully updated content",
365365
})
366366
}
367+
368+
// Delete file from content
369+
func DeleteFile(c *fiber.Ctx) error {
370+
creator_id, err := strconv.Atoi(c.Params("creator_id"))
371+
if err != nil {
372+
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid creator_id"})
373+
}
374+
375+
content_id, err := strconv.Atoi(c.Params("content_id"))
376+
if err != nil {
377+
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid content_id"})
378+
}
379+
380+
var content entity.Content
381+
// Check if the content exists
382+
if err := database.DB.Model(entity.Content{}).Preload("Module.Course").Where("id = ?", content_id).First(&content).Error; err != nil {
383+
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "Course not found"})
384+
}
385+
386+
module := content.Module
387+
//Verify that the account attempting to edit is the creator of the course
388+
if module.Course.CreatorID != creator_id {
389+
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "User is not the course creator"})
390+
}
391+
392+
//Delete file
393+
if _, err := os.Stat("./content"+content.Path); os.IsExist(err){
394+
if err := os.Remove("./content"+content.Path); err != nil{
395+
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": err.Error()})
396+
}
397+
}
398+
399+
//Update database entry
400+
if err := database.DB.Model(&content).Update("path", "").Error; err != nil{
401+
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": err.Error()})
402+
}
403+
404+
return c.JSON(fiber.Map{
405+
"message": "Successfully deleted file",
406+
})
407+
}

backend/internal/router/route.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func SetupRoutes(app *fiber.App) {
8787
app.Post("/create-course/:creator_id", handlers.CreateCourse)
8888
app.Post("/create-module/:creator_id/:course_id", handlers.CreateModule)
8989
app.Post("/create-content/:creator_id/:module_id", handlers.CreateContent)
90+
app.Post("/delete-file/:creator_id/:content_id", handlers.DeleteFile)
9091
app.Post("/edit-content/:creator_id/:content_id", handlers.EditContent)
9192
app.Post("/enroll/:user_id/:course_id", handlers.Enroll)
9293
}

frontend/src/components/Modal.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState } from 'react';
22

3-
export const Modal = ({ title, trigger, inputFields, changeHandler, confirmHandler, fileUploadHandler = null}) => {
3+
export const Modal = ({ title, trigger, inputFields, changeHandler, confirmHandler, fileUploadHandler, deleteFileHandler}) => {
44
const [showModal, setShowModal] = useState(false);
55

66
const close = () => {
@@ -46,10 +46,20 @@ export const Modal = ({ title, trigger, inputFields, changeHandler, confirmHandl
4646
<h3 className="font-semibold text-lg mb-4">{title}</h3>
4747
{fieldList}
4848
{fileUploadHandler != null ? (
49-
<div>
50-
<h1>File Upload</h1>
51-
<input type="file" onChange={fileUploadHandler}/>
52-
</div>) : null
49+
<div>
50+
<h1>File Upload</h1>
51+
<input type="file" onChange={fileUploadHandler}/>
52+
</div>) : null
53+
}
54+
{deleteFileHandler != null ? (
55+
<div>
56+
<button
57+
className="px-4 py-2 mt-2 border border-gray-300 rounded bg-red-500 text-white hover:bg-red-700"
58+
onClick={deleteFileHandler}
59+
>
60+
Delete Current File
61+
</button>
62+
</div>) : null
5363
}
5464
<div className="mt-4 flex justify-end space-x-2">
5565
<button

frontend/src/components/pages/Content.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function Content() {
2626
setFile(e.target.files[0]);
2727
}
2828

29-
const handleEditContent = (contentID) => async (e) =>{
29+
const handleEditContent = (contentID) => async (e) => {
3030
if (newContentInfo.title === "" && newContentInfo.body === "" && file === null){
3131
return
3232
}
@@ -56,6 +56,27 @@ export function Content() {
5656
}
5757
};
5858

59+
const handleDeleteFile = (contentID) => async (e) => {
60+
const userId = Cookies.get('userId');
61+
try {
62+
const response = await fetch(`http://localhost:4000/delete-file/${userId}/${contentID}`, {
63+
method: "POST"
64+
});
65+
66+
const data = await response.json();
67+
if (response.ok) {
68+
Notiflix.Notify.success("File successfully deleted!");
69+
setTimeout(() => {
70+
window.location.reload();
71+
}, 500);
72+
} else {
73+
Notiflix.Notify.failure(data.message || "File deletion failed");
74+
}
75+
} catch (error) {
76+
Notiflix.Notify.failure("Error occurred during file deletion");
77+
}
78+
};
79+
5980
useEffect(() => {
6081
const userId = Cookies.get('userId');
6182

@@ -125,6 +146,7 @@ export function Content() {
125146
changeHandler={handleContentChange}
126147
confirmHandler={handleEditContent(contentID)}
127148
fileUploadHandler={handleContentUpload}
149+
deleteFileHandler={contentInfo.path !== "" ? handleDeleteFile(contentID) : null}
128150
/>
129151

130152
);

0 commit comments

Comments
 (0)