From 1b643c0bc2b5ea09f0433156f6b10db2f89bca76 Mon Sep 17 00:00:00 2001 From: Braeden Sowinski Date: Sat, 13 Aug 2022 20:44:49 -0700 Subject: [PATCH] simplify code --- controllers/authController.go | 18 +++++++----------- controllers/emailController.go | 1 - controllers/idController.go | 10 ++-------- 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/controllers/authController.go b/controllers/authController.go index 43d10bc..34ca888 100644 --- a/controllers/authController.go +++ b/controllers/authController.go @@ -72,7 +72,7 @@ func CreateDefaultAdmin() models.Admin { var aid string for { aid = GenerateID(6) - if ValidateID(aid, 3) == true { + if ValidateID(aid, 3) { break } } @@ -113,9 +113,6 @@ func NewSystem() { var SecretKey = os.Getenv("secret") -var systemEmail string = os.Getenv("SYSTEM_EMAIL") -var systemPassword string = os.Getenv("SYSTEM_PASSWORD") - func AuthenticateUser(c *fiber.Ctx, userType int) (bool, string) { if userType < 1 || userType > 3 { log.Fatal("Invalid userType") @@ -240,7 +237,7 @@ func Enroll(c *fiber.Ctx) error { var sid string for { sid = GenerateID(6) - if ValidateID(sid, 1) == true { + if ValidateID(sid, 1) { break } } @@ -261,7 +258,7 @@ func Enroll(c *fiber.Ctx) error { var pen string for { pen = GenerateID(9) - if ValidatePEN(pen) == true { + if ValidatePEN(pen) { break } } @@ -387,8 +384,7 @@ func RegisterTeacher(c *fiber.Ctx) error { // For the unlikely event that an ID is already in use this will simply try again till it gets a id not in use for { tid = GenerateID(6) - isValid := ValidateID(tid, 2) - if isValid == true { + if ValidateID(tid, 2) { break } } @@ -485,7 +481,7 @@ func CreateAdmin(c *fiber.Ctx) error { var aid string for { aid = GenerateID(6) - if ValidateID(aid, 3) == true { + if ValidateID(aid, 3) { break } } @@ -733,7 +729,7 @@ func TeacherLogin(c *fiber.Ctx) error { defer cancel() var verified bool = teacher.ComparePasswords(data["password"]) - if verified == false { + if !verified { return c.Status(fiber.StatusOK).JSON(fiber.Map{ "success": false, "message": "incorrect password", @@ -803,7 +799,7 @@ func AdminLogin(c *fiber.Ctx) error { defer cancel() var verified bool = admin.ComparePasswords(data["password"]) - if verified == false { + if !verified { return c.Status(fiber.StatusOK).JSON(fiber.Map{ "success": false, "message": "incorrect password", diff --git a/controllers/emailController.go b/controllers/emailController.go index f82f787..19ec143 100644 --- a/controllers/emailController.go +++ b/controllers/emailController.go @@ -24,7 +24,6 @@ var config Config = Config{ } type Request struct { - from string to []string subject string body string diff --git a/controllers/idController.go b/controllers/idController.go index 9b2f740..2f8b349 100644 --- a/controllers/idController.go +++ b/controllers/idController.go @@ -29,10 +29,7 @@ func ValidateID(id string, userType int) bool { // true: valid id, false: id alr newID.ParentType = userType newID.ID = primitive.NewObjectID() _, insertErr := idCollection.InsertOne(context.Background(), newID) - if insertErr != nil { - return false - } - return true + return insertErr == nil } return false } @@ -42,10 +39,7 @@ func ValidatePEN(pen string) bool { // true: valid pen, false: pen already in us var foundID models.Id err := studentCollection.FindOne(ctx, bson.M{"schooldata.pen": pen}).Decode(&foundID) cancel() - if err != nil { - return true - } - return false + return err != nil } func GenerateID(length int) string {