Dodanie bledow i update punktow na bazie
This commit is contained in:
parent
711d9cc9ba
commit
196dfca658
57
backend/backend_tests/test_views.txt
Normal file
57
backend/backend_tests/test_views.txt
Normal file
@ -0,0 +1,57 @@
|
||||
127.0.0.1:3000/api/addNewUserView
|
||||
|
||||
{
|
||||
"login": "B",
|
||||
"password": "B"
|
||||
}
|
||||
#"[addNewUserView] Dodano uzytkownika do bazy"
|
||||
|
||||
{
|
||||
"login": "B2",
|
||||
"password": "B",
|
||||
"test": "test"
|
||||
}
|
||||
"[addNewUserView] Dodano uzytkownika do bazy"
|
||||
|
||||
{
|
||||
"password": "A",
|
||||
"points": 11111
|
||||
}
|
||||
#"[addNewUserView][Error] Nie podano loginu"
|
||||
|
||||
|
||||
{
|
||||
"login": "A",
|
||||
"password": "A"
|
||||
}
|
||||
|
||||
#"Login zajęty"
|
||||
|
||||
{
|
||||
"login": "A"
|
||||
}
|
||||
#"[addNewUserView][Error] Nie podano hasła"
|
||||
|
||||
|
||||
127.0.0.1:3000/api/updateUserPointsView
|
||||
{
|
||||
"login": "B2"
|
||||
|
||||
}
|
||||
#"[updateUserPointsView] Dodano punkt"
|
||||
|
||||
{
|
||||
"logine": "B2"
|
||||
|
||||
}
|
||||
#"[updateUserPointsView][Error] Nie podano loginu"
|
||||
|
||||
{
|
||||
"login": "B2333333333333"
|
||||
|
||||
}
|
||||
#"[updateUserPointsView][Error] Brak uzytkownika w bazie"
|
||||
|
||||
|
||||
|
||||
|
@ -16,7 +16,7 @@ func connectMysql() (*sql.DB, error) {
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func addUser(_login string, _password string, _userDescription string, _points int) {
|
||||
func addUser(_login string, _password string, _userDescription string) error {
|
||||
//do rejestracji uzytkownika
|
||||
// Insert do bazy Mysql Nowego użytkownika
|
||||
|
||||
@ -24,6 +24,7 @@ func addUser(_login string, _password string, _userDescription string, _points i
|
||||
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
queryInsert := fmt.Sprintf(`INSERT INTO users (
|
||||
@ -36,14 +37,16 @@ func addUser(_login string, _password string, _userDescription string, _points i
|
||||
"%s",
|
||||
"%s",
|
||||
"%d"
|
||||
)`, _login, _password, _userDescription, _points)
|
||||
)`, _login, _password, _userDescription, 0) //przy rejestracji kzdy ma 0 punktow
|
||||
|
||||
fmt.Printf(queryInsert)
|
||||
insert, err := db.Query(queryInsert)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
return err
|
||||
}
|
||||
fmt.Println(insert)
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
@ -82,7 +85,7 @@ func getAllUsers() []User {
|
||||
return allUsers
|
||||
}
|
||||
|
||||
func checkLoginExists(_login string) bool {
|
||||
func checkLoginExists(_login string) (bool, error) {
|
||||
//sprawdza czy dany login jest juz w bazie, przy rejestracji przydatne
|
||||
db, err := connectMysql()
|
||||
|
||||
@ -91,6 +94,7 @@ func checkLoginExists(_login string) bool {
|
||||
result, err := db.Query(querySelect)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
return false, err
|
||||
}
|
||||
|
||||
for result.Next() {
|
||||
@ -99,17 +103,19 @@ func checkLoginExists(_login string) bool {
|
||||
err = result.Scan(&userLogin)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
return false, err
|
||||
|
||||
}
|
||||
|
||||
if userLogin != "" {
|
||||
return true
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func loginUser(_login string, _password string) bool {
|
||||
func loginUser(_login string, _password string) (bool, error) {
|
||||
//do logowanie w bazie
|
||||
db, err := connectMysql()
|
||||
querySelect := fmt.Sprintf(`SELECT login FROM users WHERE login='%s' AND password='%s' ;`, _login, _password)
|
||||
@ -117,6 +123,7 @@ func loginUser(_login string, _password string) bool {
|
||||
result, err := db.Query(querySelect)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
return false, err
|
||||
}
|
||||
|
||||
for result.Next() {
|
||||
@ -125,12 +132,44 @@ func loginUser(_login string, _password string) bool {
|
||||
err = result.Scan(&userLogin)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
return false, err
|
||||
}
|
||||
|
||||
if userLogin != "" {
|
||||
return true
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func updateUserPoints(_login string) error {
|
||||
db, err := connectMysql()
|
||||
var userPoints int
|
||||
|
||||
querySelectPoints := fmt.Sprintf(`SELECT points FROM users WHERE login='%s' ;`, _login)
|
||||
|
||||
result, err := db.Query(querySelectPoints)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
for result.Next() { //pobieranie aktualnej liczby punktow uzytkownika
|
||||
|
||||
err = result.Scan(&userPoints)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
queryUpdatePoints := fmt.Sprintf(`UPDATE users SET points = %d WHERE login ='%s' ;`, userPoints+1, _login)
|
||||
result, err = db.Query(queryUpdatePoints)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
return nil //jak bez bledow wszystko
|
||||
}
|
||||
|
@ -1,40 +1,12 @@
|
||||
package main
|
||||
|
||||
// go get -u github.com/gin-gonic/gin
|
||||
// go get -u github.com/gin-gonic/contrib/static
|
||||
// go get "github.com/gin-gonic/contrib/static"
|
||||
// go get "github.com/gin-gonic/gin"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/contrib/static"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Let's create our Jokes struct. This will contain information about a Joke
|
||||
|
||||
// Joke contains information about a single Joke
|
||||
type Joke struct {
|
||||
ID int `json:"id" binding:"required"`
|
||||
Likes int `json:"likes"`
|
||||
Joke string `json:"joke" binding:"required"`
|
||||
}
|
||||
|
||||
// We'll create a list of jokes
|
||||
var jokes = []Joke{
|
||||
Joke{0, 0, "How does a penguin build it's house? Igloos it together."},
|
||||
Joke{1, 0, "Did you hear about the restaurant on the moon? Great food, no atmosphere."},
|
||||
Joke{2, 0, "What do you call a fake noodle? An Impasta."},
|
||||
Joke{3, 0, "How many apples grow on a tree? All of them."},
|
||||
Joke{4, 0, "Want to hear a joke about paper? Nevermind it's tearable."},
|
||||
Joke{5, 0, "I just watched a program about beavers. It was the best dam program I've ever seen."},
|
||||
Joke{6, 0, "Why did the coffee file a police report? It got mugged."},
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Set the router as the default one shipped with Gin
|
||||
router := gin.Default()
|
||||
@ -52,36 +24,11 @@ func main() {
|
||||
})
|
||||
}
|
||||
|
||||
// Our API will consit of just two routes
|
||||
// /jokes - which will retrieve a list of jokes a user can see
|
||||
// /jokes/like/:jokeID - which will capture likes sent to a particular joke
|
||||
api.GET("/jokes", JokeHandler)
|
||||
api.POST("/jokes/like/:jokeID", LikeJoke)
|
||||
api.GET("/getUsersView", getUsersView) //widok pobrania wszystkich użytkowników
|
||||
api.POST("/addNewUserView", addNewUserView) // json z danymi nowego uzytkownika
|
||||
api.GET("/getUsersView", getUsersView) //widok pobrania wszystkich użytkowników
|
||||
api.POST("/addNewUserView", addNewUserView) // json z danymi nowego uzytkownika
|
||||
api.POST("/loginUserView", loginUserView) //logowanie
|
||||
api.POST("/updateUserPointsView", updateUserPointsView) // inkrementacja punktow
|
||||
|
||||
// Start and run the server
|
||||
router.Run(":3000")
|
||||
}
|
||||
|
||||
// JokeHandler retrieves a list of available jokes
|
||||
func JokeHandler(c *gin.Context) {
|
||||
c.Header("Content-Type", "application/json")
|
||||
fmt.Println(reflect.TypeOf(jokes))
|
||||
|
||||
c.JSON(http.StatusOK, jokes)
|
||||
}
|
||||
|
||||
// LikeJoke increments the likes of a particular joke Item
|
||||
func LikeJoke(c *gin.Context) {
|
||||
// confirm Joke ID sent is valid
|
||||
// remember to import the `strconv` package
|
||||
//jokeid - wartosc z url'a
|
||||
if jokeid, err := strconv.Atoi(c.Param("jokeID")); err == nil {
|
||||
// return a pointer to the updated jokes list
|
||||
c.JSON(http.StatusOK, &jokes[jokeid])
|
||||
} else {
|
||||
// Joke ID is invalid
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
|
||||
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{}
|
||||
@ -22,7 +23,6 @@ func getUsersView(c *gin.Context) {
|
||||
userList = append(userList, tmp)
|
||||
}
|
||||
|
||||
c.Header("Content-Type", "application/json")
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": http.StatusOK,
|
||||
"allUsers": userList, // cast it to string before showing
|
||||
@ -30,7 +30,10 @@ func getUsersView(c *gin.Context) {
|
||||
}
|
||||
|
||||
func addNewUserView(c *gin.Context) {
|
||||
// Read the Body content
|
||||
//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)
|
||||
|
||||
@ -38,16 +41,96 @@ func addNewUserView(c *gin.Context) {
|
||||
_login := newUser.Login
|
||||
_password := newUser.Password
|
||||
_userDescription := newUser.UserDescription
|
||||
_points := newUser.Points
|
||||
|
||||
addUser(_login, _password, _userDescription, _points)
|
||||
if _login == "" {
|
||||
c.JSON(http.StatusOK, "[addNewUserView][Error] Nie podano loginu")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("PUSTE BODY : <")
|
||||
if _password == "" {
|
||||
c.JSON(http.StatusOK, "[addNewUserView][Error] Nie podano hasła")
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, "ala")
|
||||
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() {
|
||||
func loginUserView(c *gin.Context) {
|
||||
//logowanie - czy jest w bazie
|
||||
fmt.Println("Logowanie użytkownika")
|
||||
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")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user