public void UpdateProvince(ProvinceViewModel provoinceVm) { var province = Mapper.Map <ProvinceViewModel, Province>(provoinceVm); //District var newdistricts = province.Districts; //new districts added var addedDistricts = newdistricts.Where(x => x.Id == 0).ToList(); //get updated districts var updatedDistricts = newdistricts.Where(x => x.Id != 0).ToList(); //Existed districts var existedDistricts = _disctrictRepository.FindAll(x => x.ProvinceId == provoinceVm.Id); province.Districts.Clear(); //Ward var newwards = province.Wards; //new wards added var addedWards = newwards.Where(x => x.Id == 0).ToList(); //get updated wards var updatedWards = newwards.Where(x => x.Id != 0).ToList(); //Existed wards var existedWards = _wardRepository.FindAll(x => x.ProvinceId == provoinceVm.Id); province.Wards.Clear(); var newtreets = province.Streets; //new districts added var addedTreets = newtreets.Where(x => x.Id == 0).ToList(); //get updated districts var updatedTreets = newtreets.Where(x => x.Id != 0).ToList(); //Existed details var existedTreets = _streetRepository.FindAll(x => x.ProvinceId == provoinceVm.Id); //Clear db province.Streets.Clear(); foreach (var district in updatedDistricts) { _disctrictRepository.Update(district); } foreach (var ward in updatedWards) { _wardRepository.Update(ward); } foreach (var street in updatedTreets) { _streetRepository.Update(street); } }
public PagedResult <WardViewModel> GetAllPaging(string startDate, string endDate, string keyword, int provinceId, int districtId, int pageIndex, int pageSize) { var query = _wardRepository.FindAll(); if (!string.IsNullOrEmpty(startDate)) { DateTime start = DateTime.ParseExact(startDate, "dd/MM/yyyy", CultureInfo.GetCultureInfo("vi-VN")); query = query.Where(x => x.DateCreated >= start); } if (!string.IsNullOrEmpty(endDate)) { DateTime end = DateTime.ParseExact(endDate, "dd/MM/yyyy", CultureInfo.GetCultureInfo("vi-VN")); query = query.Where(x => x.DateCreated <= end); } if (!string.IsNullOrEmpty(keyword)) { query = query.Where(x => x.Name.Contains(keyword)); } if (provinceId != 0) { query = query.Where(x => x.ProvinceId == provinceId); } if (districtId != 0) { query = query.Where(x => x.DistrictId == districtId); } var totalRow = query.Count(); var data = query.OrderByDescending(x => x.DateCreated).Skip((pageIndex - 1) * pageSize).Take(pageSize) .ProjectTo <WardViewModel>().ToList(); return(new PagedResult <WardViewModel>() { CurrentPage = pageIndex, PageSize = pageSize, Results = data, RowCount = totalRow }); }