/// <summary>
        /// Adds a new User to DataBase
        /// </summary>
        /// <param name="user">User to add</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        /// <returns>Returns the Id of added Item if operation is successful otherwise returns -1</returns>
        public int Add(UserBase user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }
            if (String.IsNullOrWhiteSpace(user.Name))
            {
                throw new InvalidOperationException("User name cannot be a Null, Empty or WhiteSpace");
            }

            var restaurantuser = UserBaseToRestaurantUser(user);

            if (restaurantuser.UserRole == UserBase.RestaurantUserRole.Guest && restaurantuser.MobileNumber.ToString().Length != 10)
            {
                throw new InvalidOperationException("A Guest user cannot be added without a Valid Mobile Number");
            }

            var facebookaddsuccess = 0UL;

            if (restaurantuser.FacebookDetail != null)
            {
                facebookaddsuccess = new FacebookUserDetailRepository().Add(restaurantuser.FacebookDetail);
            }

            using (var cn = new SqlConnection(DatabaseConnection.ConnectionStringToDb))
            {
                using (var cmd = new SqlCommand("AddRestaurantUser", cn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@NAME", SqlDbType.VarChar, 100).Value = restaurantuser.Name;
                    if (restaurantuser.MobileNumber > 0)
                    {
                        cmd.Parameters.Add("@MOBILENUMBER", SqlDbType.BigInt).Value = restaurantuser.MobileNumber;
                    }
                    cmd.Parameters.Add("@ADDRESS", SqlDbType.Text).Value        = restaurantuser.Address;
                    cmd.Parameters.Add("@EMAILID", SqlDbType.VarChar, 80).Value = restaurantuser.EmailId;
                    if (facebookaddsuccess > 0 && restaurantuser.FacebookDetail != null)
                    {
                        cmd.Parameters.Add("@FACEBOOKID", SqlDbType.BigInt).Value = restaurantuser.FacebookDetail.FacebookId;
                    }
                    cmd.Parameters.Add("@PASSWORD", SqlDbType.VarChar, 80).Value      = HashPasswordForStoringInDb(restaurantuser.Password);
                    cmd.Parameters.Add("@USERROLE", SqlDbType.TinyInt).Value          = (int)restaurantuser.UserRole;
                    cmd.Parameters.Add("@LOGINEXPIRESON", SqlDbType.DateTime).Value   = restaurantuser.LoginExpiresOn;
                    cmd.Parameters.Add("@USERGUID", SqlDbType.UniqueIdentifier).Value = restaurantuser.UserGuid !=
                                                                                        Guid.Empty
                                                                                            ? restaurantuser.UserGuid
                                                                                            : (object)DBNull.Value;

                    cn.Open();
                    var res = Convert.ToInt32(cmd.ExecuteScalar());
                    return(res > 0 ? res : -1);
                }
            }
        }
        /// <summary>
        /// Updates the Available User Details with new Details
        /// </summary>
        /// <param name="user">The Modified User Detail which needs to be updated</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        /// <returns>Returns true if operation is successful otherwise returns false</returns>
        public bool Update(UserBase user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }
            if (user.UserId == 0)
            {
                throw new InvalidOperationException("To update  a Restaurant user the UserID should be non zero");
            }
            if (String.IsNullOrWhiteSpace(user.Name))
            {
                throw new InvalidOperationException("User name cannot be a Null, Empty or WhiteSpace");
            }
            var prevuser = Find(user.UserId);

            if (prevuser == null)
            {
                return(false);
            }

            var restaurantuser = UserBaseToRestaurantUser(user);

            if (restaurantuser.UserRole == UserBase.RestaurantUserRole.Guest && restaurantuser.MobileNumber < 1)
            {
                throw new InvalidOperationException("A Guest user cannot be Update without a Valid Mobile Number");
            }

            var facebookdetailexist = false;
            var facebookaddsuccess  = 0UL;

            if (restaurantuser.FacebookDetail != null)
            {
                facebookdetailexist = new FacebookUserDetailRepository().Update(restaurantuser.FacebookDetail);
            }

            if (facebookdetailexist)
            {
                facebookaddsuccess = restaurantuser.FacebookDetail.FacebookId;
            }

            if (restaurantuser.FacebookDetail != null && !facebookdetailexist)
            {
                facebookaddsuccess = new FacebookUserDetailRepository().Add(restaurantuser.FacebookDetail);
            }

            using (var cn = new SqlConnection(DatabaseConnection.ConnectionStringToDb))
            {
                using (var cmd = new SqlCommand("UpdateRestaurantUser", cn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@USERID", SqlDbType.BigInt).Value     = restaurantuser.UserId;
                    cmd.Parameters.Add("@NAME", SqlDbType.VarChar, 100).Value = restaurantuser.Name;
                    if (restaurantuser.MobileNumber > 0)
                    {
                        cmd.Parameters.Add("@MOBILENUMBER", SqlDbType.BigInt).Value = restaurantuser.MobileNumber;
                    }
                    cmd.Parameters.Add("@ADDRESS", SqlDbType.Text).Value        = restaurantuser.Address;
                    cmd.Parameters.Add("@EMAILID", SqlDbType.VarChar, 80).Value = restaurantuser.EmailId;
                    if (facebookaddsuccess > 0 && restaurantuser.FacebookDetail != null)
                    {
                        cmd.Parameters.Add("@FACEBOOKID", SqlDbType.BigInt).Value = restaurantuser.FacebookDetail.FacebookId;
                    }
                    if (restaurantuser.Password != null)
                    {
                        cmd.Parameters.Add("@PASSWORD", SqlDbType.VarChar, 80).Value =
                            //if following conditions are met then the password string is already an unchanged hashed string
                            (
                                UserBaseToRestaurantUser(prevuser).Password == restaurantuser.Password &&
                                restaurantuser.Password.Trim().Length == 73 && restaurantuser.Password.Trim().Split('.').Length == 2
                            )
                            ? restaurantuser.Password
                            : HashPasswordForStoringInDb(restaurantuser.Password);
                    }
                    cmd.Parameters.Add("@USERROLE", SqlDbType.TinyInt).Value          = (int)restaurantuser.UserRole;
                    cmd.Parameters.Add("@LOGINEXPIRESON", SqlDbType.DateTime).Value   = restaurantuser.LoginExpiresOn;
                    cmd.Parameters.Add("@USERGUID", SqlDbType.UniqueIdentifier).Value = restaurantuser.UserGuid !=
                                                                                        Guid.Empty
                                                                                            ? restaurantuser.UserGuid
                                                                                            : (object)DBNull.Value;

                    cn.Open();
                    var res = cmd.ExecuteNonQuery();
                    return(res == 1);
                }
            }
        }
 public CustomSqlRoleProviderWrapper()
 {
     UserRepository = new RestaurantUserRepository();
     FacebookDetailRepository = new FacebookUserDetailRepository();
 }