diff --git a/controllers/authController.go b/controllers/authController.go index 21cce2c..4b4553d 100644 --- a/controllers/authController.go +++ b/controllers/authController.go @@ -56,6 +56,7 @@ func Enroll(c *fiber.Ctx) error { student.City = data["city"] student.Address = data["address"] student.Postal = data["postal"] + student.Contacts = []string{} student.YOG = ((12 - student.GradeLevel) + time.Now().Year()) + 1 @@ -345,6 +346,22 @@ func Teacher(c *fiber.Ctx) error { }) } +// Should work for both teacher and student ends +func Logout(c *fiber.Ctx) error { + cookie := fiber.Cookie{ + Name: "jwt", + Value: "", + Expires: time.Now().Add(-time.Hour), + HTTPOnly: true, + } + c.Cookie(&cookie) + + return c.Status(fiber.StatusOK).JSON(fiber.Map{ + "success": true, + "message": "successfully logged out", + }) +} + func UpdateStudentName(c *fiber.Ctx) error { var data map[string]string ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) @@ -637,7 +654,7 @@ func CreateContact(c *fiber.Ctx) error { } // Check required fields are included - if data["firstname"] == "" || data["lastname"] == "" || data["homephone"] == "" || data["email"] == "" || data["priority"] == "" || data["relation"] == "" { + if data["sid"] == "" || data["firstname"] == "" || data["lastname"] == "" || data["homephone"] == "" || data["email"] == "" || data["priority"] == "" || data["relation"] == "" { cancel() return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, @@ -668,19 +685,123 @@ func CreateContact(c *fiber.Ctx) error { cancel() return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "success": false, - "message": "the contact could not be inserted", + "message": "could not insert contact", "error": insertErr, }) } + + update := bson.M{ + "$push": bson.M{ + "contacts": contact.ID, + }, + } + _, updateErr := studentCollection.UpdateOne( + ctx, + bson.M{"sid": data["sid"]}, + update, + ) + if updateErr != nil { + cancel() + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "success": false, + "message": "the student could not be updated", + "error": updateErr, + }) + } defer cancel() return c.Status(fiber.StatusOK).JSON(fiber.Map{ "success": true, - "message": "successfully inserted contact", + "message": "successfully inserted contact to student", }) } -func UpdateContact(c *fiber.Ctx) error { +func RemoveContact(c *fiber.Ctx) error { + return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ + "success": nil, + "message": "not implimented", + }) +} + +func UpdateContactName(c *fiber.Ctx) error { + var data map[string]string + + if err := c.BodyParser(&data); err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "success": false, + "message": "Failed to parse body", + "error": err, + }) + } + + return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ + "success": nil, + "message": "not implimented", + }) +} + +func UpdateContactAddress(c *fiber.Ctx) error { + var data map[string]string + + if err := c.BodyParser(&data); err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "success": false, + "message": "Failed to parse body", + "error": err, + }) + } + + return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ + "success": nil, + "message": "not implimented", + }) +} + +func UpdateContactPhone(c *fiber.Ctx) error { + var data map[string]string + + if err := c.BodyParser(&data); err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "success": false, + "message": "Failed to parse body", + "error": err, + }) + } + + return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ + "success": nil, + "message": "not implimented", + }) +} + +func UpdateContactEmail(c *fiber.Ctx) error { + var data map[string]string + + if err := c.BodyParser(&data); err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "success": false, + "message": "Failed to parse body", + "error": err, + }) + } + + return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ + "success": nil, + "message": "not implimented", + }) +} + +func UpdateContactPriority(c *fiber.Ctx) error { + var data map[string]string + + if err := c.BodyParser(&data); err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "success": false, + "message": "Failed to parse body", + "error": err, + }) + } + return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, "message": "not implimented", diff --git a/models/studentModel.go b/models/studentModel.go index a5d27cc..7086a26 100644 --- a/models/studentModel.go +++ b/models/studentModel.go @@ -33,7 +33,7 @@ type Student struct { Postal string `json:"pc"` // Postal Code DOB string `json:"dob" validate:"required"` Photo string `json:"photo"` - Contacts []Contact `json:"contacts"` + Contacts []string `json:"contacts"` // List of contact ID's rather than contact object Created_at time.Time `json:"created_at"` Updated_at time.Time `json:"updated_at"` } diff --git a/routes/routes.go b/routes/routes.go index 7a724eb..9a918c9 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -31,6 +31,15 @@ func Setup(app *fiber.App) { app.Post("/api/student/updatePhoto", controllers.UpdateStudentPhoto) app.Post("/api/student/updateEmail", controllers.UpdateStudentEmail) + // Student Contacts + app.Post("/api/student/createContact", controllers.CreateContact) + app.Post("/api/student/contact/updateName", controllers.UpdateContactName) + app.Post("/api/student/contact/updateAddress", controllers.UpdateContactAddress) + app.Post("/api/student/contact/updatePhone", controllers.UpdateContactPhone) + app.Post("/api/student/contact/updateEmail", controllers.UpdateContactEmail) + app.Post("/api/student/contact/updatePriority", controllers.UpdateContactPriority) + app.Post("/api/student/contact/removeContact", controllers.RemoveContact) + // Login + Register handling app.Get("/api/teacher", controllers.Teacher) app.Post("/api/teacher/register", controllers.RegisterTeacher) @@ -43,4 +52,7 @@ func Setup(app *fiber.App) { app.Post("/api/teacher/updateName", controllers.UpdateTeacherName) app.Post("/api/teacher/updateHomeroom", controllers.UpdateTeacherHomeroom) app.Post("/api/teacher/updateEmail", controllers.UpdateTeacherEmail) + + // General Routes + app.Post("/api/logout", controllers.Logout) }