public async Task <ActionResult> AddNewAddress(UserAddressPostModel userAddressPostModel) { if (!_userAddressService.IsValidAddress(userAddressPostModel)) { return(Ok(new { error_message = "Cu phap khong hop le" })); } User user = await _userServices.GetUserById(userAddressPostModel.UserId); if (user is null) { return(Ok(new { error_message = "Khong ton tai User" })); } DistrictAddress districtAddress = await _cityServices.GetCityAndDistrictAsync(userAddressPostModel); if (districtAddress is null) { return(Ok(new { error_message = "Khong ton tai dia chi nay" })); } await _userAddressService.SetAllUserAddressToFalseAsync(userAddressPostModel.UserId); UserAddress userAddress = _mapper.Map <UserAddress>(userAddressPostModel); bool isSaveUserAddress = await _userAddressService.AddUserAddress(userAddress); if (isSaveUserAddress) { return(Ok(new { data = userAddress })); } else { return(Ok(new { error_message = "Co loi khi them dia chi moi" })); } }
internal async Task <object> GetCityAndDistrictAsync(UserAddressPostModel userAddressPostModel) { return(await _bookstoreContext.Ward .Where(c => c.Id == userAddressPostModel.WardId) .Where(c => c.DistrictAddressId == userAddressPostModel.DistrictAddressId) .Where(c => c.CityAddressId == userAddressPostModel.CityAddressId).FirstOrDefaultAsync()); }
public async Task <ActionResult> GetDistrict(int id, UserAddressPostModel userAddressPostModel) { UserAddress userAddress = await _context.UserAddress.Where(u => u.UserId == id).FirstOrDefaultAsync(); if (userAddress is null) { return(Ok(new { error_message = "Khong ton tai dia chi cua user nay" })); } if (userAddressPostModel.Name is not null) { userAddress.Name = userAddressPostModel.Name; } if (userAddressPostModel.Phone is not null) { userAddress.Phone = userAddressPostModel.Phone; } if (userAddressPostModel.Street_Address is not null) { userAddress.Street_Address = userAddressPostModel.Street_Address; } userAddress.CityAddressId = userAddressPostModel.CityAddressId; userAddress.DistrictAddressId = userAddressPostModel.DistrictAddressId; userAddress.WardId = userAddressPostModel.WardId; await(_ = _context.SaveChangesAsync()); return(Ok(new{ data = "Cap nhat thanh cong", success = true })); }
internal bool IsValidAddress(UserAddressPostModel userAddressPostModel) { return(!(userAddressPostModel.CityAddressId <= 0 && userAddressPostModel.DistrictAddressId <= 0 && userAddressPostModel.UserId <= 0)); }