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 == "" {
|
2019-01-12 20:42:29 +01:00
|
|
|
// c.JSON(http.StatusOK, "[loginUserView][Error] Nie podano loginu")
|
|
|
|
c.JSON(http.StatusOK, false)
|
2019-01-10 18:57:21 +01:00
|
|
|
|
2019-01-09 05:30:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if _password == "" {
|
2019-01-12 20:42:29 +01:00
|
|
|
// c.JSON(http.StatusOK, "[loginUserView][Error] Nie podano hasła")
|
|
|
|
c.JSON(http.StatusOK, false)
|
|
|
|
|
2019-01-09 05:30:15 +01:00
|
|
|
}
|
2019-01-12 20:42:29 +01:00
|
|
|
|
2019-01-09 05:30:15 +01:00
|
|
|
validLoginData, err := loginUser(_login, _password)
|
|
|
|
if err != nil {
|
2019-01-12 20:42:29 +01:00
|
|
|
// c.JSON(http.StatusOK, "[loginUserView][Error] Nie mozna zalogowac")
|
|
|
|
c.JSON(http.StatusOK, false)
|
2019-01-10 18:57:21 +01:00
|
|
|
|
2019-01-09 05:30:15 +01:00
|
|
|
}
|
|
|
|
|
2019-01-12 20:42:29 +01:00
|
|
|
if validLoginData { //jezeli zwortic true - poprawne dane to generuje tokena
|
|
|
|
tokenString, err := GenerateJWT(_login) //generowanie tokenu dla dane uzytkownika
|
|
|
|
if err != nil { //jezeli jest bład
|
|
|
|
// c.JSON(http.StatusOK, "[loginUserView][Error] Nie mozna wygenerowac tokenu")
|
|
|
|
c.JSON(http.StatusOK, false)
|
2019-01-09 05:30:15 +01:00
|
|
|
|
2019-01-12 20:42:29 +01:00
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, tokenString) //zwraca token jak zalogowano
|
|
|
|
} else {
|
|
|
|
c.JSON(http.StatusOK, false) //zwraca token jak zalogowano
|
|
|
|
}
|
2019-01-09 05:30:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 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ę")
|
|
|
|
|
|
|
|
}
|
2019-01-13 01:26:13 +01:00
|
|
|
|
|
|
|
func getAllLoggedUsersView(c *gin.Context) {
|
|
|
|
fmt.Println("Zwracanie wszystkich zalogowanych uzytkownikow")
|
|
|
|
|
|
|
|
allLoggedUsers, err := getAllLoggedUsers()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("[getAllLoggedUsersView] [Error] %s", err)
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"code": http.StatusOK,
|
|
|
|
"allLoggedUsers": allLoggedUsers, // cast it to string before showing
|
|
|
|
})
|
|
|
|
|
|
|
|
} else {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"code": http.StatusOK,
|
|
|
|
"allLoggedUsers": nil, // cast it to string before showing
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateLoggedUserView(c *gin.Context) {
|
|
|
|
var loggedUser User //login zalogowane uzytkownika
|
|
|
|
c.Bind(&loggedUser)
|
|
|
|
_login := loggedUser.Login
|
|
|
|
err := updateLoggedUser(_login)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, "[updateLoggedUserView] Nie udalo sie zaktualizowac zalogowanego uzytkownika")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, true) //true jak sie udalo zaktualizowac
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateLogoutUserView(c *gin.Context) {
|
|
|
|
var logoutUser User //login zalogowane uzytkownika
|
|
|
|
c.Bind(&logoutUser)
|
|
|
|
_login := logoutUser.Login
|
2019-01-13 02:41:27 +01:00
|
|
|
fmt.Println("Wylogowano uzytkownika: %s", _login)
|
2019-01-13 03:58:27 +01:00
|
|
|
|
2019-01-13 01:26:13 +01:00
|
|
|
err := updateLogoutUser(_login)
|
2019-01-13 03:58:27 +01:00
|
|
|
|
2019-01-13 01:26:13 +01:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusOK, "[updateLoggedUserView] Nie udalo sie zaktualizowac wylogowanego uzytkownika")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, true) //true jak sie udalo zaktualizowac
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGentelman(c *gin.Context) {
|
|
|
|
//zwraca login aktualnego gentelamana
|
|
|
|
}
|