[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() {
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)
// }

View File

@ -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")

View File

@ -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

View File

@ -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() {