package main import ( "fmt" "net/http" "github.com/gin-gonic/gin" ) func getUsersView(c *gin.Context) { // dodanie nowej karty do bzy c.Header("Content-Type", "application/json") fmt.Println("Dodanie do couchDB nowej karty pytania lub odowiedzi") var userList []map[string]interface{} allUsers := getAllUsers() for _, arg := range allUsers { tmp := make(map[string]interface{}) tmp["login"] = arg.Login tmp["userDescription"] = arg.UserDescription tmp["points"] = arg.Points userList = append(userList, tmp) } c.JSON(http.StatusOK, gin.H{ "code": http.StatusOK, "allUsers": userList, // cast it to string before showing }) } func addNewUserView(c *gin.Context) { //Dane z frontu - json z danymi uzytkownika, bedzie jeden //json walidacja na froncie - tu nie musze walidowac i elo // dobra, jednak waliduje c.Header("Content-Type", "application/json") var newUser User c.Bind(&newUser) fmt.Println(newUser.Login) _login := newUser.Login _password := newUser.Password _userDescription := newUser.UserDescription if _login == "" { c.JSON(http.StatusOK, "[addNewUserView][Error] Nie podano loginu") return } if _password == "" { c.JSON(http.StatusOK, "[addNewUserView][Error] Nie podano hasła") return } isExists, err := checkLoginExists(_login) if err != nil { c.JSON(http.StatusOK, "[addNewUserView][Error] Nie mozna dodac do bazy") return } if isExists { c.JSON(http.StatusOK, "Login zajęty") return } else { err = addUser(_login, _password, _userDescription) if err != nil { c.JSON(http.StatusOK, "[addNewUserView][Error] Nie mozna dodac do bazy") return } } c.Header("Content-Type", "application/json") c.JSON(http.StatusOK, "[addNewUserView] Dodano uzytkownika do bazy") } func loginUserView(c *gin.Context) { //logowanie - czy jest w bazie c.Header("Content-Type", "application/json") var checkUser User c.Bind(&checkUser) _login := checkUser.Login _password := checkUser.Password if _login == "" { c.JSON(http.StatusOK, "[loginUserView][Error] Nie podano loginu") return } if _password == "" { c.JSON(http.StatusOK, "[loginUserView][Error] Nie podano hasła") return } validLoginData, err := loginUser(_login, _password) if err != nil { c.JSON(http.StatusOK, "[loginUserView][Error] Nie mozna zalogowac") return } c.JSON(http.StatusOK, validLoginData) //true jak zalogowano } func updateUserPointsView(c *gin.Context) { //dodawanie punktu +1 dla uzytkownika c.Header("Content-Type", "application/json") var checkUser User c.Bind(&checkUser) _login := checkUser.Login if _login == "" { c.JSON(http.StatusOK, "[updateUserPointsView][Error] Nie podano loginu") return } isExists, err := checkLoginExists(_login) if err != nil { c.JSON(http.StatusOK, "[updateUserPointsView][Error] Nie mozna dodac punktow") return } if !isExists { c.JSON(http.StatusOK, "[updateUserPointsView][Error] Brak uzytkownika w bazie") return } //TODO dopisac update punktow na bazie err = updateUserPoints(_login) if err != nil { c.JSON(http.StatusOK, "[updateUserPointsView][Error] Nie mozna dodac punktow") return } c.JSON(http.StatusOK, "[updateUserPointsView] Dodano punkt") }