update/student-locker-info

This commit is contained in:
SowinskiBraeden committed 2022-01-18 16:41:29 -08:00
1 parent 92ff358e3a
commit 1078b4f78a
3 files changed
+79 -4

No files matched your search

+6
View File
@@ -624,10 +624,16 @@ func Student(c *fiber.Ctx) error {
})
}
var locker models.Locker
if student.SchoolData.Locker != "" {
lockerCollection.FindOne(context.TODO(), bson.M{"ID": student.SchoolData.Locker}).Decode(&locker)
}
return c.Status(fiber.StatusAccepted).JSON(fiber.Map{
"success": true,
"message": "successfully logged into student",
"result": student,
"locker": locker,
})
}
+68 -3
View File
@@ -387,9 +387,74 @@ func ResetStudentPassword(c *fiber.Ctx) error {
}
func UpdateStudentLocker(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) {
cancel()
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"success": false,
"message": "Unauthorized: only an admin can perform this action",
})
}
// Check id and locker are included
if data["sid"] == "" || data["lockerNumber"] == "" {
cancel()
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false,
"message": "missing required fields",
})
}
var locker models.Locker
err := lockerCollection.FindOne(ctx, bson.M{"sid": data["sid"]}).Decode(&locker)
if err != nil {
cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "locker not found",
"error": err,
})
}
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{
"$set": bson.M{
"School.Data.FirstName": locker.ID,
"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,
})
}
+5 -1
View File
@@ -2,6 +2,7 @@ package models
import (
"math/rand"
"school-management/database"
"strings"
"time"
"unicode"
@@ -9,8 +10,11 @@ import (
"golang.org/x/crypto/bcrypt"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
var lockerCollection *mongo.Collection = database.OpenCollection(database.Client, "lockers")
type Student struct {
ID primitive.ObjectID `bson:"_id"`
PersonalData struct {
@@ -32,7 +36,7 @@ type Student struct {
SID string `json:"sid"` // Student ID
PEN string `json:"ped"` // Personal Education Number
Homeroom string `json:"homeroom"`
Locker Locker `json:"locker"`
Locker string `json:"locker"` // Locker ID
YOG int `json:"yog"` // Year of Graduation
Photo string `json:"photo"`
}