add some User functions, move api tests from js to python

This commit is contained in:
arnaucube
2019-06-14 20:00:11 +02:00
parent 8f0d0fd5e0
commit be27edda88
13 changed files with 290 additions and 158 deletions

View File

@@ -3,6 +3,7 @@ package gamesrv
import (
"fmt"
"github.com/arnaucube/gogame/constants"
"github.com/arnaucube/gogame/database"
"github.com/arnaucube/gogame/models"
"github.com/arnaucube/gogame/utils"
@@ -41,7 +42,7 @@ func (srv Service) CreatePlanet(userId bson.ObjectId) (*models.SolarSystem, *mod
// now put the planet in a solar system
// get random solar system
systemPosition := utils.RandInRange(0, models.GALAXYSIZE)
systemPosition := utils.RandInRange(0, constants.GALAXYSIZE)
solarSystem, err := srv.PutPlanetInSolarSystem(systemPosition, planet)
// TODO if error is returned because there is no empty slots for planets in the solar system in systemPosition, get another systemPosition and try again
@@ -54,9 +55,9 @@ func (srv Service) PutPlanetInSolarSystem(position int64, planet *models.Planet)
if err != nil {
// solar system non existing yet
// create a solarsystem with empty planets
var emptyPlanets []bson.ObjectId
for i := 0; i < models.SOLARSYSTEMSIZE; i++ {
emptyPlanets = append(emptyPlanets, "")
var emptyPlanets []string
for i := 0; i < constants.SOLARSYSTEMSIZE; i++ {
emptyPlanets = append(emptyPlanets, "empty")
}
newSolarSystem := models.SolarSystem{
Position: position,
@@ -71,16 +72,15 @@ func (srv Service) PutPlanetInSolarSystem(position int64, planet *models.Planet)
return &solarSystem, err
}
// get free slots in solarSystem
posInSolarSystem := utils.RandInRange(0, models.SOLARSYSTEMSIZE)
posInSolarSystem := utils.RandInRange(0, constants.SOLARSYSTEMSIZE)
if solarSystem.Planets[posInSolarSystem] != "" {
// not empty slot, take another one TODO
// if there are no empty slots, return error
fmt.Println("not empty slot")
}
// store planet in solar system
solarSystem.Planets[posInSolarSystem] = planet.Id
solarSystem.Planets[posInSolarSystem] = planet.Id.String()
err = srv.db.SolarSystems.Update(bson.M{"position": position}, solarSystem)
return &solarSystem, err
}

View File

@@ -0,0 +1,21 @@
package gamesrv
import (
"fmt"
"testing"
"github.com/arnaucube/gogame/database"
"github.com/stretchr/testify/assert"
"gopkg.in/mgo.v2/bson"
)
func TestCreatePlanet(t *testing.T) {
db, err := database.New("127.0.0.1:27017", "gogametests")
assert.Nil(t, err)
srv := New(db)
solarSystem, planet, err := srv.CreatePlanet(bson.ObjectIdHex("5d029a6ff18ba24f406168fe"))
assert.Nil(t, err)
fmt.Println(solarSystem)
fmt.Println(planet)
}

View File

@@ -31,8 +31,8 @@ func checkPasswordHash(password, hash string) bool {
}
func (srv Service) Register(name, password, email string) (*models.User, error) {
var user models.User
err := srv.db.Users.Find(bson.M{"email": email}).One(&user)
var userDb models.User
err := srv.db.Users.Find(bson.M{"email": email}).One(&userDb)
if err == nil {
return nil, errors.New("user already exist")
}
@@ -41,32 +41,22 @@ func (srv Service) Register(name, password, email string) (*models.User, error)
if err != nil {
return nil, err
}
newUser := models.User{
Name: name,
Password: hashedPassword,
Email: email,
}
err = srv.db.Users.Insert(newUser)
if err != nil {
return nil, err
}
err = srv.db.Users.Find(bson.M{"email": email}).One(&user)
user.Password = ""
return &user, err
user, err := models.NewUser(srv.db, name, hashedPassword, email)
return user, err
}
var signingKey = []byte("TODO") // TODO
func (srv Service) Login(email, password string) (*string, *models.User, error) {
var user models.User
err := srv.db.Users.Find(bson.M{"email": email}).One(&user)
var userDb models.UserDb
err := srv.db.Users.Find(bson.M{"email": email}).One(&userDb)
if err != nil {
return nil, nil, errors.New("user not exist")
}
if !checkPasswordHash(password, user.Password) {
if !checkPasswordHash(password, userDb.Password) {
return nil, nil, errors.New("error with password")
}
user.Password = ""
user := models.UserDbToUser(srv.db, userDb)
// create jwt
token := jwt.New(jwt.SigningMethodHS256)
@@ -80,5 +70,17 @@ func (srv Service) Login(email, password string) (*string, *models.User, error)
return nil, nil, errors.New("error creating token")
}
return &tokenString, &user, err
return &tokenString, user, err
}
// func (srv Service) GetUser(id bson.ObjectId) (*models.User, error) {
// // update user stats
// user := getUserFromDB
// user.GetStats()
//
// }
//
// func (srv Service) GetUser(id bson.ObjectId) (*models.User, error) {
// // update user stats
//
// }