proper / consitent status responses

This commit is contained in:
SowinskiBraeden committed 2022-05-22 12:25:45 -07:00
1 parent c2945bc013
commit 13cb0bde26
3 files changed
+38 -38

No files matched your search

+18 -18
View File
@@ -134,7 +134,7 @@ func Enroll(c *fiber.Ctx) error {
if err := c.BodyParser(&data); err != nil { if err := c.BodyParser(&data); err != nil {
cancel() cancel()
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false, "success": false,
"message": "Failed to parse body", "message": "Failed to parse body",
"error": err, "error": err,
@@ -263,7 +263,7 @@ func Enroll(c *fiber.Ctx) error {
} }
defer cancel() defer cancel()
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ return c.Status(fiber.StatusOK).JSON(fiber.Map{
"success": true, "success": true,
"message": "successfully inserted student", "message": "successfully inserted student",
}) })
@@ -501,7 +501,7 @@ func StudentLogin(c *fiber.Ctx) error {
if err != nil { if err != nil {
cancel() cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusOK).JSON(fiber.Map{
"success": false, "success": false,
"message": "student not found", "message": "student not found",
"error": err, "error": err,
@@ -537,7 +537,7 @@ func StudentLogin(c *fiber.Ctx) error {
if localAccountDisabled || student.AccountData.AccountDisabled { if localAccountDisabled || student.AccountData.AccountDisabled {
cancel() cancel()
return c.Status(fiber.StatusForbidden).JSON(fiber.Map{ return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"success": false, "success": false,
"message": "Account is Disabled, contact an Admin", "message": "Account is Disabled, contact an Admin",
}) })
@@ -566,7 +566,7 @@ func StudentLogin(c *fiber.Ctx) error {
"error": updateErr, "error": updateErr,
}) })
} }
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"success": false, "success": false,
"message": "incorrect password", "message": "incorrect password",
}) })
@@ -627,9 +627,9 @@ func TeacherLogin(c *fiber.Ctx) error {
if err != nil { if err != nil {
cancel() cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusOK).JSON(fiber.Map{
"success": false, "success": false,
"message": "student not found", "message": "teacher not found",
"error": err, "error": err,
}) })
} }
@@ -637,7 +637,7 @@ func TeacherLogin(c *fiber.Ctx) error {
var verified bool = teacher.ComparePasswords(data["password"]) var verified bool = teacher.ComparePasswords(data["password"])
if verified == false { if verified == false {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"success": false, "success": false,
"message": "incorrect password", "message": "incorrect password",
}) })
@@ -697,7 +697,7 @@ func AdminLogin(c *fiber.Ctx) error {
if err != nil { if err != nil {
cancel() cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusOK).JSON(fiber.Map{
"success": false, "success": false,
"message": "admin not found", "message": "admin not found",
"error": err, "error": err,
@@ -707,7 +707,7 @@ func AdminLogin(c *fiber.Ctx) error {
var verified bool = admin.ComparePasswords(data["password"]) var verified bool = admin.ComparePasswords(data["password"])
if verified == false { if verified == false {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"success": false, "success": false,
"message": "incorrect password", "message": "incorrect password",
}) })
@@ -787,7 +787,7 @@ func Student(c *fiber.Ctx) error {
var student models.Student var student models.Student
findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": sid}).Decode(&student) findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": sid}).Decode(&student)
if findErr != nil { if findErr != nil {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false, "success": false,
"message": "student not found", "message": "student not found",
}) })
@@ -834,7 +834,7 @@ func Teacher(c *fiber.Ctx) error {
return []byte(SecretKey), nil return []byte(SecretKey), nil
}) })
if err != nil { if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"success": false, "success": false,
"message": "not authorized", "message": "not authorized",
}) })
@@ -845,13 +845,13 @@ func Teacher(c *fiber.Ctx) error {
var teacher models.Teacher var teacher models.Teacher
findErr := teacherCollection.FindOne(context.TODO(), bson.M{"schooldata.tid": claims.Issuer}).Decode(&teacher) findErr := teacherCollection.FindOne(context.TODO(), bson.M{"schooldata.tid": claims.Issuer}).Decode(&teacher)
if findErr != nil { if findErr != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false, "success": false,
"message": "teacher not found", "message": "teacher not found",
}) })
} }
return c.Status(fiber.StatusAccepted).JSON(fiber.Map{ return c.Status(fiber.StatusOK).JSON(fiber.Map{
"success": true, "success": true,
"message": "successfully logged into teacher", "message": "successfully logged into teacher",
"result": teacher, "result": teacher,
@@ -876,13 +876,13 @@ func Admin(c *fiber.Ctx) error {
var admin models.Admin var admin models.Admin
findErr := adminCollection.FindOne(context.TODO(), bson.M{"aid": claims.Issuer}).Decode(&admin) findErr := adminCollection.FindOne(context.TODO(), bson.M{"aid": claims.Issuer}).Decode(&admin)
if findErr != nil { if findErr != nil {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false, "success": false,
"message": "admin not found", "message": "admin not found",
}) })
} }
return c.Status(fiber.StatusAccepted).JSON(fiber.Map{ return c.Status(fiber.StatusOK).JSON(fiber.Map{
"success": true, "success": true,
"message": "successfully logged into admin", "message": "successfully logged into admin",
"result": admin, "result": admin,
@@ -1024,7 +1024,7 @@ func DeleteContact(c *fiber.Ctx) error {
_, err := contactCollection.DeleteOne(ctx, bson.M{"_id": data["id"]}) _, err := contactCollection.DeleteOne(ctx, bson.M{"_id": data["id"]})
if err != nil { if err != nil {
cancel() cancel()
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false, "success": false,
"message": "Failed to delete object", "message": "Failed to delete object",
"error": err, "error": err,
@@ -1032,7 +1032,7 @@ func DeleteContact(c *fiber.Ctx) error {
} }
defer cancel() defer cancel()
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ return c.Status(fiber.StatusOK).JSON(fiber.Map{
"success": true, "success": true,
"message": "Successfully deleted contact", "message": "Successfully deleted contact",
}) })
+19 -19
View File
@@ -273,7 +273,7 @@ func UpdateStudentPassword(c *fiber.Ctx) error {
findErr := studentCollection.FindOne(ctx, bson.M{"schooldata.sid": claims.Issuer}).Decode(&student) findErr := studentCollection.FindOne(ctx, bson.M{"schooldata.sid": claims.Issuer}).Decode(&student)
if findErr != nil { if findErr != nil {
cancel() cancel()
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false, "success": false,
"message": "student not found", "message": "student not found",
}) })
@@ -292,7 +292,7 @@ func UpdateStudentPassword(c *fiber.Ctx) error {
cancel() cancel()
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"success": false, "success": false,
"message": "Your current password is incorrect", "message": "Your password is incorrect",
}) })
} }
@@ -373,7 +373,7 @@ func ResetStudentPassword(c *fiber.Ctx) error {
findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": data["sid"]}).Decode(&student) findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": data["sid"]}).Decode(&student)
if findErr != nil { if findErr != nil {
cancel() cancel()
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false, "success": false,
"message": "student not found", "message": "student not found",
}) })
@@ -468,7 +468,7 @@ func UpdateStudentLocker(c *fiber.Ctx) error {
err := lockerCollection.FindOne(ctx, bson.M{"lockernumber": data["lockernumber"]}).Decode(&locker) err := lockerCollection.FindOne(ctx, bson.M{"lockernumber": data["lockernumber"]}).Decode(&locker)
if err != nil { if err != nil {
cancel() cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false, "success": false,
"message": "locker not found", "message": "locker not found",
"error": err, "error": err,
@@ -569,7 +569,7 @@ func UpdateStudentAddress(c *fiber.Ctx) error {
}) })
} }
// In the case a student gets help back a grade, we need to update their YOG (Year of Graduation) // In the case a student gets held back a grade, we need to update their YOG (Year of Graduation)
func UpdateStudentYOG(c *fiber.Ctx) error { func UpdateStudentYOG(c *fiber.Ctx) error {
var data map[string]string var data map[string]string
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
@@ -605,7 +605,7 @@ func UpdateStudentYOG(c *fiber.Ctx) error {
findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": data["sid"]}).Decode(&student) findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": data["sid"]}).Decode(&student)
if findErr != nil { if findErr != nil {
cancel() cancel()
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false, "success": false,
"message": "student not found", "message": "student not found",
}) })
@@ -676,7 +676,7 @@ func RemoveStudentContact(c *fiber.Ctx) error {
err := contactCollection.FindOne(ctx, bson.M{"_id": data["contactid"]}).Decode(&contact) err := contactCollection.FindOne(ctx, bson.M{"_id": data["contactid"]}).Decode(&contact)
if err != nil { if err != nil {
cancel() cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false, "success": false,
"message": "contact not found", "message": "contact not found",
"error": err, "error": err,
@@ -750,7 +750,7 @@ func AddStudentContact(c *fiber.Ctx) error {
err := contactCollection.FindOne(ctx, bson.M{"_id": data["contactid"]}).Decode(&contact) err := contactCollection.FindOne(ctx, bson.M{"_id": data["contactid"]}).Decode(&contact)
if err != nil { if err != nil {
cancel() cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false, "success": false,
"message": "contact not found", "message": "contact not found",
"error": err, "error": err,
@@ -815,7 +815,7 @@ func UpdateStudentPhoto(c *fiber.Ctx) error {
findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": sid}).Decode(&student) findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": sid}).Decode(&student)
if findErr != nil { if findErr != nil {
cancel() cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false, "success": false,
"message": "the student could not be found", "message": "the student could not be found",
"error": findErr, "error": findErr,
@@ -1026,7 +1026,7 @@ func RemoveStudentsDisabled(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(fiber.Map{ return c.Status(fiber.StatusOK).JSON(fiber.Map{
"success": true, "success": true,
"message": "successfully re-enabled student account", "message": "successfully enabled student account",
"result": result, "result": result,
}) })
} }
@@ -1080,7 +1080,7 @@ func RemoveTeachersDisabled(c *fiber.Ctx) error {
cancel() cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false, "success": false,
"message": "the student account could not be re-enabled", "message": "the teacher account could not be enabled",
"error": updateErr, "error": updateErr,
}) })
} }
@@ -1088,7 +1088,7 @@ func RemoveTeachersDisabled(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(fiber.Map{ return c.Status(fiber.StatusOK).JSON(fiber.Map{
"success": true, "success": true,
"message": "successfully re-enabled teacher account", "message": "successfully enabled teacher account",
"result": result, "result": result,
}) })
} }
@@ -1197,7 +1197,7 @@ func UpdateTeacherPassword(c *fiber.Ctx) error {
findErr := studentCollection.FindOne(ctx, bson.M{"schooldata.tid": claims.Issuer}).Decode(&teacher) findErr := studentCollection.FindOne(ctx, bson.M{"schooldata.tid": claims.Issuer}).Decode(&teacher)
if findErr != nil { if findErr != nil {
cancel() cancel()
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false, "success": false,
"message": "teacher not found", "message": "teacher not found",
}) })
@@ -1216,7 +1216,7 @@ func UpdateTeacherPassword(c *fiber.Ctx) error {
cancel() cancel()
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"success": false, "success": false,
"message": "Your current password is incorrect", "message": "Your password is incorrect",
}) })
} }
@@ -1224,7 +1224,7 @@ func UpdateTeacherPassword(c *fiber.Ctx) error {
cancel() cancel()
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false, "success": false,
"message": "Your new password must match", "message": "Your new passwords must match",
}) })
} }
@@ -1296,7 +1296,7 @@ func ResetTeacherPassword(c *fiber.Ctx) error {
findErr := teacherCollection.FindOne(context.TODO(), bson.M{"schooldata.tid": data["tid"]}).Decode(&teacher) findErr := teacherCollection.FindOne(context.TODO(), bson.M{"schooldata.tid": data["tid"]}).Decode(&teacher)
if findErr != nil { if findErr != nil {
cancel() cancel()
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false, "success": false,
"message": "teacher not found", "message": "teacher not found",
}) })
@@ -1446,9 +1446,9 @@ func UpdateTeacherPhoto(c *fiber.Ctx) error {
findErr := teacherCollection.FindOne(context.TODO(), bson.M{"schooldata.tid": tid}).Decode(&teacher) findErr := teacherCollection.FindOne(context.TODO(), bson.M{"schooldata.tid": tid}).Decode(&teacher)
if findErr != nil { if findErr != nil {
cancel() cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false, "success": false,
"message": "the student could not be found", "message": "the teacher could not be found",
"error": findErr, "error": findErr,
}) })
} }
@@ -1632,7 +1632,7 @@ func UpdateTeacherName(c *fiber.Ctx) error {
teacherObjectId, idErr := primitive.ObjectIDFromHex(data["_id"]) teacherObjectId, idErr := primitive.ObjectIDFromHex(data["_id"])
if idErr != nil { if idErr != nil {
cancel() cancel()
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false, "success": false,
"message": "teacher not found", "message": "teacher not found",
"error": idErr, "error": idErr,
+1 -1
View File
@@ -11,7 +11,7 @@ import (
"github.com/gofiber/fiber/v2/middleware/cors" "github.com/gofiber/fiber/v2/middleware/cors"
) )
const version string = "\nv0.8.6-Alpha" const version string = "\nv0.8.7-Alpha"
func main() { func main() {
fmt.Println(version) fmt.Println(version)