fix cid checks\
This commit is contained in:
1 parent
dea4654cb1
commit
ff65d9eadc
2 files changed
+6
-4
No files matched your search
@@ -172,7 +172,8 @@ func RegisterTeacher(c *fiber.Ctx) error {
|
|||||||
var tid string
|
var tid string
|
||||||
for {
|
for {
|
||||||
tid = GenerateID()
|
tid = GenerateID()
|
||||||
if ValidateID(tid) == true {
|
isValid := ValidateID(tid)
|
||||||
|
if isValid == true {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,13 +19,14 @@ 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) 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)
|
||||||
err := idCollection.FindOne(ctx, bson.M{"cid": id})
|
var foundID models.Id
|
||||||
|
err := idCollection.FindOne(ctx, bson.M{"cid": id}).Decode(&foundID)
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil && err.Err() == mongo.ErrNoDocuments {
|
if err != nil {
|
||||||
var newID models.Id
|
var newID models.Id
|
||||||
newID.CID = id
|
newID.CID = id
|
||||||
newID.ID = primitive.NewObjectID()
|
newID.ID = primitive.NewObjectID()
|
||||||
_, insertErr := idCollection.InsertOne(ctx, newID)
|
_, insertErr := idCollection.InsertOne(context.Background(), newID)
|
||||||
if insertErr != nil {
|
if insertErr != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user