2019-01-06 01:53:33 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2019-01-09 06:33:30 +01:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2019-01-06 01:53:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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-09 05:30:15 +01:00
|
|
|
func addUser(_login string, _password string, _userDescription string) error {
|
2019-01-06 02:23:35 +01:00
|
|
|
//do rejestracji uzytkownika
|
|
|
|
// Insert do bazy Mysql Nowego użytkownika
|
|
|
|
|
2019-01-09 06:33:30 +01:00
|
|
|
password := []byte(_password) //zamiana stringa na bajty dla funckji hashujacej
|
2019-01-06 02:23:35 +01:00
|
|
|
|
2019-01-09 06:33:30 +01:00
|
|
|
db, err := connectMysql()
|
2019-01-06 02:23:35 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
2019-01-09 05:30:15 +01:00
|
|
|
return err
|
2019-01-06 02:23:35 +01:00
|
|
|
}
|
|
|
|
|
2019-01-09 06:33:30 +01:00
|
|
|
// Hashing the password with the default cost of 10
|
|
|
|
hashedPassword, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
|
|
|
|
encryptedPassword := string(hashedPassword)
|
|
|
|
|
2019-01-06 02:23:35 +01:00
|
|
|
queryInsert := fmt.Sprintf(`INSERT INTO users (
|
|
|
|
login,
|
|
|
|
password,
|
|
|
|
userDescription,
|
|
|
|
points
|
|
|
|
)VALUES (
|
|
|
|
"%s",
|
|
|
|
"%s",
|
|
|
|
"%s",
|
|
|
|
"%d"
|
2019-01-09 06:33:30 +01:00
|
|
|
)`, _login, encryptedPassword, _userDescription, 0) //przy rejestracji kzdy ma 0 punktow
|
2019-01-06 02:23:35 +01:00
|
|
|
|
|
|
|
fmt.Printf(queryInsert)
|
|
|
|
insert, err := db.Query(queryInsert)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
2019-01-09 05:30:15 +01:00
|
|
|
return err
|
2019-01-06 02:23:35 +01:00
|
|
|
}
|
|
|
|
fmt.Println(insert)
|
2019-01-09 05:30:15 +01:00
|
|
|
return nil
|
2019-01-06 02:23:35 +01:00
|
|
|
|
|
|
|
}
|
2019-01-06 01:53:33 +01:00
|
|
|
|
|
|
|
func getAllUsers() []User {
|
|
|
|
//pobiera wszystkich uzytkownikow - do wysiwetlenie na front
|
2019-01-09 07:26:21 +01:00
|
|
|
|
2019-01-06 01:53:33 +01:00
|
|
|
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-13 01:26:13 +01:00
|
|
|
err = result.Scan(&user.UserId, &user.Login, &user.Password, &user.UserDescription, &user.Points, &user.IsLogged)
|
2019-01-06 01:53:33 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
|
2019-01-09 02:29:12 +01:00
|
|
|
fmt.Println(user)
|
2019-01-06 01:53:33 +01:00
|
|
|
allUsers = append(allUsers, user)
|
|
|
|
}
|
|
|
|
|
2019-01-09 02:29:12 +01:00
|
|
|
// fmt.Println("All users: ", allUsers)
|
|
|
|
// fmt.Println(reflect.TypeOf(allUsers))
|
2019-01-06 01:53:33 +01:00
|
|
|
|
2019-01-09 02:29:12 +01:00
|
|
|
return allUsers
|
2019-01-06 01:53:33 +01:00
|
|
|
}
|
|
|
|
|
2019-01-09 05:30:15 +01:00
|
|
|
func checkLoginExists(_login string) (bool, error) {
|
2019-01-06 16:00:15 +01:00
|
|
|
//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())
|
2019-01-09 05:30:15 +01:00
|
|
|
return false, err
|
2019-01-06 16:00:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for result.Next() {
|
|
|
|
var userLogin string
|
|
|
|
|
|
|
|
err = result.Scan(&userLogin)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
2019-01-09 05:30:15 +01:00
|
|
|
return false, err
|
|
|
|
|
2019-01-06 16:00:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if userLogin != "" {
|
2019-01-09 05:30:15 +01:00
|
|
|
return true, nil
|
2019-01-06 16:00:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-09 05:30:15 +01:00
|
|
|
return false, nil
|
2019-01-06 16:00:15 +01:00
|
|
|
}
|
|
|
|
|
2019-01-09 05:30:15 +01:00
|
|
|
func loginUser(_login string, _password string) (bool, error) {
|
2019-01-06 16:00:15 +01:00
|
|
|
//do logowanie w bazie
|
|
|
|
db, err := connectMysql()
|
2019-01-09 06:33:30 +01:00
|
|
|
|
|
|
|
querySelect := fmt.Sprintf(`SELECT password FROM users WHERE login='%s' ;`, _login)
|
2019-01-06 16:00:15 +01:00
|
|
|
|
|
|
|
result, err := db.Query(querySelect)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
2019-01-09 05:30:15 +01:00
|
|
|
return false, err
|
2019-01-06 16:00:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for result.Next() {
|
2019-01-09 06:33:30 +01:00
|
|
|
var hashedPassword string
|
2019-01-06 16:00:15 +01:00
|
|
|
|
2019-01-09 06:33:30 +01:00
|
|
|
err = result.Scan(&hashedPassword)
|
2019-01-06 16:00:15 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
2019-01-09 05:30:15 +01:00
|
|
|
return false, err
|
2019-01-06 16:00:15 +01:00
|
|
|
}
|
|
|
|
|
2019-01-09 06:33:30 +01:00
|
|
|
// Comparing the password with the hash
|
|
|
|
err = bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(_password))
|
|
|
|
if err == nil { // nil means it is a match
|
2019-01-09 05:30:15 +01:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateUserPoints(_login string) error {
|
|
|
|
db, err := connectMysql()
|
|
|
|
var userPoints int
|
|
|
|
|
|
|
|
querySelectPoints := fmt.Sprintf(`SELECT points FROM users WHERE login='%s' ;`, _login)
|
|
|
|
|
|
|
|
result, err := db.Query(querySelectPoints)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for result.Next() { //pobieranie aktualnej liczby punktow uzytkownika
|
|
|
|
|
|
|
|
err = result.Scan(&userPoints)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
return err
|
2019-01-06 16:00:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-09 05:30:15 +01:00
|
|
|
queryUpdatePoints := fmt.Sprintf(`UPDATE users SET points = %d WHERE login ='%s' ;`, userPoints+1, _login)
|
|
|
|
result, err = db.Query(queryUpdatePoints)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil //jak bez bledow wszystko
|
2019-01-06 16:00:15 +01:00
|
|
|
}
|
2019-01-13 01:26:13 +01:00
|
|
|
|
|
|
|
func getAllLoggedUsers() ([]string, error) {
|
|
|
|
//pobieranie listy obiektow zalogowanych uzytkownikow
|
|
|
|
db, err := connectMysql()
|
|
|
|
loggedUsers := make([]string, 0)
|
|
|
|
|
|
|
|
result, err := db.Query("SELECT * FROM users WHERE isLogged = true;")
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
fmt.Println("[getAllLoggedUsers] [Error] %s", err)
|
|
|
|
|
|
|
|
return nil, err // zwraca nula jak nie uda sie pobrac listy loginow
|
|
|
|
}
|
|
|
|
|
|
|
|
for result.Next() {
|
|
|
|
var user User
|
|
|
|
|
|
|
|
err = result.Scan(&user.UserId, &user.Login, &user.Password, &user.UserDescription, &user.Points, &user.IsLogged)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
return nil, err // zwraca nula jak nie uda sie pobrac listy loginow
|
|
|
|
fmt.Println("[getAllLoggedUsers] [Error] %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(user)
|
|
|
|
loggedUsers = append(loggedUsers, user.Login)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Wszyscy zalogowani: ", loggedUsers)
|
|
|
|
return loggedUsers, nil // zwraca nula jak nie uda sie pobrac listy loginow
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateLoggedUser(_login string) error {
|
|
|
|
//aktualizuje stan uzytkownika na zalogowanego
|
|
|
|
db, err := connectMysql()
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
fmt.Println("[updateLoggedUser] [Error] %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
updateLogin := fmt.Sprintf(`UPDATE users SET isLogged=true WHERE login='%s' ;`, _login)
|
|
|
|
_, err = db.Query(updateLogin)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
fmt.Println("[getAllLoggedUsers] [Error] %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateLogoutUser(_login string) error {
|
|
|
|
//aktualizuje stan na false jak sie uzytkownik wyloguje
|
|
|
|
db, err := connectMysql()
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
fmt.Println("[updateLoggedUser] [Error] Nie udało polaczy sie z baza")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
updateLogout := fmt.Sprintf(`UPDATE users SET isLogged=false WHERE login='%s' ;`, _login)
|
2019-01-13 03:58:27 +01:00
|
|
|
result, err := db.Query(updateLogout)
|
|
|
|
fmt.Println("RESULT: ", result)
|
2019-01-13 01:26:13 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
fmt.Println("[getAllLoggedUsers] [Error] Nie udalo sie zaktulizowac wylogowanego uzytkownika")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|