From 711d9cc9ba4eeccf6a5a7ab3c89c707c126f87fd Mon Sep 17 00:00:00 2001 From: pawlaczyk Date: Wed, 9 Jan 2019 03:55:36 +0100 Subject: [PATCH] [GIN request.Body]Dodawania uzytkownikow do bazy z rest api --- backend/connector_mysql.go | 20 +------------------- backend/main.go | 5 ++--- backend/models.go | 10 +++++----- backend/views.go | 30 +++++++++++++++++++----------- 4 files changed, 27 insertions(+), 38 deletions(-) diff --git a/backend/connector_mysql.go b/backend/connector_mysql.go index 0288dd2..6df3dda 100644 --- a/backend/connector_mysql.go +++ b/backend/connector_mysql.go @@ -67,7 +67,7 @@ func getAllUsers() []User { for result.Next() { var user User - err = result.Scan(&user.userId, &user.login, &user.password, &user.userDescription, &user.points) + err = result.Scan(&user.UserId, &user.Login, &user.Password, &user.UserDescription, &user.Points) if err != nil { panic(err.Error()) } @@ -134,21 +134,3 @@ func loginUser(_login string, _password string) bool { return false } - -// 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)) - -// // 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) - -// } diff --git a/backend/main.go b/backend/main.go index ae0c60d..91025a1 100644 --- a/backend/main.go +++ b/backend/main.go @@ -57,9 +57,8 @@ func main() { // /jokes/like/:jokeID - which will capture likes sent to a particular joke api.GET("/jokes", JokeHandler) api.POST("/jokes/like/:jokeID", LikeJoke) - api.GET("/getUsersView", getUsersView) //widok pobrania wszystkich użytkowników - - // addUser(_login string, _password string, _userDescription string, _points int) + api.GET("/getUsersView", getUsersView) //widok pobrania wszystkich użytkowników + api.POST("/addNewUserView", addNewUserView) // json z danymi nowego uzytkownika // Start and run the server router.Run(":3000") diff --git a/backend/models.go b/backend/models.go index 1008953..9774b43 100644 --- a/backend/models.go +++ b/backend/models.go @@ -15,11 +15,11 @@ CREATE TABLE users ( ); */ type User struct { - userId int `json:"id"` // do pobierania z GET - login string `json:"login"` - password string `json:"password"` - userDescription string `json:"userDescription"` - points int `json:points` + UserId int `json:"userId"` // do pobierania z GET + Login string `json:"login"` + Password string `json:"password"` + UserDescription string `json:"userDescription"` + Points int `json:points` } // Structura karty diff --git a/backend/views.go b/backend/views.go index dcecef0..ab7eb06 100644 --- a/backend/views.go +++ b/backend/views.go @@ -7,11 +7,6 @@ import ( "github.com/gin-gonic/gin" ) -// func JokeHandler(c *gin.Context) { -// c.Header("Content-Type", "application/json") -// c.JSON(http.StatusOK, jokes) -// } - func getUsersView(c *gin.Context) { // dodanie nowej karty do bzy fmt.Println("Dodanie do couchDB nowej karty pytania lub odowiedzi") @@ -21,9 +16,9 @@ func getUsersView(c *gin.Context) { allUsers := getAllUsers() for _, arg := range allUsers { tmp := make(map[string]interface{}) - tmp["login"] = arg.login - tmp["userDescription"] = arg.userDescription - tmp["points"] = arg.points + tmp["login"] = arg.Login + tmp["userDescription"] = arg.UserDescription + tmp["points"] = arg.Points userList = append(userList, tmp) } @@ -34,9 +29,22 @@ func getUsersView(c *gin.Context) { }) } -func addNewUserView() { - // dodanie nowego użytkownika - fmt.Println("Rejestracja nowego użytkownika") +func addNewUserView(c *gin.Context) { + // Read the Body content + var newUser User + c.Bind(&newUser) + + fmt.Println(newUser.Login) + _login := newUser.Login + _password := newUser.Password + _userDescription := newUser.UserDescription + _points := newUser.Points + + addUser(_login, _password, _userDescription, _points) + + fmt.Println("PUSTE BODY : <") + + c.JSON(http.StatusOK, "ala") } func loginUserView() {