コード例 #1
0
ファイル: WardService.cs プロジェクト: Uaena1811/Old-Project
        private async Task <BaseResult> Update(Ward ward, int updateBy = 0, string updateByUserName = "")
        {
            var result = new BaseResult()
            {
                Result = Result.Success
            };
            var wardForUpdate = _wardRepository.Query().FirstOrDefault(c => c.Id == ward.Id);

            if (wardForUpdate == null || ward.Id <= 0)
            {
                result.Result  = Result.Failed;
                result.Message = "Không tìm thấy phường yêu cầu!";
                return(result);
            }
            else
            {
                var district = _districtRepository.Query()
                               .Include(d => d.City)
                               .Include(d => d.Wards)
                               .AsNoTracking()
                               .FirstOrDefault(d => d.Id == ward.ParentId);
                var exists = _wardRepository.Query()
                             .Include(w => w.District)
                             .ThenInclude(d => d.City)
                             .AsNoTracking()
                             .Any(w => !w.IsDeleted && w.District.Name == district.Name && w.Name == ward.Name && w.Id != wardForUpdate.Id);
                if (exists)
                {
                    result.Result  = Result.Failed;
                    result.Message = "Xã/Phường đã tồn tại!";
                    return(result);
                }
                ward.CityRealm  = district.CityRealm;
                ward.Name       = ward.Name.Trim();
                ward.UnsignName = !string.IsNullOrEmpty(ward.Name) ? ward.Name.Unsigned() : "";
            }
            try
            {
                wardForUpdate = ward.ToWard(wardForUpdate);

                //Cập nhật thông tin chung của thực thể
                wardForUpdate = wardForUpdate.UpdateCommonInt(updateBy, updateByUserName);

                await _wardRepository.UpdateAsync(wardForUpdate);

                var warehouses = _wareHouseRepository.Query()
                                 .Where(w => w.ParentId == wardForUpdate.Id)
                                 .Include(w => w.Ward)
                                 .ThenInclude(w => w.District)
                                 .ThenInclude(d => d.City)
                                 .ToList();
                foreach (WareHouse w in warehouses)
                {
                    WareHouseModel wareHouse = w.ToModel();
                    await _wareHouseService.CreateOrUpdate(wareHouse);
                }
            }
            catch (Exception e)
            {
                result.Result  = Result.SystemError;
                result.Message = e.ToString();
            }
            return(result);
        }
コード例 #2
0
        public async Task <IActionResult> CreateOrUpdate([FromBody] WareHouseModel wareHouse)
        {
            var result = await _wareHouseService.CreateOrUpdate(wareHouse);

            return(Ok(result));
        }