Beispiel #1
0
        public Entity.City[] Select_City(int p_City_Country)
        {
            DataTable o_DataTable = d_City.Select_City(p_City_Country, 0x7FFFFFFF, 1, ref g_TotalCount, ref g_TotalPage);
            if (o_DataTable == null)
                return null;
            else
            {
                Entity.City[] e_City = new Entity.City[o_DataTable.Rows.Count];

                int i = 0;
                foreach (DataRow o_DataRow in o_DataTable.Rows)
                {
                    e_City[i] = new Entity.City();

                    e_City[i].City_ID = Convert.ToInt32(o_DataRow["City_ID"].ToString());
                    e_City[i].City_Country = Convert.ToInt32(o_DataRow["City_Country"].ToString());
                    e_City[i].City_Name = o_DataRow["City_Name"].ToString();
                    e_City[i].City_Title = o_DataRow["City_Title"].ToString();

                    i++;
                }

                return e_City;
            }
        }
        /// <summary>
        /// Updates user personal information.
        /// </summary>
        /// <param name="userId">The user ID to be updated.</param>
        /// <param name="newData">The new data to input.</param>
        /// <returns>Whether the operation was successful.</returns>
        public async Task <bool> UpdateUserPersonalInfo(Guid userId, IUserData newData)
        {
            // TODO: make this function less terrible

            var cityString = newData.GetCity();

            Entity.City cityEntity = null;
            if (cityString != null)
            {
                cityEntity = await _context.Addresses
                             .Where(a => a.City.Name.ToLower() == cityString.ToLower().Trim())
                             .Select(a => a.City)
                             .FirstOrDefaultAsync();

                if (cityEntity == null)
                {
                    cityEntity      = new Entity.City();
                    cityEntity.Name = cityString.Trim();
                }
            }

            // TODO: validate states
            var stateString = newData.GetState();

            Entity.State stateEntity = null;
            if (stateString != null)
            {
                stateEntity = await _context.Addresses
                              .Where(a => a.State.Name.ToLower() == stateString.ToLower().Trim())
                              .Select(a => a.State)
                              .FirstOrDefaultAsync();

                if (stateEntity == null)
                {
                    stateEntity      = new Entity.State();
                    stateEntity.Name = stateString.Trim();
                }
            }


            var zipString = newData.GetZip();

            if (zipString != null)
            {
                // Match any amount of numbers, optionally followed by a dash and any amount of numbers.
                var zipValidator = new Regex(@"^[0-9]{5}(-?[0-9]{4})?$");
                if (!zipValidator.IsMatch(newData.GetZip().Trim()))
                {
                    return(false);
                }
            }

            Entity.ZipCode zipEntity = null;
            if (zipString != null)
            {
                zipEntity = await _context.Addresses
                            .Where(a => a.Zip.Zip == zipString.Trim())
                            .Select(a => a.Zip)
                            .FirstOrDefaultAsync();

                if (zipEntity == null)
                {
                    zipEntity     = new Entity.ZipCode();
                    zipEntity.Zip = zipString.Trim();
                }
            }

            var addressLine1String = newData.GetAddressLine1();

            Entity.AddressLine1 addressLine1Entity = null;
            if (addressLine1String != null)
            {
                addressLine1Entity = await _context.AddressLine1s
                                     .Where(l => l.Data.ToLower() == addressLine1String.ToLower().Trim())
                                     .Select(l => l)
                                     .FirstOrDefaultAsync();

                if (addressLine1Entity == null)
                {
                    addressLine1Entity      = new Entity.AddressLine1();
                    addressLine1Entity.Data = addressLine1String.Trim();
                }
            }

            var addressLine2String = newData.GetAddressLine2();

            Entity.AddressLine2 addressLine2Entity = null;
            if (addressLine2String != null)
            {
                addressLine2Entity = await _context.AddressLine2s
                                     .Where(l => l.Data.ToLower() == addressLine2String.ToLower().Trim())
                                     .Select(l => l)
                                     .FirstOrDefaultAsync();

                if (addressLine2Entity == null)
                {
                    addressLine2Entity      = new Entity.AddressLine2();
                    addressLine2Entity.Data = addressLine2String.Trim();
                }
            }

            var user = await _context.Users
                       .Include(c => c.Address)
                       .ThenInclude(a => a.City)
                       .Include(c => c.Address)
                       .ThenInclude(a => a.State)
                       .Include(c => c.Address)
                       .ThenInclude(a => a.Zip)
                       .Include(c => c.Address)
                       .ThenInclude(a => a.Line1)
                       .Include(c => c.Address)
                       .ThenInclude(a => a.Line2)
                       .Where(c => c.UserId == userId)
                       .Select(c => c)
                       .FirstOrDefaultAsync();

            Entity.Address address = null;
            if (user.Address == null)
            {
                address = new Entity.Address();
                _context.Add(address);
                user.Address = address;
            }
            else
            {
                address = await _context.Addresses
                          .Where(a => a.AddressId == user.Address.AddressId)
                          .Select(a => a)
                          .FirstOrDefaultAsync();
            }

            if (newData.GetFirstName() != null)
            {
                user.FirstName = newData.GetFirstName().Trim();
            }
            else
            {
                user.FirstName = null;
            }

            if (newData.GetLastName() != null)
            {
                user.LastName = newData.GetLastName().Trim();
            }
            else
            {
                user.LastName = null;
            }

            address.City  = cityEntity;
            address.State = stateEntity;
            address.Zip   = zipEntity;
            address.Line1 = addressLine1Entity;
            address.Line2 = addressLine2Entity;

            await _context.SaveChangesAsync();

            return(true);
        }