Example #1
0
        public async Task <Result> Post([FromBody] UserAddressCreateParam param)
        {
            var user = await _workContext.GetCurrentUserAsync();

            var countryId = (int)CountryWithId.China;
            var provinces = await _countryService.GetProvinceByCache(countryId);

            if (provinces == null || provinces.Count <= 0)
            {
                throw new Exception("省市区数据异常,请联系管理员");
            }

            if (!provinces.Any(c => c.Id == param.StateOrProvinceId && c.Level == StateOrProvinceLevel.Default))
            {
                throw new Exception("所选择省信息不存在");
            }

            if (!provinces.Any(c => c.Id == param.CityId && c.Level == StateOrProvinceLevel.City && c.ParentId == param.StateOrProvinceId))
            {
                throw new Exception("所选择市信息不存在");
            }

            if (param.DistrictId.HasValue)
            {
                if (!provinces.Any(c => c.Id == param.DistrictId && c.Level == StateOrProvinceLevel.District && c.ParentId == param.CityId))
                {
                    throw new Exception("所选择区/县信息不存在");
                }
            }

            var address = new Address()
            {
                AddressLine1      = param.AddressLine1,
                ContactName       = param.ContactName,
                Phone             = param.Phone,
                CountryId         = countryId,
                StateOrProvinceId = param.DistrictId ?? param.CityId, // 存储最小结构数据
            };
            var userAddress = new UserAddress()
            {
                Address     = address,
                UserId      = user.Id,
                AddressType = AddressType.Shipping
            };

            _userAddressRepository.Add(userAddress);

            var tran = _userAddressRepository.BeginTransaction();
            await _userAddressRepository.SaveChangesAsync();

            if (param.IsDefault)
            {
                var dbUser = await _userRepository.FirstOrDefaultAsync(user.Id);

                dbUser.DefaultShippingAddress = userAddress;
                await _userRepository.SaveChangesAsync();
            }
            tran.Commit();
            return(Result.Ok());
        }
Example #2
0
        public async Task <Result> Put(int id, [FromBody] UserAddressCreateParam param)
        {
            var user = await _workContext.GetCurrentUserAsync();

            var userAddress = await _userAddressRepository.Query().Include(c => c.Address).FirstOrDefaultAsync(c => c.Id == id && c.UserId == user.Id);

            if (userAddress?.Address == null)
            {
                throw new Exception("地址不存在");
            }

            var countryId = (int)CountryWithId.China;
            var provinces = await _countryService.GetProvinceByCache(countryId);

            if (provinces == null || provinces.Count <= 0)
            {
                throw new Exception("省市区数据异常,请联系管理员");
            }

            if (!provinces.Any(c => c.Id == param.StateOrProvinceId && c.Level == StateOrProvinceLevel.Default))
            {
                throw new Exception("所选择省信息不存在");
            }

            if (!provinces.Any(c => c.Id == param.CityId && c.Level == StateOrProvinceLevel.City && c.ParentId == param.StateOrProvinceId))
            {
                throw new Exception("所选择市信息不存在");
            }

            if (param.DistrictId.HasValue)
            {
                if (!provinces.Any(c => c.Id == param.DistrictId && c.Level == StateOrProvinceLevel.District && c.ParentId == param.CityId))
                {
                    throw new Exception("所选择区/县信息不存在");
                }
            }

            userAddress.Address.AddressLine1      = param.AddressLine1;
            userAddress.Address.ContactName       = param.ContactName;
            userAddress.Address.Phone             = param.Phone;
            userAddress.Address.CountryId         = countryId;
            userAddress.Address.StateOrProvinceId = param.DistrictId ?? param.CityId;
            userAddress.Address.UpdatedOn         = DateTime.Now;

            await _userAddressRepository.SaveChangesAsync();

            if (param.IsDefault)
            {
                var dbUser = await _userRepository.Query()
                             .FirstOrDefaultAsync(c => c.Id == user.Id);

                dbUser.DefaultShippingAddressId = userAddress.Id;
                await _userRepository.SaveChangesAsync();
            }
            else
            {
                var dbUser = await _userRepository.Query()
                             .FirstOrDefaultAsync(c => c.Id == user.Id && c.DefaultShippingAddressId == id);

                if (dbUser?.DefaultShippingAddress != null)
                {
                    dbUser.DefaultShippingAddressId = null;
                    await _userRepository.SaveChangesAsync();
                }
            }
            return(Result.Ok());
        }