fix db queries
This commit is contained in:
8 files changed
+61
-61
No files matched your search
@@ -633,7 +633,7 @@ func StudentLogin(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
var student models.Student
|
||||
err := StudentCollection.FindOne(ctx, bson.M{"School.sid": data["sid"]}).Decode(&student)
|
||||
err := StudentCollection.FindOne(ctx, bson.M{"school.sid": data["sid"]}).Decode(&student)
|
||||
|
||||
if err != nil {
|
||||
cancel()
|
||||
@@ -666,7 +666,7 @@ func StudentLogin(c *fiber.Ctx) error {
|
||||
|
||||
_, updateErr := StudentCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.sid": data["sid"]},
|
||||
bson.M{"school.sid": data["sid"]},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -715,7 +715,7 @@ func StudentLogin(c *fiber.Ctx) error {
|
||||
|
||||
_, updateErr := StudentCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.sid": data["sid"]},
|
||||
bson.M{"school.sid": data["sid"]},
|
||||
update,
|
||||
)
|
||||
cancel()
|
||||
@@ -742,7 +742,7 @@ func StudentLogin(c *fiber.Ctx) error {
|
||||
|
||||
_, updateErr := StudentCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.sid": data["sid"]},
|
||||
bson.M{"school.sid": data["sid"]},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -805,7 +805,7 @@ func TeacherLogin(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
var teacher models.Teacher
|
||||
err := TeacherCollection.FindOne(ctx, bson.M{"School.tid": data["tid"]}).Decode(&teacher)
|
||||
err := TeacherCollection.FindOne(ctx, bson.M{"school.tid": data["tid"]}).Decode(&teacher)
|
||||
defer cancel()
|
||||
|
||||
if err != nil {
|
||||
@@ -968,7 +968,7 @@ func Student(c *fiber.Ctx) error {
|
||||
responseData["photo"] = nil
|
||||
|
||||
var student models.Student
|
||||
findErr := StudentCollection.FindOne(context.TODO(), bson.M{"School.sid": sid}).Decode(&student)
|
||||
findErr := StudentCollection.FindOne(context.TODO(), bson.M{"school.sid": sid}).Decode(&student)
|
||||
if findErr != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
@@ -1033,7 +1033,7 @@ func Teacher(c *fiber.Ctx) error {
|
||||
claims := token.Claims.(*jwt.StandardClaims)
|
||||
|
||||
var teacher models.Teacher
|
||||
findErr := TeacherCollection.FindOne(context.TODO(), bson.M{"School.tid": claims.Issuer}).Decode(&teacher)
|
||||
findErr := TeacherCollection.FindOne(context.TODO(), bson.M{"school.tid": claims.Issuer}).Decode(&teacher)
|
||||
if findErr != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
@@ -1269,7 +1269,7 @@ func RemoveStudent(c *fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
_, deleteErr = StudentCollection.DeleteOne(ctx, bson.M{"School.sid": data["sid"]})
|
||||
_, deleteErr = StudentCollection.DeleteOne(ctx, bson.M{"school.sid": data["sid"]})
|
||||
if deleteErr != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
@@ -1327,7 +1327,7 @@ func RemoveTeacher(c *fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
_, deleteErr = TeacherCollection.DeleteOne(ctx, bson.M{"School.tid": data["tid"]})
|
||||
_, deleteErr = TeacherCollection.DeleteOne(ctx, bson.M{"school.tid": data["tid"]})
|
||||
if deleteErr != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
|
||||
@@ -37,7 +37,7 @@ func ValidateID(id string, userType int) bool { // true: valid id, false: id alr
|
||||
func ValidatePEN(pen string) bool { // true: valid pen, false: pen already in use
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
var foundID models.Id
|
||||
err := StudentCollection.FindOne(ctx, bson.M{"School.pen": pen}).Decode(&foundID)
|
||||
err := StudentCollection.FindOne(ctx, bson.M{"school.pen": pen}).Decode(&foundID)
|
||||
cancel()
|
||||
return err != nil
|
||||
}
|
||||
|
||||
@@ -378,7 +378,7 @@ func RemoveStudentsDisabled(c *fiber.Ctx) error {
|
||||
|
||||
result, updateErr := StudentCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.sid": data["sid"]},
|
||||
bson.M{"school.sid": data["sid"]},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -440,7 +440,7 @@ func RemoveTeachersDisabled(c *fiber.Ctx) error {
|
||||
|
||||
result, updateErr := TeacherCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.tid": data["tid"]},
|
||||
bson.M{"school.tid": data["tid"]},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
|
||||
@@ -63,17 +63,17 @@ func UpdateStudentName(c *fiber.Ctx) error {
|
||||
if updateMiddle {
|
||||
update = bson.M{
|
||||
"$set": bson.M{
|
||||
"Personal.firstname": data["firstname"],
|
||||
"Personal.middlename": data["middlename"],
|
||||
"Personal.lastname": data["lastname"],
|
||||
"personal.firstname": data["firstname"],
|
||||
"personal.middlename": data["middlename"],
|
||||
"personal.lastname": data["lastname"],
|
||||
"updated_at": update_time,
|
||||
},
|
||||
}
|
||||
} else {
|
||||
update = bson.M{
|
||||
"$set": bson.M{
|
||||
"Personal.firstname": data["firstname"],
|
||||
"Personal.lastname": data["lastname"],
|
||||
"personal.firstname": data["firstname"],
|
||||
"personal.lastname": data["lastname"],
|
||||
"updated_at": update_time,
|
||||
},
|
||||
}
|
||||
@@ -81,7 +81,7 @@ func UpdateStudentName(c *fiber.Ctx) error {
|
||||
|
||||
result, updateErr := StudentCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.sid": data["sid"]},
|
||||
bson.M{"school.sid": data["sid"]},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -142,7 +142,7 @@ func UpdateStudentGradeLevel(c *fiber.Ctx) error {
|
||||
|
||||
_, updateErr := StudentCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.sid": data["sid"].(string)},
|
||||
bson.M{"school.sid": data["sid"].(string)},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -213,7 +213,7 @@ func UpdateStudentHomeroom(c *fiber.Ctx) error {
|
||||
|
||||
_, updateErr := StudentCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.sid": data["sid"]},
|
||||
bson.M{"school.sid": data["sid"]},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -261,7 +261,7 @@ func UpdateStudentPassword(c *fiber.Ctx) error {
|
||||
claims := token.Claims.(*jwt.StandardClaims)
|
||||
|
||||
var student models.Student
|
||||
findErr := StudentCollection.FindOne(ctx, bson.M{"School.sid": claims.Issuer}).Decode(&student)
|
||||
findErr := StudentCollection.FindOne(ctx, bson.M{"school.sid": claims.Issuer}).Decode(&student)
|
||||
if findErr != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
@@ -317,7 +317,7 @@ func UpdateStudentPassword(c *fiber.Ctx) error {
|
||||
|
||||
_, updateErr := StudentCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.sid": claims.Issuer},
|
||||
bson.M{"school.sid": claims.Issuer},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -372,7 +372,7 @@ func ResetStudentPassword(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
var student models.Student
|
||||
findErr := StudentCollection.FindOne(context.TODO(), bson.M{"School.sid": data["sid"]}).Decode(&student)
|
||||
findErr := StudentCollection.FindOne(context.TODO(), bson.M{"school.sid": data["sid"]}).Decode(&student)
|
||||
if findErr != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
@@ -401,7 +401,7 @@ func ResetStudentPassword(c *fiber.Ctx) error {
|
||||
|
||||
result, updateErr := StudentCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.sid": data["sid"]},
|
||||
bson.M{"school.sid": data["sid"]},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -485,7 +485,7 @@ func UpdateStudentLocker(c *fiber.Ctx) error {
|
||||
|
||||
_, updateErr := StudentCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.sid": data["sid"]},
|
||||
bson.M{"school.sid": data["sid"]},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -538,10 +538,10 @@ func UpdateStudentAddress(c *fiber.Ctx) error {
|
||||
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"Personal.address": data["address"],
|
||||
"Personal.city": data["city"],
|
||||
"Personal.province": data["province"],
|
||||
"Personal.postal": data["postal"],
|
||||
"personal.address": data["address"],
|
||||
"personal.city": data["city"],
|
||||
"personal.province": data["province"],
|
||||
"personal.postal": data["postal"],
|
||||
"updated_at": update_time,
|
||||
},
|
||||
}
|
||||
@@ -600,7 +600,7 @@ func UpdateStudentYOG(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
var student models.Student
|
||||
findErr := StudentCollection.FindOne(context.TODO(), bson.M{"School.sid": data["sid"].(string)}).Decode(&student)
|
||||
findErr := StudentCollection.FindOne(context.TODO(), bson.M{"school.sid": data["sid"].(string)}).Decode(&student)
|
||||
if findErr != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
@@ -619,7 +619,7 @@ func UpdateStudentYOG(c *fiber.Ctx) error {
|
||||
|
||||
result, updateErr := StudentCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.sid": data["sid"].(string)},
|
||||
bson.M{"school.sid": data["sid"].(string)},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -687,13 +687,13 @@ func RemoveStudentContact(c *fiber.Ctx) error {
|
||||
"updated_at": update_time,
|
||||
},
|
||||
"$pull": bson.M{
|
||||
"Personal.contacts": contact.ID,
|
||||
"personal.contacts": contact.ID,
|
||||
},
|
||||
}
|
||||
|
||||
result, updateErr := StudentCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.sid": data["sid"]},
|
||||
bson.M{"school.sid": data["sid"]},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -761,13 +761,13 @@ func AddStudentContact(c *fiber.Ctx) error {
|
||||
"updated_at": update_time,
|
||||
},
|
||||
"$push": bson.M{
|
||||
"Personal.contacts": contact.ID,
|
||||
"personal.contacts": contact.ID,
|
||||
},
|
||||
}
|
||||
|
||||
result, updateErr := StudentCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.sid": data["sid"]},
|
||||
bson.M{"school.sid": data["sid"]},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -810,7 +810,7 @@ func UpdateStudentPhoto(c *fiber.Ctx) error {
|
||||
|
||||
// Get student
|
||||
var student models.Student
|
||||
findErr := StudentCollection.FindOne(context.TODO(), bson.M{"School.sid": sid}).Decode(&student)
|
||||
findErr := StudentCollection.FindOne(context.TODO(), bson.M{"school.sid": sid}).Decode(&student)
|
||||
if findErr != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
@@ -951,14 +951,14 @@ func UpdateStudentEmail(c *fiber.Ctx) error {
|
||||
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"Personal.email": data["email"],
|
||||
"personal.email": data["email"],
|
||||
"updated_at": update_time,
|
||||
},
|
||||
}
|
||||
|
||||
result, updateErr := StudentCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.sid": sid},
|
||||
bson.M{"school.sid": sid},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
|
||||
@@ -68,7 +68,7 @@ func UpdateTeacherHomeroom(c *fiber.Ctx) error {
|
||||
|
||||
_, updateErr := TeacherCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.tid": data["tid"]},
|
||||
bson.M{"school.tid": data["tid"]},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -116,7 +116,7 @@ func UpdateTeacherPassword(c *fiber.Ctx) error {
|
||||
claims := token.Claims.(*jwt.StandardClaims)
|
||||
|
||||
var teacher models.Teacher
|
||||
findErr := TeacherCollection.FindOne(ctx, bson.M{"School.tid": claims.Issuer}).Decode(&teacher)
|
||||
findErr := TeacherCollection.FindOne(ctx, bson.M{"school.tid": claims.Issuer}).Decode(&teacher)
|
||||
if findErr != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
@@ -172,7 +172,7 @@ func UpdateTeacherPassword(c *fiber.Ctx) error {
|
||||
|
||||
_, updateErr := TeacherCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.tid": claims.Issuer},
|
||||
bson.M{"school.tid": claims.Issuer},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -225,7 +225,7 @@ func ResetTeacherPassword(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
var teacher models.Teacher
|
||||
findErr := TeacherCollection.FindOne(context.TODO(), bson.M{"School.tid": data["tid"]}).Decode(&teacher)
|
||||
findErr := TeacherCollection.FindOne(context.TODO(), bson.M{"school.tid": data["tid"]}).Decode(&teacher)
|
||||
if findErr != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
@@ -254,7 +254,7 @@ func ResetTeacherPassword(c *fiber.Ctx) error {
|
||||
|
||||
result, updateErr := StudentCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.tid": data["tid"]},
|
||||
bson.M{"school.tid": data["tid"]},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -320,17 +320,17 @@ func UpdateTeacherAddress(c *fiber.Ctx) error {
|
||||
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"Personal.address": data["address"],
|
||||
"Personal.city": data["city"],
|
||||
"Personal.province": data["province"],
|
||||
"Personal.postal": data["postal"],
|
||||
"personal.address": data["address"],
|
||||
"personal.city": data["city"],
|
||||
"personal.province": data["province"],
|
||||
"personal.postal": data["postal"],
|
||||
"updated_at": update_time,
|
||||
},
|
||||
}
|
||||
|
||||
_, updateErr := TeacherCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.tid": data["tid"]},
|
||||
bson.M{"school.tid": data["tid"]},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -372,7 +372,7 @@ func UpdateTeacherPhoto(c *fiber.Ctx) error {
|
||||
|
||||
// Get teacher
|
||||
var teacher models.Teacher
|
||||
findErr := TeacherCollection.FindOne(context.TODO(), bson.M{"School.tid": tid}).Decode(&teacher)
|
||||
findErr := TeacherCollection.FindOne(context.TODO(), bson.M{"school.tid": tid}).Decode(&teacher)
|
||||
if findErr != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
@@ -514,14 +514,14 @@ func UpdateTeacherEmail(c *fiber.Ctx) error {
|
||||
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"Personal.email": data["email"],
|
||||
"personal.email": data["email"],
|
||||
"updated_at": update_time,
|
||||
},
|
||||
}
|
||||
|
||||
_, updateErr := TeacherCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.tid": tid},
|
||||
bson.M{"school.tid": tid},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -573,7 +573,7 @@ func UpdateTeacherName(c *fiber.Ctx) error {
|
||||
|
||||
// Get teacher
|
||||
var teacher models.Teacher
|
||||
findErr := TeacherCollection.FindOne(ctx, bson.M{"School.tid": data["tid"]}).Decode(&teacher)
|
||||
findErr := TeacherCollection.FindOne(ctx, bson.M{"school.tid": data["tid"]}).Decode(&teacher)
|
||||
if findErr != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
@@ -592,16 +592,16 @@ func UpdateTeacherName(c *fiber.Ctx) error {
|
||||
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"Personal.firstname": data["firstname"],
|
||||
"Personal.middlename": middlename,
|
||||
"Personal.lastname": data["lastname"],
|
||||
"personal.firstname": data["firstname"],
|
||||
"personal.middlename": middlename,
|
||||
"personal.lastname": data["lastname"],
|
||||
"updated_at": update_time,
|
||||
},
|
||||
}
|
||||
|
||||
_, updateErr := TeacherCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"School.tid": data["tid"]},
|
||||
bson.M{"school.tid": data["tid"]},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
|
||||
@@ -34,7 +34,7 @@ type Admin struct {
|
||||
|
||||
func (a *Admin) EmailExists(email string) bool {
|
||||
var admin Admin
|
||||
findErr := AdminCollection.FindOne(context.TODO(), bson.M{"Account.schoolemail": email}).Decode(&admin)
|
||||
findErr := AdminCollection.FindOne(context.TODO(), bson.M{"account.schoolemail": email}).Decode(&admin)
|
||||
return findErr == nil
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ type Student struct {
|
||||
Postal string `json:"postal"`
|
||||
DOB string `json:"dob" validate:"required"`
|
||||
Contacts []string `json:"contacts"` // List of contact ID's rather than contact object
|
||||
} `json:"Personal"`
|
||||
} `json:"personal"`
|
||||
School struct {
|
||||
GradeLevel float64 `json:"gradelevel" validate:"required"`
|
||||
SID string `json:"sid"` // Student ID
|
||||
@@ -75,7 +75,7 @@ func (s *Student) HashPassword(password string) string {
|
||||
|
||||
func (s *Student) EmailExists(email string) bool {
|
||||
var student Student
|
||||
findErr := StudentCollection.FindOne(context.TODO(), bson.M{"Account.schoolemail": email}).Decode(&student)
|
||||
findErr := StudentCollection.FindOne(context.TODO(), bson.M{"account.schoolemail": email}).Decode(&student)
|
||||
return findErr == nil
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ type Teacher struct {
|
||||
Province string `json:"province"`
|
||||
Postal string `json:"postal"`
|
||||
DOB string `json:"dob" validate:"required"`
|
||||
} `json:"Personal"`
|
||||
} `json:"personal"`
|
||||
School struct {
|
||||
TID string `json:"tid"` // Teacher ID
|
||||
Homeroom string `json:"homeroom"`
|
||||
@@ -73,7 +73,7 @@ func (t *Teacher) HashPassword(password string) string {
|
||||
|
||||
func (t *Teacher) EmailExists(email string) bool {
|
||||
var teacher Teacher
|
||||
findErr := TeacherCollection.FindOne(context.TODO(), bson.M{"Account.schoolemail": email}).Decode(&teacher)
|
||||
findErr := TeacherCollection.FindOne(context.TODO(), bson.M{"account.schoolemail": email}).Decode(&teacher)
|
||||
return findErr == nil
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user