2018-11-20 00:32:55 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2019-01-09 06:33:30 +01:00
|
|
|
// go get "golang.org/x/crypto/bcrypt"
|
2018-11-20 00:32:55 +01:00
|
|
|
)
|
|
|
|
|
2019-01-10 18:57:21 +01:00
|
|
|
type TokenAPI struct {
|
|
|
|
Token string
|
|
|
|
}
|
|
|
|
|
2019-01-09 02:29:12 +01:00
|
|
|
func getUsersView(c *gin.Context) {
|
|
|
|
// dodanie nowej karty do bzy
|
2019-01-10 18:57:21 +01:00
|
|
|
fmt.Println("-----------------------------WYPISANIE TOKENU ------------------------------")
|
|
|
|
|
|
|
|
var token TokenAPI
|
|
|
|
c.Bind(&token)
|
|
|
|
fmt.Println("MOJ TOKEN %s", token.Token)
|
|
|
|
|
|
|
|
// c.Header("Content-Type", "application/json")
|
|
|
|
fmt.Println("Pobieranei listy użytkowników")
|
2019-01-09 02:29:12 +01:00
|
|
|
|
|
|
|
var userList []map[string]interface{}
|
|
|
|
|
|
|
|
allUsers := getAllUsers()
|
|
|
|
for _, arg := range allUsers {
|
|
|
|
tmp := make(map[string]interface{})
|
2019-01-09 03:55:36 +01:00
|
|
|
tmp["login"] = arg.Login
|
|
|
|
tmp["userDescription"] = arg.UserDescription
|
|
|
|
tmp["points"] = arg.Points
|
2019-01-09 02:29:12 +01:00
|
|
|
userList = append(userList, tmp)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"code": http.StatusOK,
|
|
|
|
"allUsers": userList, // cast it to string before showing
|
|
|
|
})
|
2018-11-20 00:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-01-09 03:55:36 +01:00
|
|
|
func addNewUserView(c *gin.Context) {
|
2019-01-09 05:30:15 +01:00
|
|
|
//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")
|
2019-01-09 03:55:36 +01:00
|
|
|
var newUser User
|
|
|
|
c.Bind(&newUser)
|
|
|
|
|
|
|
|
fmt.Println(newUser.Login)
|
|
|
|
_login := newUser.Login
|
|
|
|
_password := newUser.Password
|
|
|
|
_userDescription := newUser.UserDescription
|
|
|
|
|
2019-01-09 05:30:15 +01:00
|
|
|
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
|
|
|
|
}
|
2019-01-09 03:55:36 +01:00
|
|
|
|
2019-01-09 05:30:15 +01:00
|
|
|
if isExists {
|
|
|
|
c.JSON(http.StatusOK, "Login zajęty")
|
|
|
|
return
|
2019-01-09 06:33:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, "[addNewUserView][Error] Nie mozna zaszyfrowac hasla")
|
|
|
|
return
|
|
|
|
}
|
2019-01-09 03:55:36 +01:00
|
|
|
|
2019-01-09 06:33:30 +01:00
|
|
|
err = addUser(_login, _password, _userDescription)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, "[addNewUserView][Error] Nie mozna dodac do bazy")
|
|
|
|
return
|
2019-01-09 05:30:15 +01:00
|
|
|
}
|
2019-01-09 06:33:30 +01:00
|
|
|
|
2019-01-09 05:30:15 +01:00
|
|
|
c.Header("Content-Type", "application/json")
|
|
|
|
c.JSON(http.StatusOK, "[addNewUserView] Dodano uzytkownika do bazy")
|
2018-11-20 00:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-01-09 05:30:15 +01:00
|
|
|
func loginUserView(c *gin.Context) {
|
2019-01-09 02:29:12 +01:00
|
|
|
//logowanie - czy jest w bazie
|
2019-01-10 18:57:21 +01:00
|
|
|
fmt.Println("HEARES--------------------------------")
|
|
|
|
fmt.Println(c.Header)
|
|
|
|
fmt.Println(c.Request)
|
2019-01-09 05:30:15 +01:00
|
|
|
c.Header("Content-Type", "application/json")
|
|
|
|
|
|
|
|
var checkUser User
|
|
|
|
c.Bind(&checkUser)
|
|
|
|
_login := checkUser.Login
|
|
|
|
_password := checkUser.Password
|
2019-01-10 18:57:21 +01:00
|
|
|
_token := checkUser.Token
|
2019-01-09 05:30:15 +01:00
|
|
|
|
2019-01-10 03:45:25 +01:00
|
|
|
fmt.Println("_login: ", _login)
|
|
|
|
fmt.Println("_pasowrd: ", _password)
|
2019-01-10 18:57:21 +01:00
|
|
|
fmt.Println("_token: ", _token)
|
2019-01-10 03:45:25 +01:00
|
|
|
|
2019-01-09 05:30:15 +01:00
|
|
|
if _login == "" {
|
|
|
|
c.JSON(http.StatusOK, "[loginUserView][Error] Nie podano loginu")
|
2019-01-10 18:57:21 +01:00
|
|
|
|
2019-01-09 05:30:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if _password == "" {
|
|
|
|
c.JSON(http.StatusOK, "[loginUserView][Error] Nie podano hasła")
|
|
|
|
}
|
|
|
|
validLoginData, err := loginUser(_login, _password)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, "[loginUserView][Error] Nie mozna zalogowac")
|
2019-01-10 18:57:21 +01:00
|
|
|
|
2019-01-09 05:30:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2018-11-20 00:32:55 +01:00
|
|
|
}
|
2019-01-09 07:26:21 +01:00
|
|
|
|
|
|
|
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")
|
2019-01-10 03:45:25 +01:00
|
|
|
fmt.Println("-------------------- ADD NEW CARD ----------------------------------------------")
|
2019-01-09 09:22:07 +01:00
|
|
|
fmt.Println("------------------------------------------------------------------")
|
|
|
|
fmt.Println("------------------------------------------------------------------")
|
|
|
|
fmt.Println("------------------------------------------------------------------")
|
|
|
|
fmt.Println("------------------------------------------------------------------")
|
|
|
|
fmt.Println("------------------------------------------------------------------")
|
|
|
|
fmt.Println("------------------------------------------------------------------")
|
2019-01-09 07:26:21 +01:00
|
|
|
|
|
|
|
var newCard Card
|
|
|
|
c.Bind(&newCard)
|
|
|
|
_isQuestion := newCard.IsQuestion
|
|
|
|
_blank := newCard.Blank
|
|
|
|
_text := newCard.Text
|
2019-01-10 03:45:25 +01:00
|
|
|
fmt.Println("_isQuestion: ", _isQuestion)
|
|
|
|
fmt.Println("_blank: ", _blank)
|
|
|
|
fmt.Println("_text: ", _text)
|
2019-01-09 07:26:21 +01:00
|
|
|
|
|
|
|
if _text == "" {
|
|
|
|
c.JSON(http.StatusOK, "[updateUserPointsView][Error] Nie podano 'text'")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := addNewCard(_isQuestion, _blank, _text)
|
|
|
|
if err != nil {
|
2019-01-10 03:45:25 +01:00
|
|
|
fmt.Println("Err: ")
|
2019-01-09 07:26:21 +01:00
|
|
|
c.JSON(http.StatusOK, "[addNewCardView][Error] Nie można oddac nowej karty do [couchdb]")
|
|
|
|
return
|
|
|
|
}
|
2019-01-10 03:45:25 +01:00
|
|
|
|
2019-01-09 07:26:21 +01:00
|
|
|
c.JSON(http.StatusOK, "[addNewCardView] Dodano nową kartę")
|
|
|
|
|
|
|
|
}
|