Register endpoint working
This commit is contained in:
@ -68,8 +68,6 @@ func GetDatabaseWithName(companyName string) (*sql.DB, error) {
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// TODO: call upper function in this one to reduce code
|
||||
|
||||
// GetDatabase Get orm and user information
|
||||
func GetDatabase(tokenString string) (jwt.MapClaims, *sql.DB, error) {
|
||||
// validate token
|
||||
@ -86,9 +84,7 @@ func GetDatabase(tokenString string) (jwt.MapClaims, *sql.DB, error) {
|
||||
return tokenMap, dbs[companyName], nil
|
||||
}
|
||||
|
||||
conStr := fmt.Sprintf("host=127.0.0.1 port=5435 user=postgres password=postgre dbname=%s sslmode=disable", companyName)
|
||||
fmt.Println(conStr)
|
||||
db, err := sql.Open("postgres", conStr)
|
||||
db, err := GetDatabaseWithName(companyName)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@ -97,36 +93,63 @@ func GetDatabase(tokenString string) (jwt.MapClaims, *sql.DB, error) {
|
||||
return tokenMap, db, nil
|
||||
}
|
||||
|
||||
// CreateDatabase Create a database by copying the template
|
||||
func CreateDatabase(companyName string) {
|
||||
/*
|
||||
if dbs[companyName] != nil {
|
||||
fmt.Println("DB Already open")
|
||||
return dbs[companyName], nil
|
||||
}
|
||||
/*
|
||||
db, err = sql.Open("postgres", "host=127.0.0.1 port=5435 user=postgres password=postgre dbname=company5 sslmode=disable")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// HasDatabase Check if DB exists
|
||||
func HasDatabase(dbname string) bool {
|
||||
systemDB := GetSystemDatabase()
|
||||
result, err := systemDB.Query("SELECT datname FROM pg_database WHERE datistemplate = false;")
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
or, err := orm.NewOrmWithDB("postgres", "temp", db)
|
||||
*/
|
||||
for result.Next() {
|
||||
var aDbName string
|
||||
result.Scan(&aDbName)
|
||||
if aDbName == dbname {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// CreateDatabase Create a database by copying the template
|
||||
func CreateDatabase(companyName string) (*sql.DB, error) {
|
||||
if HasDatabase(companyName) {
|
||||
return nil, errors.New("DB already exists")
|
||||
}
|
||||
|
||||
systemDB := GetSystemDatabase()
|
||||
// Takes about 1.2 seconds and we trust companyName to be sanitized in register
|
||||
queryString := fmt.Sprintf("CREATE DATABASE %s TEMPLATE company_template;", companyName)
|
||||
_, err := systemDB.Exec(queryString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
db, err := GetDatabaseWithName(companyName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// DeleteDatabase Delete an entire database, this is very very dangerous :-)
|
||||
func DeleteDatabase(token string) {
|
||||
//_, db, err := GetDatabase(token)
|
||||
/*
|
||||
remove from map!
|
||||
db.Close()
|
||||
fmt.Println("Closed company5")
|
||||
//}
|
||||
func DeleteDatabase(companyName string) error {
|
||||
if !HasDatabase(companyName) {
|
||||
return errors.New("DB does not exist")
|
||||
}
|
||||
systemDB := GetSystemDatabase()
|
||||
|
||||
res, err := o.Raw("DROP DATABASE company5;").Exec()
|
||||
if err == nil {
|
||||
num, _ := res.RowsAffected()
|
||||
fmt.Println("mysql row affected number: ", num)
|
||||
}
|
||||
*/
|
||||
db, err := GetDatabaseWithName(companyName)
|
||||
db.Close()
|
||||
delete(dbs, companyName)
|
||||
fmt.Println("Closed %s", companyName)
|
||||
|
||||
queryString := fmt.Sprintf("DROP DATABASE %s;", companyName)
|
||||
_, err = systemDB.Exec(queryString)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user