コード例 #1
0
        /// <summary>
        /// Update user address
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual async Task <ResultModel> UpdateUserAddressAsync(EditUserProfileAddressViewModel model)
        {
            var resultModel = new ResultModel();
            var user        = (await _userManager.GetCurrentUserAsync()).Result;

            if (user == null)
            {
                return(new NotAuthorizedResultModel().ToBase());
            }
            var currentAddress = await _context.UserAddresses.FirstOrDefaultAsync(x => x.Id.Equals(model.Id));

            if (currentAddress == null && model.Id != Guid.Empty)
            {
                if (!await _context.UserAddresses.AnyAsync(x => x.UserId == user.Id))
                {
                    var newAddress = _mapper.Map <AddNewAddressViewModel>(model);
                    newAddress.IsDefault = true;
                    var addReq = await AddAddressAsync(newAddress);

                    return(addReq.ToBase());
                }
            }

            if (currentAddress == null)
            {
                resultModel.Errors.Add(new ErrorModel(string.Empty, "Address not found"));
                return(resultModel);
            }

            if (model.IsDefault)
            {
                _context.UserAddresses
                .Where(x => x.UserId.Equals(currentAddress.UserId))
                .ToList().ForEach(b => b.IsDefault = false);
            }

            currentAddress.CountryId         = model.CountryId;
            currentAddress.StateOrProvinceId = model.CityId;
            currentAddress.AddressLine1      = model.AddressLine1;
            currentAddress.AddressLine2      = model.AddressLine2;
            currentAddress.ContactName       = model.ContactName;
            currentAddress.Phone             = model.Phone;
            currentAddress.ZipCode           = model.ZipCode;
            currentAddress.IsDefault         = model.IsDefault;
            currentAddress.Region            = model.Region;

            _context.Update(currentAddress);
            var result = await _context.PushAsync();

            return(result);
        }