[GIN request.Body]Dodawania uzytkownikow do bazy z rest api

This commit is contained in:
pawlaczyk 2019-01-09 03:55:36 +01:00
parent 6c5e9d9223
commit 711d9cc9ba
4 changed files with 27 additions and 38 deletions

View File

@ -67,7 +67,7 @@ func getAllUsers() []User {
for result.Next() { for result.Next() {
var user User 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 { if err != nil {
panic(err.Error()) panic(err.Error())
} }
@ -134,21 +134,3 @@ func loginUser(_login string, _password string) bool {
return false 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)
// }

View File

@ -58,8 +58,7 @@ func main() {
api.GET("/jokes", JokeHandler) api.GET("/jokes", JokeHandler)
api.POST("/jokes/like/:jokeID", LikeJoke) api.POST("/jokes/like/:jokeID", LikeJoke)
api.GET("/getUsersView", getUsersView) //widok pobrania wszystkich użytkowników api.GET("/getUsersView", getUsersView) //widok pobrania wszystkich użytkowników
api.POST("/addNewUserView", addNewUserView) // json z danymi nowego uzytkownika
// addUser(_login string, _password string, _userDescription string, _points int)
// Start and run the server // Start and run the server
router.Run(":3000") router.Run(":3000")

View File

@ -15,11 +15,11 @@ CREATE TABLE users (
); );
*/ */
type User struct { type User struct {
userId int `json:"id"` // do pobierania z GET UserId int `json:"userId"` // do pobierania z GET
login string `json:"login"` Login string `json:"login"`
password string `json:"password"` Password string `json:"password"`
userDescription string `json:"userDescription"` UserDescription string `json:"userDescription"`
points int `json:points` Points int `json:points`
} }
// Structura karty // Structura karty

View File

@ -7,11 +7,6 @@ import (
"github.com/gin-gonic/gin" "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) { func getUsersView(c *gin.Context) {
// dodanie nowej karty do bzy // dodanie nowej karty do bzy
fmt.Println("Dodanie do couchDB nowej karty pytania lub odowiedzi") fmt.Println("Dodanie do couchDB nowej karty pytania lub odowiedzi")
@ -21,9 +16,9 @@ func getUsersView(c *gin.Context) {
allUsers := getAllUsers() allUsers := getAllUsers()
for _, arg := range allUsers { for _, arg := range allUsers {
tmp := make(map[string]interface{}) tmp := make(map[string]interface{})
tmp["login"] = arg.login tmp["login"] = arg.Login
tmp["userDescription"] = arg.userDescription tmp["userDescription"] = arg.UserDescription
tmp["points"] = arg.points tmp["points"] = arg.Points
userList = append(userList, tmp) userList = append(userList, tmp)
} }
@ -34,9 +29,22 @@ func getUsersView(c *gin.Context) {
}) })
} }
func addNewUserView() { func addNewUserView(c *gin.Context) {
// dodanie nowego użytkownika // Read the Body content
fmt.Println("Rejestracja nowego użytkownika") 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() { func loginUserView() {