From 13cb0bde26de1ea22397ab8f63c810aab82082ae Mon Sep 17 00:00:00 2001 From: Braeden Sowinski Date: Sun, 22 May 2022 12:25:45 -0700 Subject: [PATCH] proper / consitent status responses --- controllers/authController.go | 36 +++++++++++++++---------------- controllers/updateController.go | 38 ++++++++++++++++----------------- main.go | 2 +- 3 files changed, 38 insertions(+), 38 deletions(-) diff --git a/controllers/authController.go b/controllers/authController.go index f799d77..5f36ec5 100644 --- a/controllers/authController.go +++ b/controllers/authController.go @@ -134,7 +134,7 @@ func Enroll(c *fiber.Ctx) error { if err := c.BodyParser(&data); err != nil { cancel() - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "success": false, "message": "Failed to parse body", "error": err, @@ -263,7 +263,7 @@ func Enroll(c *fiber.Ctx) error { } defer cancel() - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + return c.Status(fiber.StatusOK).JSON(fiber.Map{ "success": true, "message": "successfully inserted student", }) @@ -501,7 +501,7 @@ func StudentLogin(c *fiber.Ctx) error { if err != nil { cancel() - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + return c.Status(fiber.StatusOK).JSON(fiber.Map{ "success": false, "message": "student not found", "error": err, @@ -537,7 +537,7 @@ func StudentLogin(c *fiber.Ctx) error { if localAccountDisabled || student.AccountData.AccountDisabled { cancel() - return c.Status(fiber.StatusForbidden).JSON(fiber.Map{ + return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, "message": "Account is Disabled, contact an Admin", }) @@ -566,7 +566,7 @@ func StudentLogin(c *fiber.Ctx) error { "error": updateErr, }) } - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, "message": "incorrect password", }) @@ -627,9 +627,9 @@ func TeacherLogin(c *fiber.Ctx) error { if err != nil { cancel() - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + return c.Status(fiber.StatusOK).JSON(fiber.Map{ "success": false, - "message": "student not found", + "message": "teacher not found", "error": err, }) } @@ -637,7 +637,7 @@ func TeacherLogin(c *fiber.Ctx) error { var verified bool = teacher.ComparePasswords(data["password"]) if verified == false { - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, "message": "incorrect password", }) @@ -697,7 +697,7 @@ func AdminLogin(c *fiber.Ctx) error { if err != nil { cancel() - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + return c.Status(fiber.StatusOK).JSON(fiber.Map{ "success": false, "message": "admin not found", "error": err, @@ -707,7 +707,7 @@ func AdminLogin(c *fiber.Ctx) error { var verified bool = admin.ComparePasswords(data["password"]) if verified == false { - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, "message": "incorrect password", }) @@ -787,7 +787,7 @@ func Student(c *fiber.Ctx) error { var student models.Student findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": sid}).Decode(&student) if findErr != nil { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "message": "student not found", }) @@ -834,7 +834,7 @@ func Teacher(c *fiber.Ctx) error { return []byte(SecretKey), nil }) if err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, "message": "not authorized", }) @@ -845,13 +845,13 @@ func Teacher(c *fiber.Ctx) error { var teacher models.Teacher findErr := teacherCollection.FindOne(context.TODO(), bson.M{"schooldata.tid": claims.Issuer}).Decode(&teacher) if findErr != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "message": "teacher not found", }) } - return c.Status(fiber.StatusAccepted).JSON(fiber.Map{ + return c.Status(fiber.StatusOK).JSON(fiber.Map{ "success": true, "message": "successfully logged into teacher", "result": teacher, @@ -876,13 +876,13 @@ func Admin(c *fiber.Ctx) error { var admin models.Admin findErr := adminCollection.FindOne(context.TODO(), bson.M{"aid": claims.Issuer}).Decode(&admin) if findErr != nil { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "message": "admin not found", }) } - return c.Status(fiber.StatusAccepted).JSON(fiber.Map{ + return c.Status(fiber.StatusOK).JSON(fiber.Map{ "success": true, "message": "successfully logged into admin", "result": admin, @@ -1024,7 +1024,7 @@ func DeleteContact(c *fiber.Ctx) error { _, err := contactCollection.DeleteOne(ctx, bson.M{"_id": data["id"]}) if err != nil { cancel() - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "success": false, "message": "Failed to delete object", "error": err, @@ -1032,7 +1032,7 @@ func DeleteContact(c *fiber.Ctx) error { } defer cancel() - return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ + return c.Status(fiber.StatusOK).JSON(fiber.Map{ "success": true, "message": "Successfully deleted contact", }) diff --git a/controllers/updateController.go b/controllers/updateController.go index 14ed9f6..d634b79 100644 --- a/controllers/updateController.go +++ b/controllers/updateController.go @@ -273,7 +273,7 @@ func UpdateStudentPassword(c *fiber.Ctx) error { findErr := studentCollection.FindOne(ctx, bson.M{"schooldata.sid": claims.Issuer}).Decode(&student) if findErr != nil { cancel() - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "message": "student not found", }) @@ -292,7 +292,7 @@ func UpdateStudentPassword(c *fiber.Ctx) error { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "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) if findErr != nil { cancel() - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "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) if err != nil { cancel() - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "message": "locker not found", "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 { var data map[string]string 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) if findErr != nil { cancel() - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "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) if err != nil { cancel() - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "message": "contact not found", "error": err, @@ -750,7 +750,7 @@ func AddStudentContact(c *fiber.Ctx) error { err := contactCollection.FindOne(ctx, bson.M{"_id": data["contactid"]}).Decode(&contact) if err != nil { cancel() - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "message": "contact not found", "error": err, @@ -815,7 +815,7 @@ func UpdateStudentPhoto(c *fiber.Ctx) error { findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": sid}).Decode(&student) if findErr != nil { cancel() - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "message": "the student could not be found", "error": findErr, @@ -1026,7 +1026,7 @@ func RemoveStudentsDisabled(c *fiber.Ctx) error { return c.Status(fiber.StatusOK).JSON(fiber.Map{ "success": true, - "message": "successfully re-enabled student account", + "message": "successfully enabled student account", "result": result, }) } @@ -1080,7 +1080,7 @@ func RemoveTeachersDisabled(c *fiber.Ctx) error { cancel() return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "success": false, - "message": "the student account could not be re-enabled", + "message": "the teacher account could not be enabled", "error": updateErr, }) } @@ -1088,7 +1088,7 @@ func RemoveTeachersDisabled(c *fiber.Ctx) error { return c.Status(fiber.StatusOK).JSON(fiber.Map{ "success": true, - "message": "successfully re-enabled teacher account", + "message": "successfully enabled teacher account", "result": result, }) } @@ -1197,7 +1197,7 @@ func UpdateTeacherPassword(c *fiber.Ctx) error { findErr := studentCollection.FindOne(ctx, bson.M{"schooldata.tid": claims.Issuer}).Decode(&teacher) if findErr != nil { cancel() - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "message": "teacher not found", }) @@ -1216,7 +1216,7 @@ func UpdateTeacherPassword(c *fiber.Ctx) error { cancel() return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ "success": false, - "message": "Your current password is incorrect", + "message": "Your password is incorrect", }) } @@ -1224,7 +1224,7 @@ func UpdateTeacherPassword(c *fiber.Ctx) error { cancel() return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "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) if findErr != nil { cancel() - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "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) if findErr != nil { cancel() - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, - "message": "the student could not be found", + "message": "the teacher could not be found", "error": findErr, }) } @@ -1632,7 +1632,7 @@ func UpdateTeacherName(c *fiber.Ctx) error { teacherObjectId, idErr := primitive.ObjectIDFromHex(data["_id"]) if idErr != nil { cancel() - return c.Status(fiber.StatusNotFound).JSON(fiber.Map{ + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "success": false, "message": "teacher not found", "error": idErr, diff --git a/main.go b/main.go index a174684..1daedb4 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ import ( "github.com/gofiber/fiber/v2/middleware/cors" ) -const version string = "\nv0.8.6-Alpha" +const version string = "\nv0.8.7-Alpha" func main() { fmt.Println(version)