PracowniaProgramowania/backend/connector_mysql.go

185 lines
3.9 KiB
Go
Raw Normal View History

package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"golang.org/x/crypto/bcrypt"
)
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
}
func addUser(_login string, _password string, _userDescription string) error {
//do rejestracji uzytkownika
// Insert do bazy Mysql Nowego użytkownika
password := []byte(_password) //zamiana stringa na bajty dla funckji hashujacej
db, err := connectMysql()
if err != nil {
panic(err.Error())
return err
}
// Hashing the password with the default cost of 10
hashedPassword, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
encryptedPassword := string(hashedPassword)
queryInsert := fmt.Sprintf(`INSERT INTO users (
login,
password,
userDescription,
points
)VALUES (
"%s",
"%s",
"%s",
"%d"
)`, _login, encryptedPassword, _userDescription, 0) //przy rejestracji kzdy ma 0 punktow
fmt.Printf(queryInsert)
insert, err := db.Query(queryInsert)
if err != nil {
panic(err.Error())
return err
}
fmt.Println(insert)
return nil
}
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
err = result.Scan(&user.UserId, &user.Login, &user.Password, &user.UserDescription, &user.Points)
if err != nil {
panic(err.Error())
}
2019-01-09 02:29:12 +01:00
fmt.Println(user)
allUsers = append(allUsers, user)
}
2019-01-09 02:29:12 +01:00
// fmt.Println("All users: ", allUsers)
// fmt.Println(reflect.TypeOf(allUsers))
2019-01-09 02:29:12 +01:00
return allUsers
}
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())
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())
return false, err
2019-01-06 16:00:15 +01:00
}
if userLogin != "" {
return true, nil
2019-01-06 16:00:15 +01:00
}
}
return false, nil
2019-01-06 16:00: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()
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())
return false, err
2019-01-06 16:00:15 +01:00
}
for result.Next() {
var hashedPassword string
2019-01-06 16:00:15 +01:00
err = result.Scan(&hashedPassword)
2019-01-06 16:00:15 +01:00
if err != nil {
panic(err.Error())
return false, err
2019-01-06 16:00:15 +01:00
}
// Comparing the password with the hash
err = bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(_password))
if err == nil { // nil means it is a match
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
}
}
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
}