Пример #1
0
        /// <summary>
        /// Inserta en la tabla earthwatcher al usuario creado
        /// </summary>
        /// <param name="earthwatcher"></param>
        /// <returns></returns>
        public Earthwatcher CreateEarthwatcher(Earthwatcher earthwatcher)
        {
            var passwordPrefix = "";
            var hashedPassword = "";

            if (earthwatcher.Password != null)
            {
                passwordPrefix = PasswordChecker.GeneratePrefix();
                hashedPassword = PasswordChecker.GenerateHashedPassword(earthwatcher.Password, passwordPrefix);
            }
            connection.Open();
            var cmd = connection.CreateCommand() as SqlCommand;

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Earthwatcher_CreateEarthwatcher";
            cmd.Parameters.Add(new SqlParameter("@guid", earthwatcher.Guid));
            if (string.IsNullOrEmpty(earthwatcher.Name))
            {
                cmd.Parameters.Add(new SqlParameter("@name", ""));
            }
            else
            {
                cmd.Parameters.Add(new SqlParameter("@name", earthwatcher.Name));
            }
            cmd.Parameters.Add(new SqlParameter("@role", earthwatcher.Role));
            cmd.Parameters.Add(new SqlParameter("@prefix", passwordPrefix));
            cmd.Parameters.Add(new SqlParameter("@hash", hashedPassword));
            cmd.Parameters.Add(new SqlParameter("@country", earthwatcher.Country));
            cmd.Parameters.Add(new SqlParameter("@language", earthwatcher.Language));
            cmd.Parameters.Add(new SqlParameter("@playingCountry", earthwatcher.PlayingCountry));
            cmd.Parameters.Add(new SqlParameter("@playingRegion", earthwatcher.PlayingRegion));
            if (!string.IsNullOrEmpty(earthwatcher.NickName))
            {
                cmd.Parameters.Add(new SqlParameter("@nick", earthwatcher.NickName));
            }
            if (earthwatcher.PlayingRegion == null || earthwatcher.PlayingRegion == 0)
            {
                var           regionRepository = new RegionRepository(connection.ConnectionString);
                List <Region> regions          = regionRepository.GetByCountryCode(earthwatcher.Country);
                earthwatcher.PlayingRegion = regions.FirstOrDefault().Id;
            }

            var idParameter = new SqlParameter("@ID", SqlDbType.Int)
            {
                Direction = ParameterDirection.Output
            };

            cmd.Parameters.Add(idParameter);
            cmd.ExecuteNonQuery();
            var id = (int)idParameter.Value;

            earthwatcher.Id = id;
            connection.Close();
            return(earthwatcher);
        }
        /// <summary>
        /// Inserta en la tabla earthwatcher al usuario creado
        /// </summary>
        /// <param name="earthwatcher"></param>
        /// <returns></returns>
        public Earthwatcher CreateEarthwatcher(Earthwatcher earthwatcher)
        {
            var passwordPrefix = "";
            var hashedPassword = "";
            if (earthwatcher.Password != null)
            {
               passwordPrefix = PasswordChecker.GeneratePrefix();
               hashedPassword = PasswordChecker.GenerateHashedPassword(earthwatcher.Password, passwordPrefix);
            }
            connection.Open();
            var cmd = connection.CreateCommand() as SqlCommand;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Earthwatcher_CreateEarthwatcher";
            cmd.Parameters.Add(new SqlParameter("@guid", earthwatcher.Guid));
            if(string.IsNullOrEmpty(earthwatcher.Name))
            {
                cmd.Parameters.Add(new SqlParameter("@name", ""));
            }
            else
            {
                cmd.Parameters.Add(new SqlParameter("@name", earthwatcher.Name));
            }
            cmd.Parameters.Add(new SqlParameter("@role", earthwatcher.Role));
            cmd.Parameters.Add(new SqlParameter("@prefix", passwordPrefix));
            cmd.Parameters.Add(new SqlParameter("@hash", hashedPassword));
            cmd.Parameters.Add(new SqlParameter("@country", earthwatcher.Country));
            cmd.Parameters.Add(new SqlParameter("@language", earthwatcher.Language));
            cmd.Parameters.Add(new SqlParameter("@playingCountry", earthwatcher.PlayingCountry));
            cmd.Parameters.Add(new SqlParameter("@playingRegion", earthwatcher.PlayingRegion));
            if (!string.IsNullOrEmpty(earthwatcher.NickName))
            {
                cmd.Parameters.Add(new SqlParameter("@nick", earthwatcher.NickName));
            }
            if (earthwatcher.PlayingRegion == null || earthwatcher.PlayingRegion == 0)
            {
                var regionRepository = new RegionRepository(connection.ConnectionString);
                List<Region> regions = regionRepository.GetByCountryCode(earthwatcher.Country);
                earthwatcher.PlayingRegion = regions.FirstOrDefault().Id;
            }

            var idParameter = new SqlParameter("@ID", SqlDbType.Int) { Direction = ParameterDirection.Output };
            cmd.Parameters.Add(idParameter);
            cmd.ExecuteNonQuery();
            var id = (int)idParameter.Value;
            earthwatcher.Id = id;
            connection.Close();
            return earthwatcher;
        }