public override bool DeleteData(int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (WarehouseCIMngEntities context = CreateContext()) { WarehouseCI dbItem = context.WarehouseCI.FirstOrDefault(o => o.WarehouseCIID == id); if (dbItem == null) { notification.Message = "Invoice not found!"; return(false); } else { List <WarehouseCIDetail> need_delete_details = new List <WarehouseCIDetail>(); foreach (WarehouseCIDetail dbDetail in dbItem.WarehouseCIDetail) { need_delete_details.Add(dbDetail); } List <WarehouseCIExtDetail> need_delete_extdetail = new List <WarehouseCIExtDetail>(); foreach (WarehouseCIExtDetail dbExtDetail in dbItem.WarehouseCIExtDetail) { need_delete_extdetail.Add(dbExtDetail); } foreach (var item in need_delete_details) { context.WarehouseCIDetail.Remove(item); } foreach (var item in need_delete_extdetail) { context.WarehouseCIExtDetail.Remove(item); } context.WarehouseCI.Remove(dbItem); context.SaveChanges(); return(true); } } } catch (Exception ex) { notification = new Library.DTO.Notification() { Message = ex.Message, Type = Library.DTO.NotificationType.Warning }; return(false); } }
public void DTO2DB_WarehouseCI(DTO.WarehouseCIMng.WarehouseCI dtoItem, ref WarehouseCI dbItem) { /* * MAP & CHECK WarehouseCIDetail DELETED */ List <WarehouseCIDetail> ItemNeedDelete_Detail = new List <WarehouseCIDetail>(); if (dtoItem.WarehouseCIDetails != null) { //CHECK foreach (WarehouseCIDetail dbDetail in dbItem.WarehouseCIDetail.Where(o => !dtoItem.WarehouseCIDetails.Select(s => s.WarehouseCIDetailID).Contains(o.WarehouseCIDetailID))) { ItemNeedDelete_Detail.Add(dbDetail); } foreach (WarehouseCIDetail dbDetail in ItemNeedDelete_Detail) { dbItem.WarehouseCIDetail.Remove(dbDetail); } //MAP foreach (DTO.WarehouseCIMng.WarehouseCIDetail dtoDetail in dtoItem.WarehouseCIDetails) { WarehouseCIDetail dbDetail; if (dtoDetail.WarehouseCIDetailID < 0) { dbDetail = new WarehouseCIDetail(); dbItem.WarehouseCIDetail.Add(dbDetail); } else { dbDetail = dbItem.WarehouseCIDetail.FirstOrDefault(o => o.WarehouseCIDetailID == dtoDetail.WarehouseCIDetailID); } if (dbDetail != null) { AutoMapper.Mapper.Map <DTO.WarehouseCIMng.WarehouseCIDetail, WarehouseCIDetail>(dtoDetail, dbDetail); } } } /* * MAP & CHECK WarehouseCIExtDetail DELETED */ List <WarehouseCIExtDetail> ItemNeedDelete_ExtDetail = new List <WarehouseCIExtDetail>(); if (dtoItem.WarehouseCIExtDetails != null) { //CHECK foreach (WarehouseCIExtDetail dbDetail in dbItem.WarehouseCIExtDetail.Where(o => !dtoItem.WarehouseCIExtDetails.Select(s => s.WarehouseCIExtDetailID).Contains(o.WarehouseCIExtDetailID))) { ItemNeedDelete_ExtDetail.Add(dbDetail); } foreach (WarehouseCIExtDetail dbDetail in ItemNeedDelete_ExtDetail) { dbItem.WarehouseCIExtDetail.Remove(dbDetail); } //MAP foreach (DTO.WarehouseCIMng.WarehouseCIExtDetail dtoDetail in dtoItem.WarehouseCIExtDetails) { WarehouseCIExtDetail dbDetail; if (dtoDetail.WarehouseCIExtDetailID < 0) { dbDetail = new WarehouseCIExtDetail(); dbItem.WarehouseCIExtDetail.Add(dbDetail); } else { dbDetail = dbItem.WarehouseCIExtDetail.FirstOrDefault(o => o.WarehouseCIExtDetailID == dtoDetail.WarehouseCIExtDetailID); } if (dbDetail != null) { AutoMapper.Mapper.Map <DTO.WarehouseCIMng.WarehouseCIExtDetail, WarehouseCIExtDetail>(dtoDetail, dbDetail); } } } if (!string.IsNullOrEmpty(dtoItem.IssuedDateFormated)) { dtoItem.IssuedDate = DateTime.ParseExact(dtoItem.IssuedDateFormated, "d", new System.Globalization.CultureInfo("vi-VN")); } AutoMapper.Mapper.Map <DTO.WarehouseCIMng.WarehouseCI, WarehouseCI>(dtoItem, dbItem); }
public override bool UpdateData(int id, ref DTO.WarehouseCIMng.WarehouseCI dtoItem, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (WarehouseCIMngEntities context = CreateContext()) { WarehouseCI dbItem = null; if (id == 0) { dbItem = new WarehouseCI(); context.WarehouseCI.Add(dbItem); } else { dbItem = context.WarehouseCI.FirstOrDefault(o => o.WarehouseCIID == id); } if (dbItem == null) { notification.Message = "Invoice not found!"; return(false); } else { // check concurrency if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoItem.ConcurrencyFlag_String))) { throw new Exception(DALBase.Helper.TEXT_CONCURRENCY_CONFLICT); } converter.DTO2DB_WarehouseCI(dtoItem, ref dbItem); if (id == 0) { dbItem.CreatedBy = dtoItem.UpdatedBy; dbItem.CreatedDate = DateTime.Now; } else { dbItem.UpdatedBy = dtoItem.UpdatedBy; dbItem.UpdatedDate = DateTime.Now; } context.SaveChanges(); dtoItem = GetData(dbItem.WarehouseCIID, out notification); return(true); } } } catch (Exception ex) { notification = new Library.DTO.Notification() { Message = ex.InnerException.InnerException.Message, Type = Library.DTO.NotificationType.Warning }; return(false); } }