package main import ( "fmt" "net/http" "github.com/gin-gonic/gin" // go get "golang.org/x/crypto/bcrypt" ) 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 } if err != nil { c.JSON(http.StatusOK, "[addNewUserView][Error] Nie mozna zaszyfrowac hasla") return } 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 fmt.Println("_login: ", _login) fmt.Println("_pasowrd: ", _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") } func addNewCardView(c *gin.Context) { //dodawanie karty na backendzie, odczytywanie wszystkich na froncie bo to nie sa zadne dane wrazliwe //ale dodawanei tutaj bo i tak mozna to zrobic w tej samem domenie co couchdb //PATRZ: documentation/couchdb/local.ini; documentation/couchdb/corsy_ustawienie.txt c.Header("Content-Type", "application/json") fmt.Println("-------------------- ADD NEW CARD ----------------------------------------------") fmt.Println("------------------------------------------------------------------") fmt.Println("------------------------------------------------------------------") fmt.Println("------------------------------------------------------------------") fmt.Println("------------------------------------------------------------------") fmt.Println("------------------------------------------------------------------") fmt.Println("------------------------------------------------------------------") var newCard Card c.Bind(&newCard) _isQuestion := newCard.IsQuestion _blank := newCard.Blank _text := newCard.Text fmt.Println("_isQuestion: ", _isQuestion) fmt.Println("_blank: ", _blank) fmt.Println("_text: ", _text) if _text == "" { c.JSON(http.StatusOK, "[updateUserPointsView][Error] Nie podano 'text'") return } err := addNewCard(_isQuestion, _blank, _text) if err != nil { fmt.Println("Err: ") c.JSON(http.StatusOK, "[addNewCardView][Error] Nie można oddac nowej karty do [couchdb]") return } c.JSON(http.StatusOK, "[addNewCardView] Dodano nową kartę") }