@@ -436,3 +436,57 @@ func DeleteFile(c *fiber.Ctx) error {
436436 "message" : "Successfully deleted file" ,
437437 })
438438}
439+
440+ // Edit thumbnail of course
441+ func EditThumbnail (c * fiber.Ctx ) error {
442+ creator_id , err := strconv .Atoi (c .Params ("creator_id" ))
443+ if err != nil {
444+ return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "Invalid creator_id" })
445+ }
446+
447+ course_id , err := strconv .Atoi (c .Params ("course_id" ))
448+ if err != nil {
449+ return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "Invalid course_id" })
450+ }
451+
452+ var course entity.Course
453+ // Check if the course exists
454+ if err := database .DB .Where ("id = ?" , course_id ).First (& course ).Error ; err != nil {
455+ return c .Status (fiber .StatusNotFound ).JSON (fiber.Map {"error" : "Course not found" })
456+ }
457+
458+ //Verify that the account attempting to create a module is the creator of the course
459+ if course .CreatorID != creator_id {
460+ return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "User is not the course creator" })
461+ }
462+
463+ file , err := c .FormFile ("file" )
464+ if err != nil {
465+ if err .Error () != "there is no uploaded file associated with the given key" {
466+ return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "Invalid file" })
467+ }
468+ }
469+
470+ if file != nil {
471+ if err := os .MkdirAll (fmt .Sprintf ("./content/%d/" , course_id ), 0777 ); err != nil {
472+ return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {"message" : err .Error ()})
473+ }
474+
475+ //Remove previous attachment if there is one
476+ if _ , err := os .Stat (fmt .Sprintf ("./content/%d/thumbnail.png" , course_id )); err == nil {
477+ if err := os .Remove (fmt .Sprintf ("./content/%d/thumbnail.png" , course_id )); err != nil {
478+ return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {"message" : err .Error ()})
479+ }
480+ } else if ! os .IsNotExist (err ){
481+ return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {"message" : err .Error ()})
482+ }
483+
484+ if err := c .SaveFile (file , fmt .Sprintf ("./content/%d/thumbnail.png" , course_id )); err != nil {
485+ return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {"message" : err .Error ()})
486+ }
487+ }
488+
489+ return c .JSON (fiber.Map {
490+ "message" : "Successfully updated thumbnail" ,
491+ })
492+ }
0 commit comments