PracowniaProgramowania/backend/views.go

213 lines
5.7 KiB
Go
Raw Normal View History

2018-11-20 00:32:55 +01:00
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
// 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{})
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
}
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")
2018-11-20 00:32:55 +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)
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-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
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
}
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-12 20:42:29 +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-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-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
}
}
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ę")
}