update/student-email
This commit is contained in:
1 parent
f8a071f066
commit
ae01dd80cc
2 files changed
+82
-7
No files matched your search
@@ -48,6 +48,27 @@ func AuthAdmin(c *fiber.Ctx) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func AuthStudent(c *fiber.Ctx) bool {
|
||||
cookie := c.Cookies("jwt")
|
||||
|
||||
token, err := jwt.ParseWithClaims(cookie, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
return []byte(SecretKey), nil
|
||||
})
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
claims := token.Claims.(*jwt.StandardClaims)
|
||||
|
||||
var student models.Admin
|
||||
findErr := adminCollection.FindOne(context.TODO(), bson.M{"sid": claims.Issuer}).Decode(&student)
|
||||
if findErr != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func Enroll(c *fiber.Ctx) error {
|
||||
var data map[string]string
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
|
||||
@@ -492,10 +492,10 @@ func UpdateStudentAddress(c *fiber.Ctx) error {
|
||||
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"PersonalData.address": data["address"],
|
||||
"PersonalData.city": data["city"],
|
||||
"PersonalData.province": data["province"],
|
||||
"PersonalData.postal": data["postal"],
|
||||
"PersonalData.Address": data["address"],
|
||||
"PersonalData.City": data["city"],
|
||||
"PersonalData.Province": data["province"],
|
||||
"PersonalData.Postal": data["postal"],
|
||||
"updated_at": update_time,
|
||||
},
|
||||
}
|
||||
@@ -609,9 +609,63 @@ func UpdateStudentPhoto(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func UpdateStudentEmail(c *fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
||||
"success": nil,
|
||||
"message": "not implimented",
|
||||
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) && !AuthStudent(c) {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "Unauthorized: only an admin or teacher can perform this action",
|
||||
})
|
||||
}
|
||||
|
||||
// Check required fields are included
|
||||
if data["sid"] == "" || data["email"] == "" {
|
||||
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{
|
||||
"PersonalData.Email": data["email"],
|
||||
"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,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user