update/student-locker-info
This commit is contained in:
1 parent
92ff358e3a
commit
1078b4f78a
3 files changed
+79
-4
No files matched your search
@@ -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{
|
return c.Status(fiber.StatusAccepted).JSON(fiber.Map{
|
||||||
"success": true,
|
"success": true,
|
||||||
"message": "successfully logged into student",
|
"message": "successfully logged into student",
|
||||||
"result": student,
|
"result": student,
|
||||||
|
"locker": locker,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -387,9 +387,74 @@ func ResetStudentPassword(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UpdateStudentLocker(c *fiber.Ctx) error {
|
func UpdateStudentLocker(c *fiber.Ctx) error {
|
||||||
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
var data map[string]string
|
||||||
"success": nil,
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
"message": "not implimented",
|
|
||||||
|
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,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"school-management/database"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
@@ -9,8 +10,11 @@ import (
|
|||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"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 {
|
type Student struct {
|
||||||
ID primitive.ObjectID `bson:"_id"`
|
ID primitive.ObjectID `bson:"_id"`
|
||||||
PersonalData struct {
|
PersonalData struct {
|
||||||
@@ -32,7 +36,7 @@ type Student struct {
|
|||||||
SID string `json:"sid"` // Student ID
|
SID string `json:"sid"` // Student ID
|
||||||
PEN string `json:"ped"` // Personal Education Number
|
PEN string `json:"ped"` // Personal Education Number
|
||||||
Homeroom string `json:"homeroom"`
|
Homeroom string `json:"homeroom"`
|
||||||
Locker Locker `json:"locker"`
|
Locker string `json:"locker"` // Locker ID
|
||||||
YOG int `json:"yog"` // Year of Graduation
|
YOG int `json:"yog"` // Year of Graduation
|
||||||
Photo string `json:"photo"`
|
Photo string `json:"photo"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user