@@ -11,22 +11,21 @@ import (
1111
1212// Courses function retrieves enrolled course titles and descriptions based on user_id from the URL
1313func Courses (c * fiber.Ctx ) error {
14-
1514 user_id := c .Params ("user_id" )
1615
1716 var user entity.Account
1817 // Check if the user exists
1918 if err := database .DB .Where ("id = ?" , user_id ).First (& user ).Error ; err != nil {
2019 return c .Status (fiber .StatusNotFound ).JSON (fiber.Map {"error" : "User not found" })
2120 }
22-
21+
2322 var courses []entity.Course
2423 // Get all courses that user is enrolled in
2524 if err := database .DB .Model (& entity.Course {}).Where ("id IN (?)" , database .DB .Table ("enrollment" ).Select ("course_id" ).Where ("account_id = ?" , user_id )).Find (& courses ).Error ; err != nil {
2625 return c .Status (fiber .StatusNotFound ).JSON (fiber.Map {"error" : "Error retrieving courses" })
2726 }
2827
29- if len (courses ) == 0 {
28+ if len (courses ) == 0 {
3029 return c .JSON (fiber.Map {
3130 "message" : "Not enrolled in any courses" ,
3231 "courses" : courses ,
@@ -37,12 +36,10 @@ func Courses(c *fiber.Ctx) error {
3736 "message" : "Courses successfully retrieved" ,
3837 "courses" : courses ,
3938 })
40-
4139}
4240
4341// Course function retrieves an indiviudal course from its id
4442func Course (c * fiber.Ctx ) error {
45-
4643 course_id := c .Params ("course_id" )
4744
4845 var course entity.Course
@@ -53,14 +50,12 @@ func Course(c *fiber.Ctx) error {
5350
5451 return c .JSON (fiber.Map {
5552 "message" : "Course successfully retrieved" ,
56- "course" : course ,
53+ "course" : course ,
5754 })
58-
5955}
6056
6157// Modules function retrieves the modules of a course
6258func Modules (c * fiber.Ctx ) error {
63-
6459 course_id := c .Params ("course_id" )
6560
6661 var modules []entity.Module
@@ -69,7 +64,7 @@ func Modules(c *fiber.Ctx) error {
6964 return c .Status (fiber .StatusNotFound ).JSON (fiber.Map {"error" : "Course not found" })
7065 }
7166
72- if len (modules ) == 0 {
67+ if len (modules ) == 0 {
7368 return c .JSON (fiber.Map {
7469 "message" : "No modules in course" ,
7570 "modules" : modules ,
@@ -80,63 +75,60 @@ func Modules(c *fiber.Ctx) error {
8075 "message" : "Modules successfully retrieved" ,
8176 "modules" : modules ,
8277 })
83-
8478}
8579
8680// Content function retrieves the content of a module
8781func Content (c * fiber.Ctx ) error {
88-
8982 module_id := c .Params ("module_id" )
9083
9184 var module entity.Module
9285 // Check if the module exists
9386 if err := database .DB .Preload ("Content" ).Where ("id = ?" , module_id ).First (& module ).Error ; err != nil {
9487 return c .Status (fiber .StatusNotFound ).JSON (fiber.Map {"error" : "Module not found" })
9588 }
96-
89+
9790 var content = module .Content
9891
99- if len (content ) == 0 {
92+ if len (content ) == 0 {
10093 return c .JSON (fiber.Map {
10194 "message" : "No content in module" ,
102- "content" : [] string {},
95+ "content" : []string {},
10396 })
10497 }
10598
10699 return c .JSON (fiber.Map {
107100 "message" : "Content successfully retrieved" ,
108101 "content" : content ,
109102 })
110-
111103}
112104
113105// CreateCourse creates a course and adds it to the database.
114106func CreateCourse (c * fiber.Ctx ) error {
115-
116- creator_id ,err := strconv .Atoi (c .Params ("creator_id" ))
117- if err != nil {
107+ creator_id , err := strconv .Atoi (c .Params ("creator_id" ))
108+ if err != nil {
118109 return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "Invalid creator_id" })
119110 }
120111
121- var data map [string ]string ;
112+ var data map [string ]string
122113 if err := c .BodyParser (& data ); err != nil {
123114 return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "Invalid input" })
124115 }
125116
126117 //Verify that the account attempting to create a course is an educator
127- var role string ;
118+ var role string
128119 if err := database .DB .Model (& entity.Account {}).Select ("role" ).Where ("id = ?" , creator_id ).First (& role ).Error ; err != nil {
129120 return c .Status (fiber .StatusNotFound ).JSON (fiber.Map {"error" : "User not found" })
130121 }
131- if role != "educator" {
122+
123+ if role != "educator" {
132124 return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "User is not an educator" })
133125 }
134126
135127 // Create course
136128 course := entity.Course {
137- Title : data ["title" ],
129+ Title : data ["title" ],
138130 Description : data ["description" ],
139- CreatorID : creator_id ,
131+ CreatorID : creator_id ,
140132 }
141133
142134 // Add course to database
@@ -146,22 +138,21 @@ func CreateCourse(c *fiber.Ctx) error {
146138
147139 return c .JSON (fiber.Map {
148140 "message" : "Course created successfully" ,
149- "course" : course ,
141+ "course" : course ,
150142 })
151143}
152144
153145// Enroll user into course
154146func Enroll (c * fiber.Ctx ) error {
155-
156147 user_id := c .Params ("user_id" )
157148 course_id := c .Params ("course_id" )
158-
149+
159150 var user entity.Account
160151 // Check if the user exists
161152 if err := database .DB .Where ("id = ?" , user_id ).First (& user ).Error ; err != nil {
162153 return c .Status (fiber .StatusNotFound ).JSON (fiber.Map {"error" : "User not found" })
163154 }
164-
155+
165156 var course entity.Course
166157 // Check if the course exists
167158 if err := database .DB .Where ("id = ?" , course_id ).First (& course ).Error ; err != nil {
@@ -176,23 +167,21 @@ func Enroll(c *fiber.Ctx) error {
176167 return c .JSON (fiber.Map {
177168 "message" : fmt .Sprintf ("Successfully enrolled user id %d in course %s" , user .ID , course .Title ),
178169 })
179-
180170}
181171
182172// Create a module inside a course
183173func CreateModule (c * fiber.Ctx ) error {
184-
185- creator_id ,err := strconv .Atoi (c .Params ("creator_id" ))
186- if err != nil {
174+ creator_id , err := strconv .Atoi (c .Params ("creator_id" ))
175+ if err != nil {
187176 return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "Invalid creator_id" })
188177 }
189178
190- course_id ,err := strconv .Atoi (c .Params ("course_id" ))
191- if err != nil {
179+ course_id , err := strconv .Atoi (c .Params ("course_id" ))
180+ if err != nil {
192181 return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "Invalid course_id" })
193182 }
194183
195- var data map [string ]string ;
184+ var data map [string ]string
196185 if err := c .BodyParser (& data ); err != nil {
197186 return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "Invalid input" })
198187 }
@@ -202,14 +191,15 @@ func CreateModule(c *fiber.Ctx) error {
202191 if err := database .DB .Where ("id = ?" , course_id ).First (& course ).Error ; err != nil {
203192 return c .Status (fiber .StatusNotFound ).JSON (fiber.Map {"error" : "Course not found" })
204193 }
194+
205195 //Verify that the account attempting to create a module is the creator of the course
206- if course .CreatorID != creator_id {
196+ if course .CreatorID != creator_id {
207197 return c .Status (fiber .StatusNotFound ).JSON (fiber.Map {"error" : "User is not the course creator" })
208198 }
209199
210200 // Create module
211201 module := entity.Module {
212- Title : data ["title" ],
202+ Title : data ["title" ],
213203 Course : course ,
214204 }
215205
@@ -220,24 +210,23 @@ func CreateModule(c *fiber.Ctx) error {
220210
221211 return c .JSON (fiber.Map {
222212 "message" : "Module created successfully" ,
223- "module" : module ,
213+ "module" : module ,
224214 })
225215}
226216
227217// Create content inside a module
228218func CreateContent (c * fiber.Ctx ) error {
229-
230- creator_id ,err := strconv .Atoi (c .Params ("creator_id" ))
231- if err != nil {
219+ creator_id , err := strconv .Atoi (c .Params ("creator_id" ))
220+ if err != nil {
232221 return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "Invalid creator_id" })
233222 }
234223
235- module_id ,err := strconv .Atoi (c .Params ("module_id" ))
236- if err != nil {
224+ module_id , err := strconv .Atoi (c .Params ("module_id" ))
225+ if err != nil {
237226 return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "Invalid module_id" })
238227 }
239228
240- var data map [string ]string ;
229+ var data map [string ]string
241230 if err := c .BodyParser (& data ); err != nil {
242231 return c .Status (fiber .StatusBadRequest ).JSON (fiber.Map {"error" : "Invalid input" })
243232 }
@@ -247,16 +236,18 @@ func CreateContent(c *fiber.Ctx) error {
247236 if err := database .DB .Preload ("Course" ).Where ("id = ?" , module_id ).First (& module ).Error ; err != nil {
248237 return c .Status (fiber .StatusNotFound ).JSON (fiber.Map {"error" : "Module not found" })
249238 }
239+
250240 //Verify that the account attempting to create content is the creator of the course
251- if module .Course .CreatorID != creator_id {
241+ if module .Course .CreatorID != creator_id {
252242 return c .Status (fiber .StatusNotFound ).JSON (fiber.Map {"error" : "User is not the course creator" })
253243 }
254244
255245 // Create content
256246 content := entity.Content {
257- Title : data ["title" ],
247+ Title : data ["title" ],
258248 Module : module ,
259249 }
250+
260251 content .Path = fmt .Sprintf ("content/%d/%d" , module .Course .ID , content .ID )
261252
262253 // Add content to database
@@ -266,6 +257,6 @@ func CreateContent(c *fiber.Ctx) error {
266257
267258 return c .JSON (fiber.Map {
268259 "message" : "Content created successfully" ,
269- "content" : content ,
260+ "content" : content ,
270261 })
271- }
262+ }
0 commit comments