diff --git a/internal/client/client.go b/internal/client/client.go new file mode 100644 index 0000000000000000000000000000000000000000..2785f33ec9d804434b9740163c2cece3824f2c24 --- /dev/null +++ b/internal/client/client.go @@ -0,0 +1,37 @@ +package client + +import ( + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + +type Client struct { + gorm.Model + ID uint `gorm:"primaryKey"` + Id_client string + Secret string +} + +var db *gorm.DB + +func Init() { + var err error + db, err = gorm.Open(sqlite.Open("glcpro.db"), &gorm.Config{}) + if err != nil { + panic("failed to connect database") + } + //Migrate the schema + db.AutoMigrate(&Client{}) +} + +func Exists(id string, secret string) bool { + var client Client + if err := db.Where("id_client = ? AND secret = ?", id, secret).First(&client).Error; err != nil { + return false + } + return true +} + +func Create(id string, secret string) { + db.Create(&Client{Id_client: id, Secret: secret}) +} diff --git a/internal/client/client_test.go b/internal/client/client_test.go new file mode 100644 index 0000000000000000000000000000000000000000..e42dba15e53a253d5f96eef66c3758ac04eb2573 --- /dev/null +++ b/internal/client/client_test.go @@ -0,0 +1,34 @@ +package client + +import ( + "testing" +) + +func TestExists(t *testing.T) { + Init() + db.Unscoped().Where("id_client = ?", "ID_CLIENT").Delete(Client{}) + Create("ID_CLIENT", "SECRET") + + type args struct { + id_client string + secret string + } + + tests := []struct { + name string + args args + want bool + }{ + {"exists", args{id_client: "ID_CLIENT", secret: "SECRET"}, true}, + {"not_exists_id_client", args{id_client: "NOTID_CLIENT", secret: "SECRET"}, false}, + {"false_secret", args{id_client: "ID_CLIENT", secret: "WRONGSECRET"}, false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Exists(tt.args.id_client, tt.args.secret); got != tt.want { + t.Errorf("Exists() = %v, want %v", got, tt.want) + } + }) + } +}