@@ -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" ))
@@ -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
@@ -247,20 +265,40 @@ func CreateContent(c *fiber.Ctx) error {
247265 if module .Course .CreatorID != creator_id {
248266 return c .Status (fiber .StatusNotFound ).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 }
263279
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" })
284+ }
285+
286+ var fileExtension = file .Filename [strings .LastIndex (file .Filename , "." ):]
287+
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 ()})
290+ }
291+
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 ()})
295+ }
296+
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 ()})
300+ }
301+
264302 return c .JSON (fiber.Map {
265303 "message" : "Content created successfully" ,
266304 "content" : content ,
0 commit comments