public bool HasPassword(UserEntity user)
        {
            var passwordHash = _users.Find(u => u.Id == user.Id).PasswordHash;
            var hasPassword = !string.IsNullOrEmpty(passwordHash);

            return hasPassword;
        }
        public void Update(UserEntity user)
        {
            var userToUpdate = CatchMeContext.Users.Find(user.Id);

            if (userToUpdate != null)
            {
                var userProfile = CatchMeContext.UserProfiles.FirstOrDefault(up => up.UserId == user.Id);

                if (userProfile != null)
                {
                    userProfile.FirstName = user.Profile.FirstName;
                    userProfile.LastName = user.Profile.LastName;
                    userProfile.PhoneNumber = user.Profile.PhoneNumber;
                }

                userToUpdate.Email = user.Email;
                userToUpdate.EmailConfirmed = user.EmailConfirmed;
                userToUpdate.PasswordHash = user.PasswordHash;
                userToUpdate.SecurityStamp = user.SecurityStamp;
                userToUpdate.UserName = user.UserName;
                userToUpdate.Profile = user.Profile;
                userToUpdate.CreationTime = user.CreationTime;

                CatchMeContext.SaveChanges();
            }
        }
        public int Create(UserEntity user)
        {
            CatchMeContext.UserProfiles.Add(user.Profile);
            CatchMeContext.Users.Add(user);
            CatchMeContext.SaveChanges();

            return user.Id;
        }
        public void Delete(UserEntity user)
        {
            var parameters = new[]
        {
                new SqlParameter(SpParams.UserCommon.UserId, user.Id) { Direction = ParameterDirection.Input, DbType = DbType.Int32 }
            };

            ExecuteNonQueryProc(SpNames.DeleteUserById, parameters);
        }
        public int Create(UserEntity user)
        {
            var maxId = _users.Max(u => u.Id);
            var newId = maxId != 0 ? maxId + 1 : 1;            

            _users.Add(user);

            return newId;
        }
        public void Delete(UserEntity user)
        {
            var userToDelete = CatchMeContext.Users             
                .FirstOrDefault(u => u.Id == user.Id);

            if (userToDelete != null)
            {                
                CatchMeContext.Users.Remove(userToDelete);
                CatchMeContext.SaveChanges();
            }
        }
        public void Update(UserEntity user)
        {
            var parameters = new[]
           {
                CreateSqlParameter(SpParams.SaveUser.UserId, user.Id, DbType.Int32),
                CreateSqlParameter(SpParams.SaveUser.Email, user.Email, DbType.String),
                CreateSqlParameter(SpParams.SaveUser.EmailConfirmed, user.EmailConfirmed, DbType.Byte),
                CreateSqlParameter(SpParams.SaveUser.PasswordHash, user.PasswordHash, DbType.String),
                CreateSqlParameter(SpParams.SaveUser.UserName, user.UserName, DbType.String),
                CreateSqlParameter(SpParams.SaveUser.SecurityStamp, user.SecurityStamp, DbType.String),
                CreateSqlParameter(SpParams.SaveUser.CreationTime, user.CreationTime, DbType.DateTime),

                CreateSqlParameter(SpParams.SaveUser.FirstName, user.Profile.FirstName, DbType.String),
                CreateSqlParameter(SpParams.SaveUser.LastName, user.Profile.LastName, DbType.String),
                CreateSqlParameter(SpParams.SaveUser.PhoneNumber, user.Profile.PhoneNumber, DbType.String)
            };

            ExecuteNonQueryProc(SpNames.UpdateUserById, parameters);
        }
        public int Create(UserEntity user)
        {
            var parameters = new[]
            {
                CreateSqlParameter(SpParams.SaveUser.Email, user.Email, DbType.String),
                CreateSqlParameter(SpParams.SaveUser.EmailConfirmed, user.EmailConfirmed, DbType.Byte),
                CreateSqlParameter(SpParams.SaveUser.PasswordHash, user.PasswordHash, DbType.String),
                CreateSqlParameter(SpParams.SaveUser.UserName, user.UserName, DbType.String),
                CreateSqlParameter(SpParams.SaveUser.SecurityStamp, user.SecurityStamp, DbType.String),
                CreateSqlParameter(SpParams.SaveUser.CreationTime, user.CreationTime, DbType.DateTime),

                CreateSqlParameter(SpParams.SaveUser.FirstName, user.Profile.FirstName, DbType.String),
                CreateSqlParameter(SpParams.SaveUser.LastName, user.Profile.LastName, DbType.String),
                CreateSqlParameter(SpParams.SaveUser.PhoneNumber, user.Profile.PhoneNumber, DbType.String)
            };

            var userId = ExecuteScalar(SpNames.AddUser, parameters);

            return userId;
        }
        public IList<string> GetRoles(UserEntity user)
        {
            List<string> userRoles = new List<string>();

            var parameters = new[]
            {
                new SqlParameter(SpParams.UserCommon.UserId, user.Id) { Direction = ParameterDirection.Input, DbType = DbType.Int32 }
            };

            ExecuteDataReaderProc(SpNames.GetUserRoles, parameters, (reader) =>
            {
                if (reader.Read())
                {
                    userRoles.Add(this.PopulateUserRole(reader));
                }
            });

            return userRoles;
        }
        public UserEntity PopulateUserEntity(SqlDataReader reader)
        {
            var user = new UserEntity()
            {
                Id = reader["UserId"].FromDb<int>(),
                Email = reader["Email"].FromDb<string>(),
                EmailConfirmed = reader["EmailConfirmed"].FromDb<bool>(),
                PasswordHash = reader["PasswordHash"].FromDb<string>(),
                SecurityStamp = reader["SecurityStamp"].FromDb<string>(),
                UserName = reader["UserName"].FromDb<string>(),
                CreationTime = reader["CreationTime"].FromDb<DateTime>(),
                Profile = new UserProfileEntity()
                {
                    Id = reader["UserProfileId"].FromDb<int>(),
                    UserId = reader["UserId"].FromDb<int>(),
                    FirstName = reader["FirstName"].FromDb<string>(),
                    LastName = reader["LastName"].FromDb<string>(),
                    PhoneNumber = reader["PhoneNumber"].FromDb<string>()
                }
            };

            return user;
        }
        public string GetEmail(UserEntity user)
        {
            var entity = _users.Find(u => u.Id == user.Id);

            return entity != null ? entity.Email : user.Email;
        }
        public void RemoveFromRole(UserEntity user, string roleName)
        {
            var parameters = new[]
            {
                new SqlParameter(SpParams.UserCommon.UserId, user.Id) { Direction = ParameterDirection.Input, DbType = DbType.Int32 },
                new SqlParameter(SpParams.RoleCommon.RoleName, roleName) { Direction = ParameterDirection.Input, DbType = DbType.String }
            };

            ExecuteNonQueryProc(SpNames.RemoveUserFromRole, parameters);
        }
        public bool IsInRole(UserEntity user, string roleName)
        {
            var userRoles = GetRoles(user).ToList();

            return userRoles.Contains(roleName);
        }
        public bool IsInRole(UserEntity user, string roleName)
        {
            var userInRole = _userRoles.Find(ur => ur.User.Id == user.Id && ur.Role == roleName);

            return userInRole != null;
        }
 public void SetPasswordHash(UserEntity user, string passwordHash)
 {
     user.PasswordHash = passwordHash;
 }
        public void RemoveFromRole(UserEntity user, string roleName)
        {
            var role = CatchMeContext.Roles.FirstOrDefault(r => r.Name == roleName);

            if (role != null)
            {
                var foundedUser = CatchMeContext.Users
                    .Where(u => u.Id == user.Id)
                    .Include(x => x.Roles)
                    .FirstOrDefault();

                if (foundedUser != null)
                {
                    foundedUser.Roles.Remove(role);
                    CatchMeContext.SaveChanges();
                }
            }
        }
 public void Delete(UserEntity user)
 {
     _users.RemoveAll(u => u.Id == user.Id && u.UserName == user.UserName);
 }
        public void Update(UserEntity user)
        {
            var index = _users.FindIndex(u => u.Id == user.Id);

            _users[index] = user;
        }
 public void RemoveFromRole(UserEntity user, string roleName)
 {
     _userRoles.Remove(_userRoles.Find(ur => ur.User.Id == user.Id && ur.Role == roleName));
 }
 public void AddToRole(UserEntity user, string roleName)
 {
     _userRoles.Add(new UserInRole() { User = user, Role = roleName });
 }
 public void SetEmailConfirmed(UserEntity user, bool confirmed)
 {
     user.EmailConfirmed = confirmed;
 }
        public string GetPasswordHash(UserEntity user)
        {
            var passwordHash = _users.Find(u => u.Id == user.Id).PasswordHash;

            return passwordHash;
        }
        public IList<string> GetRoles(UserEntity user)
        {
            var roles = _userRoles.Where(ur => ur.User.Id == user.Id).Select(ur => ur.Role);

            return roles.ToList();
        }
        public IList<string> GetRoles(UserEntity user)
        {
            var foundedUser = CatchMeContext.Users
                .Where(u => u.Id == user.Id)
                .Include(x => x.Roles)
                .First();

            return foundedUser.Roles.Select(r => r.Name).ToList();
        }
 private void SendNotificationToTripPassenger(TripEntity trip, UserEntity passenger)
 {
     _emailService.Send(new EmailMessage()
     {
         Destination = passenger.UserName,
         Subject = "New trip",
         Message =
             string.Format("You was successfully assigned to trip from {0} to {1} ({2}) with Driver: {3}.\n" +
                           " Contact information:\n {4}.\n Price: {5}.\n Car:\n {6} ",
                 trip.Origin.FormattedLongAddress,
                 trip.Destination.FormattedLongAddress,
                 trip.StartDateTime,
                 trip.Driver.Email,
                 trip.Driver.Profile,
                 trip.Price,
                 trip.Vehicle)
     });
 }
 public void SetEmail(UserEntity user, string email)
 {
     user.Email = email;
 }
        public bool GetEmailConfirmed(UserEntity user)
        {
            var emailConfirmed = _users.Find(u => u.Id == user.Id).EmailConfirmed;

            return emailConfirmed;
        }
        private TripEntity PopulateTripEntity(SqlDataReader dataReader)
        {
            var tripId = dataReader["Id"].FromDb<int>();

            var driver = new UserEntity()
            {
                Id = dataReader["UserId"].FromDb<int>(),
                UserName = dataReader["UserName"].FromDb<string>(),
                Email = dataReader["UserEmail"].FromDb<string>(),
            };

            var vehicle = new VehicleEntity()
            {
                Id = dataReader["VehicleId"].FromDb<int>(),
                Manufacturer = dataReader["VehicleManufacturer"].FromDb<string>(),
                Model = dataReader["VehicleModel"].FromDb<string>(),
                Color = dataReader["VehicleColor"].FromDb<string>(),
                Year = dataReader["VehicleYear"].FromDb<int>()
            };

            var mapPoints = this.GetMapPoints(tripId);

            var trip = new TripEntity()
            {
                Id = tripId,
                Vehicle = vehicle,
                Driver = driver,
                Seats = dataReader["Seats"].FromDb<int>(),
                SeatsTaken = dataReader["SeatsTaken"].FromDb<int>(),
                Price = dataReader["Price"].FromDb<decimal>(),
                StaticMapUrl = dataReader["StaticMapUrl"].FromDb<string>(),
                StartDateTime = dataReader["StartDateTime"].FromDb<DateTime>(),
                Origin = mapPoints.FirstOrDefault(),
                Destination = mapPoints.LastOrDefault(),
                WayPoints = mapPoints.Count > 2 ? mapPoints.GetRange(1, mapPoints.Count - 2) : new List<MapPoint>()
            };

            return trip;
        }
 private void SendNotificationToTripDriver(TripEntity trip, UserEntity passenger)
 {
     _emailService.Send(new EmailMessage()
     {
         Destination = trip.Driver.UserName,
         Subject = "New passenger",
         Message = string.Format("Your trip from {0} to {1} ({2}) has new passenger: {3}. Contact information:\n {4}.\nPrice: {5}.\n Your car:\n {6}",
        trip.Origin.FormattedLongAddress,
        trip.Destination.FormattedLongAddress,
        trip.StartDateTime,
        passenger.Email,
        passenger.Profile,
        trip.Price,
        trip.Vehicle)
     });
 }