public UserDto Create(UserDto user)
        {
            User newUser;

            using (var db = new OnlineCasinoDb())
            {
                if (db.Users.FirstOrDefault(u => object.Equals(u.Username, user.Username)) != null)
                {
                    throw new ConflictException();
                }

                newUser = new User()
                {
                    FullName = user.FullName,
                    Email    = user.Email,
                    Money    = 0
                };

                CryptographicManager.SetNewUserInfo(newUser, user.Username, user.Password);

                db.Users.Add(newUser);
                db.SaveChanges();
            }

            return(CreateUserDTO(newUser));
        }
Пример #2
0
        public void SetUp()
        {
            // Configure server and client
            var config = new HttpConfiguration();

            WebApiConfig.Register(config);
            TestHelper.Server = new HttpServer(config);
            TestHelper.Client = new HttpClient(TestHelper.Server);

            // Make sure a test user exists in database if not create it
            TestHelper.TestUser = new UserDto()
            {
                Username = "******",
                Password = Convert.ToBase64String(Guid.NewGuid().ToByteArray()),
                FullName = "Test Testov",
                Email    = "*****@*****.**",
                Money    = 9999
            };

            User user;

            using (var db = new OnlineCasinoDb())
            {
                user = db.Users.FirstOrDefault(u => object.Equals(u.Username, TestHelper.TestUser.Username));

                if (user == null)
                {
                    user = new User();
                }

                CryptographicManager.SetNewUserInfo(user, TestHelper.TestUser.Username, TestHelper.TestUser.Password);
                user.Username = TestHelper.TestUser.Username;
                user.FullName = TestHelper.TestUser.FullName;
                user.Email    = TestHelper.TestUser.Email;
                user.Money    = TestHelper.TestUser.Money;

                db.Users.AddOrUpdate(user);
                db.SaveChanges();
            }

            TestHelper.TestUser.Id = user.Id; // save user id

            // Create test login
            TestHelper.TestLogin = new LoginDto()
            {
                UserId = TestHelper.TestUser.Id,
                Token  = Convert.ToBase64String(Guid.NewGuid().ToByteArray())
            };

            Login login;

            using (var db = new OnlineCasinoDb())
            {
                login = new Login()
                {
                    UserId = TestHelper.TestLogin.UserId,
                    Token  = TestHelper.TestLogin.Token
                };

                db.Logins.Add(login);
                db.SaveChanges();
            }

            TestHelper.TestLogin.Id = login.Id; // save login id
        }