2019-01-06 01:53:33 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
)
|
|
|
|
|
|
|
|
func connectMysql() (*sql.DB, error) {
|
|
|
|
db, err := sql.Open("mysql", "root:password@tcp(127.0.0.1:3306)/golang_user")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2019-01-06 02:23:35 +01:00
|
|
|
func addUser(_login string, _password string, _userDescription string, _points int) {
|
|
|
|
//do rejestracji uzytkownika
|
|
|
|
// Insert do bazy Mysql Nowego użytkownika
|
|
|
|
|
|
|
|
db, err := connectMysql()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
queryInsert := fmt.Sprintf(`INSERT INTO users (
|
|
|
|
login,
|
|
|
|
password,
|
|
|
|
userDescription,
|
|
|
|
points
|
|
|
|
)VALUES (
|
|
|
|
"%s",
|
|
|
|
"%s",
|
|
|
|
"%s",
|
|
|
|
"%d"
|
|
|
|
)`, _login, _password, _userDescription, _points)
|
|
|
|
|
|
|
|
fmt.Printf(queryInsert)
|
|
|
|
insert, err := db.Query(queryInsert)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
fmt.Println(insert)
|
|
|
|
|
|
|
|
}
|
2019-01-06 01:53:33 +01:00
|
|
|
|
|
|
|
func getAllUsers() []User {
|
|
|
|
//pobiera wszystkich uzytkownikow - do wysiwetlenie na front
|
|
|
|
/* --------------------- users -----------------------
|
|
|
|
userId int `json:"id"` // do pobierania z GET
|
|
|
|
login string `json:"login"`
|
|
|
|
password string `json:"password"`
|
|
|
|
userDescription string `json:"userDescription"`
|
|
|
|
points int `json:points`
|
|
|
|
*/
|
|
|
|
allUsers := []User{}
|
|
|
|
db, err := connectMysql()
|
|
|
|
|
|
|
|
result, err := db.Query("SELECT * FROM users")
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
for result.Next() {
|
|
|
|
var user User
|
|
|
|
|
2019-01-06 02:23:35 +01:00
|
|
|
err = result.Scan(&user.userId, &user.login, &user.password, &user.userDescription, &user.points)
|
2019-01-06 01:53:33 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
allUsers = append(allUsers, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
return allUsers
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-01-06 16:00:15 +01:00
|
|
|
func checkLoginExists(_login string) bool {
|
|
|
|
//sprawdza czy dany login jest juz w bazie, przy rejestracji przydatne
|
|
|
|
db, err := connectMysql()
|
|
|
|
|
|
|
|
querySelect := fmt.Sprintf(`SELECT login FROM users WHERE login='%s' ;`, _login)
|
|
|
|
|
|
|
|
result, err := db.Query(querySelect)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
for result.Next() {
|
|
|
|
var userLogin string
|
|
|
|
|
|
|
|
err = result.Scan(&userLogin)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if userLogin != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func loginUser(_login string, _password string) bool {
|
|
|
|
//do logowanie w bazie
|
|
|
|
db, err := connectMysql()
|
|
|
|
querySelect := fmt.Sprintf(`SELECT login FROM users WHERE login='%s' AND password='%s' ;`, _login, _password)
|
|
|
|
|
|
|
|
result, err := db.Query(querySelect)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
for result.Next() {
|
|
|
|
var userLogin string
|
|
|
|
|
|
|
|
err = result.Scan(&userLogin)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if userLogin != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-01-06 01:53:33 +01:00
|
|
|
func main() {
|
|
|
|
fmt.Println("Go Mysql Tutorial")
|
|
|
|
// db, err := connectMysql()
|
|
|
|
|
|
|
|
// // db, err := sql.Open("mysql", "root:password@tcp(127.0.0.1:3306)/golang_user")
|
|
|
|
// fmt.Println(reflect.TypeOf(db))
|
|
|
|
// fmt.Println(reflect.TypeOf(err))
|
2019-01-06 02:23:35 +01:00
|
|
|
|
2019-01-06 16:00:15 +01:00
|
|
|
// addUser("login10", "password", "userDescription", 123)
|
|
|
|
// allUsers := getAllUsers()
|
|
|
|
// fmt.Println(allUsers)
|
|
|
|
wynik := checkLoginExists("_login")
|
|
|
|
fmt.Printf("%v", wynik)
|
|
|
|
wynik = loginUser("_login", "_password3")
|
|
|
|
fmt.Printf("%v", wynik)
|
2019-01-06 02:23:35 +01:00
|
|
|
|
2019-01-06 01:53:33 +01:00
|
|
|
}
|