package controllers import ( "context" "school-management/models" "time" "github.com/dgrijalva/jwt-go" "github.com/gofiber/fiber/v2" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) func UpdateStudentName(c *fiber.Ctx) error { var data map[string]string ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) if err := c.BodyParser(&data); err != nil { cancel() return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "success": false, "message": "Failed to parse body", "error": err, }) } // Ensure Authenticated admin sent request if !AuthAdmin(c) { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, "message": "Unauthorized: only an admin can perform this action", }) } // Check id and names are included // Middle name is optional if data["sid"] == "" || data["firstname"] == "" || data["lastname"] == "" { cancel() return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "message": "missing required fields", }) } update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update := bson.M{ "$set": bson.M{ "firstname": data["firstname"], "middlename": data["middlename"], "lastname": data["lastname"], "updated_at": update_time, }, } result, 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 updated student", "result": result, }) } func UpdateStudentGradeLevel(c *fiber.Ctx) error { var data map[string]string ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) if err := c.BodyParser(&data); err != nil { cancel() return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "success": false, "message": "Failed to parse body", "error": err, }) } // Ensure Authorized admin sent request if !AuthAdmin(c) { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, "message": "Unauthorized: only an admin can perform this action", }) } // Check required fields are included if data["sid"] == "" || data["gradelevel"] == "" { cancel() return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "message": "missing required fields", }) } update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update := bson.M{ "$set": bson.M{ "gradelevel": data["gradelevel"], "updated_at": update_time, }, } result, 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 updated student", "result": result, }) } func UpdateStudentHomeroom(c *fiber.Ctx) error { var data map[string]string ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) if err := c.BodyParser(&data); err != nil { cancel() return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "success": false, "message": "Failed to parse body", "error": err, }) } // Ensure Authenticated admin sent request if !AuthAdmin(c) { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, "message": "Unauthorized: only an admin can perform this action", }) } // Check required fields are included if data["sid"] == "" || data["homeroom"] == "" { cancel() return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "message": "missing required fields", }) } update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update := bson.M{ "$set": bson.M{ "homeroom": data["homeroom"], "updated_at": update_time, }, } result, 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 updated student", "result": result, }) } func UpdateStudentPassword(c *fiber.Ctx) error { var data map[string]string ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) if err := c.BodyParser(&data); err != nil { cancel() return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "success": false, "message": "Failed to parse body", "error": err, }) } cookie := c.Cookies("jwt") token, err := jwt.ParseWithClaims(cookie, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) { return []byte(SecretKey), nil }) if err != nil { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, "message": "not authorized", }) } claims := token.Claims.(*jwt.StandardClaims) var student models.Student findErr := studentCollection.FindOne(context.TODO(), bson.M{"sid": claims.Issuer}).Decode(&student) if findErr != nil { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, "message": "student not found", }) } // Check required fields are included if data["currentPassword"] == "" || data["newPassword1"] == "" || data["newPassword2"] == "" { cancel() return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "message": "missing required fields", }) } if student.ComparePasswords(data["currentPassword"]) { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, "message": "Your current password is incorrect", }) } if data["newPassword1"] != data["newPassword2"] { cancel() return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "message": "Your new password must match", }) } update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update := bson.M{ "$set": bson.M{ "password": student.HashPassword(data["newPassword1"]), "temppassword": false, // If it were a temp password, its not now "updated_at": update_time, }, } result, 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 password could not be updated", "error": updateErr, }) } defer cancel() return c.Status(fiber.StatusOK).JSON(fiber.Map{ "success": true, "message": "successfully updated student password", "result": result, }) } func ResetStudentPassword(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "succes": nil, "message": "not implimented", }) } func UpdateStudentLocker(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, "message": "not implimented", }) } func UpdateStudentAddress(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, "message": "not implimented", }) } func UpdateStudentYOG(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, "message": "not implimented", }) } func UpdateStudentContacts(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, "message": "not implimented", }) } func UpdateStudentPhoto(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, "message": "not implimented", }) } func UpdateStudentEmail(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, "message": "not implimented", }) } func RemoveStudentsDisabled(c *fiber.Ctx) error { var data map[string]string ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) if err := c.BodyParser(&data); err != nil { cancel() return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "success": false, "message": "Failed to parse body", "error": err, }) } // Ensure Authorized admin sent request if !AuthAdmin(c) { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, "message": "Unauthorized: only an admin can perform this action", }) } // Check required fields are included if data["sid"] == "" { cancel() return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "message": "missing required fields", }) } update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update := bson.M{ "$set": bson.M{ "accountdisabled": false, "attempts": 0, "updated_at": update_time, }, } result, 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 account could not be re-enabled", "error": updateErr, }) } defer cancel() return c.Status(fiber.StatusOK).JSON(fiber.Map{ "success": true, "message": "successfully re-enabled student account", "result": result, }) } func UpdateTeacherHomeroom(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, "message": "not implimented", }) } func UpdateTeacherPassword(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, "message": "not implimented", }) } func UpdateTeacherAddress(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, "message": "not implimented", }) } func UpdateTeacherPhoto(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, "message": "not implimented", }) } func UpdateTeacherEmail(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, "message": "not implimented", }) } func UpdateTeacherName(c *fiber.Ctx) error { var data map[string]string ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) if err := c.BodyParser(&data); err != nil { cancel() return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "success": false, "message": "Failed to parse body", "error": err, }) } // Ensure Authenticated admin sent request if !AuthAdmin(c) { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, "message": "Unauthorized: only an admin can perform this action", }) } // Check id and names are included if data["_id"] == "" || data["firstname"] == "" || data["middlename"] == "" || data["lastname"] == "" { cancel() return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "message": "missing required fields", }) } teacherObjectId, idErr := primitive.ObjectIDFromHex(data["_id"]) if idErr != nil { cancel() return c.Status(fiber.StatusNotFound).JSON(fiber.Map{ "success": false, "message": "teacher not found", "error": idErr, }) } update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update := bson.M{ "$set": bson.M{ "firstname": data["firstname"], "middlename": data["middlename"], "lastname": data["lastname"], "updated_at": update_time, }, } result, updateErr := teacherCollection.UpdateOne( ctx, bson.M{"_id": teacherObjectId}, update, ) if updateErr != nil { cancel() return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "success": false, "message": "the teacher could not be updated", "error": updateErr, }) } defer cancel() return c.Status(fiber.StatusOK).JSON(fiber.Map{ "success": true, "message": "successfully updated teacher", "result": result, }) } func UpdateContactName(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, "message": "not implimented", }) } func UpdateContactAddress(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, "message": "not implimented", }) } func UpdateContactPhone(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, "message": "not implimented", }) } func UpdateContactEmail(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, "message": "not implimented", }) } func UpdateContactPriority(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, "message": "not implimented", }) }