@@ -2,7 +2,9 @@ package handlers
22
33import (
44 "fmt"
5+ "os"
56 "strconv"
7+ "strings"
68
79 "github.com/gofiber/fiber/v2"
810 "github.com/sandbox-science/online-learning-platform/configs/database"
@@ -83,8 +85,8 @@ func Modules(c *fiber.Ctx) error {
8385 })
8486}
8587
86- // Content function retrieves the content of a module
87- func Content (c * fiber.Ctx ) error {
88+ // AllContent function retrieves the content of a module
89+ func AllContent (c * fiber.Ctx ) error {
8890 module_id := c .Params ("module_id" )
8991
9092 var module entity.Module
@@ -108,6 +110,22 @@ func Content(c *fiber.Ctx) error {
108110 })
109111}
110112
113+ // Content function retrieves a single peice of content by id
114+ func Content (c * fiber.Ctx ) error {
115+ content_id := c .Params ("content_id" )
116+
117+ var content entity.Content
118+ // Check if the module exists
119+ if err := database .DB .Where ("id = ?" , content_id ).First (& content ).Error ; err != nil {
120+ return c .Status (fiber .StatusNotFound ).JSON (fiber.Map {"error" : "Content not found" })
121+ }
122+
123+ return c .JSON (fiber.Map {
124+ "message" : "Content successfully retrieved" ,
125+ "content" : content ,
126+ })
127+ }
128+
111129// CreateCourse creates a course and adds it to the database.
112130func CreateCourse (c * fiber.Ctx ) error {
113131 creator_id , err := strconv .Atoi (c .Params ("creator_id" ))
@@ -200,7 +218,7 @@ func CreateModule(c *fiber.Ctx) error {
200218
201219 //Verify that the account attempting to create a module is the creator of the course
202220 if course .CreatorID != creator_id {
203- 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" })
204222 }
205223
206224 // Create module
@@ -232,8 +250,8 @@ func CreateContent(c *fiber.Ctx) error {
232250 return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "Invalid module_id" })
233251 }
234252
235- var data map [ string ] string
236- if err := c . BodyParser ( & data ); err != nil {
253+ title := c . FormValue ( "title" )
254+ if title == "" {
237255 return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "Invalid input" })
238256 }
239257
@@ -245,18 +263,16 @@ func CreateContent(c *fiber.Ctx) error {
245263
246264 //Verify that the account attempting to create content is the creator of the course
247265 if module .Course .CreatorID != creator_id {
248- 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" })
249267 }
250-
251- // Create content
268+
269+ // Create content database entry
252270 content := entity.Content {
253- Title : data [ " title" ] ,
271+ Title : title ,
254272 Module : module ,
255273 }
256274
257- content .Path = fmt .Sprintf ("content/%d/%d" , module .Course .ID , content .ID )
258-
259- // Add content to database
275+ // Create content entry
260276 if err := database .DB .Create (& content ).Error ; err != nil {
261277 return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {"message" : err .Error ()})
262278 }
@@ -266,3 +282,126 @@ func CreateContent(c *fiber.Ctx) error {
266282 "content" : content ,
267283 })
268284}
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" })
291+ }
292+
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+ }
297+
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" })
308+ }
309+
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+ }
322+ }
323+
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+ }
355+
356+ // Add the type
357+ filetype := file .Header .Get ("Content-Type" )
358+ if err := database .DB .Model (& content ).Update ("type" , filetype ).Error ; err != nil {
359+ return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {"message" : err .Error ()})
360+ }
361+ }
362+
363+ return c .JSON (fiber.Map {
364+ "message" : "Successfully updated content" ,
365+ })
366+ }
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+ }
0 commit comments