@@ -218,7 +218,7 @@ func CreateModule(c *fiber.Ctx) error {
218218
219219 //Verify that the account attempting to create a module is the creator of the course
220220 if course .CreatorID != creator_id {
221- return c .Status (fiber .StatusNotFound ).JSON (fiber.Map {"error" : "User is not the course creator" })
221+ return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "User is not the course creator" })
222222 }
223223
224224 // Create module
@@ -263,7 +263,7 @@ func CreateContent(c *fiber.Ctx) error {
263263
264264 //Verify that the account attempting to create content is the creator of the course
265265 if module .Course .CreatorID != creator_id {
266- return c .Status (fiber .StatusNotFound ).JSON (fiber.Map {"error" : "User is not the course creator" })
266+ return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "User is not the course creator" })
267267 }
268268
269269 // Create content database entry
@@ -277,30 +277,84 @@ func CreateContent(c *fiber.Ctx) error {
277277 return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {"message" : err .Error ()})
278278 }
279279
280- //Save file
281- file , err := c .FormFile ("file" );
282- if err != nil {
283- return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "Invalid file" })
280+ return c .JSON (fiber.Map {
281+ "message" : "Content created successfully" ,
282+ "content" : content ,
283+ })
284+ }
285+
286+ // Edit content inside a module
287+ func EditContent (c * fiber.Ctx ) error {
288+ creator_id , err := strconv .Atoi (c .Params ("creator_id" ))
289+ if err != nil {
290+ return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "Invalid creator_id" })
284291 }
285292
286- var fileExtension = file .Filename [strings .LastIndex (file .Filename , "." ):]
293+ content_id , err := strconv .Atoi (c .Params ("content_id" ))
294+ if err != nil {
295+ return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "Invalid content_id" })
296+ }
287297
288- if err := os .MkdirAll (fmt .Sprintf ("./content/%d/" , module .Course .ID ), 0777 ); err != nil {
289- return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {"message" : err .Error ()})
298+ var content entity.Content
299+ // Check if the content exists
300+ if err := database .DB .Model (entity.Content {}).Preload ("Module.Course" ).Where ("id = ?" , content_id ).First (& content ).Error ; err != nil {
301+ return c .Status (fiber .StatusNotFound ).JSON (fiber.Map {"error" : "Course not found" })
302+ }
303+
304+ module := content .Module
305+ //Verify that the account attempting to edit is the creator of the course
306+ if module .Course .CreatorID != creator_id {
307+ return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "User is not the course creator" })
290308 }
291309
292- path := fmt .Sprintf ("/%d/%s" , module .Course .ID , strconv .FormatUint (uint64 (content .ID ), 10 ) + fileExtension )
293- if err := c .SaveFile (file , "./content" + path ); err != nil {
294- return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {"message" : err .Error ()})
310+ title := c .FormValue ("title" )
311+ if title != "" {
312+ if err := database .DB .Model (& content ).Update ("title" , title ).Error ; err != nil {
313+ return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {"message" : err .Error ()})
314+ }
315+ }
316+
317+ body := c .FormValue ("body" )
318+ if body != "" {
319+ if err := database .DB .Model (& content ).Update ("body" , body ).Error ; err != nil {
320+ return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {"message" : err .Error ()})
321+ }
295322 }
296323
297- // Add the path
298- if err := database .DB .Model (& content ).Update ("path" , path ).Error ; err != nil {
299- return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {"message" : err .Error ()})
324+ file , err := c .FormFile ("file" );
325+ if err != nil {
326+ if err .Error () != "there is no uploaded file associated with the given key" {
327+ return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "Invalid file" })
328+ }
329+ }
330+
331+ if file != nil {
332+ var fileExtension = file .Filename [strings .LastIndex (file .Filename , "." ):]
333+
334+ if err := os .MkdirAll (fmt .Sprintf ("./content/%d/" , module .Course .ID ), 0777 ); err != nil {
335+ return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {"message" : err .Error ()})
336+ }
337+
338+ path := fmt .Sprintf ("/%d/%s" , module .Course .ID , strconv .FormatUint (uint64 (content .ID ), 10 ) + fileExtension )
339+
340+ //Remove previous attachment if there is one
341+ if _ , err := os .Stat ("./content" + path ); os .IsExist (err ){
342+ if err := os .Remove ("./content" + path ); err != nil {
343+ return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {"message" : err .Error ()})
344+ }
345+ }
346+
347+ if err := c .SaveFile (file , "./content" + path ); err != nil {
348+ return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {"message" : err .Error ()})
349+ }
350+
351+ // Add the path
352+ if err := database .DB .Model (& content ).Update ("path" , path ).Error ; err != nil {
353+ return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {"message" : err .Error ()})
354+ }
300355 }
301356
302357 return c .JSON (fiber.Map {
303- "message" : "Content created successfully" ,
304- "content" : content ,
358+ "message" : "Successfully updated content" ,
305359 })
306360}
0 commit comments