From 6244041e5947f414971a3c06f073eedd627042c5 Mon Sep 17 00:00:00 2001 From: Braeden57 Date: Wed, 6 Oct 2021 14:08:26 -0700 Subject: [PATCH] UpdateStudentName func --- controllers/authController.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/controllers/authController.go b/controllers/authController.go index 60e3cd8..b8c7e7f 100644 --- a/controllers/authController.go +++ b/controllers/authController.go @@ -8,6 +8,7 @@ import ( "strconv" "github.com/gofiber/fiber/v2" + "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" ) @@ -106,7 +107,34 @@ func RegisterTeacher(c *fiber.Ctx) error { } func UpdateStudentName(c *fiber.Ctx) error { - return c.JSON("status:ok") + var data map[string]string + + if err := c.BodyParser(&data); err != nil { + return err + } + + // Check id and names are included + if data["_id"] == "" || data["firstname"] == "" || data["middlename"] == "" || data["lastname"] == "" { + return c.Status(400).JSON(ErrorStruct{Error: "missing required fields"}) + } + // Incoming json should include first, middle and last name. + // if no change is made to a name, it's old value will be put in + + studentObjectId := data["_id"] + update := bson.M{"$set": bson.M{ + "firtsname": data["firstname"], + "middlename": data["middlename"], + "lastname": data["lastname"]}} + + result, err := studentCollection.UpdateOne( + c.Context(), + bson.M{"_id": studentObjectId}, + update) + if err != nil { + return c.Status(500).JSON(ErrorStruct{Error: "Unable to Update student"}) + } + + return c.Status(200).JSON(result) } func UpdateStudentGradeLevel(c *fiber.Ctx) error {