private void UpdateDB(List <Department> fromClient, List <Department> fromDB) { if (fromClient == null || fromDB == null || fromClient.Count == 0) { return; } // we parse all the items and populate the status field // we do this if for optimization if (fromClient.Count > fromDB.Count) { foreach (Department dept in fromClient) { if (dept == null) { continue; } Department dbDept = fromDB.Where(dDB => dDB.Id == dept.Id).FirstOrDefault(); if (dbDept == null) { dept.Status = ElementStatus.New; int errorCount = DeptDAL.InsertDepartment(dept); _job.IncrementError(errorCount); _job.IncrementNew(errorCount); continue; } if (dept.DifferFrom(dbDept)) { dbDept.Status = dept.Status = ElementStatus.Update; int errorCount = DeptDAL.UpdateDepartment(dept, dbDept); _job.IncrementError(errorCount); _job.IncrementUpdated(errorCount); continue; } else { dbDept.Status = dept.Status = ElementStatus.Unchanged; continue; } } fromDB .Where(d => d.Status == null) .ToList() .ForEach(d => { if (d.DateRemoved == null) { d.Status = ElementStatus.Delete; int errorCount = DeptDAL.DeleteDepartment(d.RetailerId, d.Id); _job.IncrementError(errorCount); _job.IncrementUpdated(errorCount); } }); } else { foreach (Department dept in fromDB) { Department clientDept = fromClient.Where(d => d.Id == dept.Id).FirstOrDefault(); if (clientDept == null) { if (dept.DateRemoved == null) { dept.Status = ElementStatus.Delete; int errorCount = DeptDAL.DeleteDepartment(dept.RetailerId, dept.Id); _job.IncrementError(errorCount); _job.IncrementDeleted(errorCount); } continue; } if (dept.DifferFrom(clientDept)) { clientDept.Status = dept.Status = ElementStatus.Update; int errorCount = DeptDAL.UpdateDepartment(clientDept, dept); _job.IncrementError(errorCount); _job.IncrementUpdated(errorCount); continue; } else { clientDept.Status = dept.Status = ElementStatus.Unchanged; continue; } } fromClient .Where(d => d.Status == null) .ToList() .ForEach(d => { d.Status = ElementStatus.New; int errorCount = DeptDAL.InsertDepartment(d); _job.IncrementError(errorCount); _job.IncrementNew(errorCount); }); } }
private void UpdateDB(List <Item> fromClient, List <Item> fromDB) { if (fromClient == null || fromDB == null || fromClient.Count == 0) { return; } // we parse all the items and populate the status field // we do this if for optimization if (fromClient.Count > fromDB.Count) { foreach (Item item in fromClient) { if (item == null) { continue; } Item dbItem = fromDB.Where(iDB => iDB.SKU == item.SKU).FirstOrDefault(); if (dbItem == null) { item.Status = ElementStatus.New; int errorCount = ItmDAL.InsertItem(item); _job.IncrementError(errorCount); _job.IncrementNew(errorCount); continue; } if (item.DifferFrom(dbItem)) { dbItem.Status = item.Status = ElementStatus.Update; int errorCount = ItmDAL.UpdateItem(item, dbItem); _job.IncrementError(errorCount); _job.IncrementUpdated(errorCount); continue; } else { dbItem.Status = item.Status = ElementStatus.Unchanged; continue; } } fromDB .Where(i => i.Status == null) .ToList() .ForEach(i => { if (i.DateRemoved == null) { i.Status = ElementStatus.Delete; int errorCount = ItmDAL.DeleteItem(i.RetailerId, i.SKU); _job.IncrementError(errorCount); _job.IncrementDeleted(errorCount); } }); } else { foreach (Item item in fromDB) { Item clientItem = fromClient.Where(i => i != null && item != null && i.SKU == item.SKU).FirstOrDefault(); if (clientItem == null) { if (item.DateRemoved == null) { item.Status = ElementStatus.Delete; int errorCount = ItmDAL.DeleteItem(item.RetailerId, item.SKU); _job.IncrementError(errorCount); _job.IncrementDeleted(errorCount); } continue; } if (item.DifferFrom(clientItem)) { clientItem.Status = item.Status = ElementStatus.Update; int errorCount = ItmDAL.UpdateItem(clientItem, item); _job.IncrementError(errorCount); _job.IncrementUpdated(errorCount); continue; } else { clientItem.Status = item.Status = ElementStatus.Unchanged; continue; } } fromClient .Where(i => i != null && i.Status == null) .ToList() .ForEach(i => { i.Status = ElementStatus.New; int errorCount = ItmDAL.InsertItem(i); _job.IncrementError(errorCount); _job.IncrementNew(errorCount); }); } }