Last active
October 4, 2020 22:58
-
-
Save smartass08/7d86c12c8e7b1182a18bb5960f0fe4ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"context" | |
"fmt" | |
"go.mongodb.org/mongo-driver/bson" | |
"go.mongodb.org/mongo-driver/mongo" | |
"go.mongodb.org/mongo-driver/mongo/options" | |
"log" | |
"sync" | |
"time" | |
) | |
var wg sync.WaitGroup | |
var ID []int64 | |
type DB struct { | |
client *mongo.Client | |
} | |
func (C *DB) Access(url string) { | |
log.Println("Starting DB process") | |
client, err := mongo.NewClient(options.Client().ApplyURI(url)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) | |
defer cancel() | |
err = client.Connect(ctx) | |
if err != nil { | |
log.Println("Error while connecting DB", err) | |
return | |
} | |
C.client = client | |
} | |
func (C *DB) GetAllID() { | |
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second) | |
defer cancel() | |
collection := C.client.Database("MirrorBot").Collection("ID") | |
all, err := collection.Find(ctx, bson.D{}) | |
if err != nil { | |
fmt.Println(err) | |
} | |
defer all.Close(ctx) | |
for all.Next(ctx) { | |
var result bson.M | |
err = all.Decode(&result) | |
if err != nil { | |
log.Println(err) | |
} else { | |
if result["id"] != nil { | |
id := ParseInterfaceToInt(result["id"]) | |
ID = append(ID, int64(id)) | |
} | |
} | |
} | |
} | |
func (C *DB) Insert(id int64) { | |
ID = append(ID, id) | |
wg.Add(1) | |
go func() { | |
collection := C.client.Database("MirrorBot").Collection("ID") | |
j := bson.M{"id": id} | |
_, err := collection.InsertOne(context.Background(), j) | |
if err != nil { | |
log.Printf("Error inserting the id into db %s", err) | |
return | |
} | |
}() | |
} | |
func CheckValid(id int64) bool { | |
for _, i := range ID { | |
if i == id { | |
return true | |
} | |
} | |
return false | |
} | |
func getIdIndex(userId int64) (int, bool) { | |
for i, j := range ID { | |
if j == userId { | |
return i, true | |
} | |
} | |
return 0, false | |
} | |
func (C *DB) Delete(id int64) { | |
defer wg.Done() | |
index, found := getIdIndex(id) | |
if found { | |
ID[index] = ID[len(ID)-1] | |
ID[len(ID)-1] = 0 | |
ID = ID[:len(ID)-1] | |
} | |
wg.Add(1) | |
go func() { | |
collection := C.client.Database("MirrorBot").Collection("ID") | |
_, err := collection.DeleteOne(context.Background(), bson.M{"id": id}) | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
}() | |
} | |
func ParseInterfaceToInt(i interface{}) int { | |
tempType := reflect.TypeOf(i).Name() | |
if tempType == "int32" { | |
return int(i.(int32)) | |
} | |
return int(i.(int64)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment