set parentType in idManager.go

This commit is contained in:
SowinskiBraeden committed 2022-06-07 11:14:00 -07:00
1 parent eb065c4142
commit 27eb0937c7
2 files changed
+6 -5

No files matched your search

+4 -4
View File
@@ -69,7 +69,7 @@ func CreateDefaultAdmin() models.Admin {
var aid string var aid string
for { for {
aid = GenerateID(6) aid = GenerateID(6)
if ValidateID(aid) == true { if ValidateID(aid, 3) == true {
break break
} }
} }
@@ -237,7 +237,7 @@ func Enroll(c *fiber.Ctx) error {
var sid string var sid string
for { for {
sid = GenerateID(6) sid = GenerateID(6)
if ValidateID(sid) == true { if ValidateID(sid, 1) == true {
break break
} }
} }
@@ -372,7 +372,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 the unlikely event that an ID is already in use this will simply try again till it gets a id not in use
for { for {
tid = GenerateID(6) tid = GenerateID(6)
isValid := ValidateID(tid) isValid := ValidateID(tid, 2)
if isValid == true { if isValid == true {
break break
} }
@@ -458,7 +458,7 @@ func CreateAdmin(c *fiber.Ctx) error {
var aid string var aid string
for { for {
aid = GenerateID(6) aid = GenerateID(6)
if ValidateID(aid) == true { if ValidateID(aid, 3) == true {
break break
} }
} }
+2 -1
View File
@@ -18,7 +18,7 @@ var idCollection *mongo.Collection = database.OpenCollection(database.Client, "c
var table = [...]byte{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'} var table = [...]byte{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}
func ValidateID(id string) bool { // true: valid id, false: id already in use func ValidateID(id string, userType int) bool { // true: valid id, false: id already in use
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
var foundID models.Id var foundID models.Id
err := idCollection.FindOne(ctx, bson.M{"cid": id}).Decode(&foundID) err := idCollection.FindOne(ctx, bson.M{"cid": id}).Decode(&foundID)
@@ -26,6 +26,7 @@ func ValidateID(id string) bool { // true: valid id, false: id already in use
if err != nil { // If there is no id found create new ID object to be stored and return true (unless insert error then try again) if err != nil { // If there is no id found create new ID object to be stored and return true (unless insert error then try again)
var newID models.Id var newID models.Id
newID.CID = id newID.CID = id
newID.ParentType = userType
newID.ID = primitive.NewObjectID() newID.ID = primitive.NewObjectID()
_, insertErr := idCollection.InsertOne(context.Background(), newID) _, insertErr := idCollection.InsertOne(context.Background(), newID)
if insertErr != nil { if insertErr != nil {