public void DTO2DB_DraftPackingList(DTO.DraftPackingListDTO dtoItem, ref DraftPackingList dbItem) { /* * MAP & CHECK PackingListDetail */ List <DraftPackingListDetail> ItemNeedDelete_Detail = new List <DraftPackingListDetail>(); if (dtoItem.DraftPackingListDetail != null) { //CHECK //foreach (DraftPackingListDetail dbDetail in dbItem.DraftPackingListDetail.Where(o => !dtoItem.DraftPackingListDetail.Select(s => s.DraftPackingListDetailID).Contains(o.DraftPackingListDetailID))) //{ // ItemNeedDelete_Detail.Add(dbDetail); //} //foreach (DraftPackingListDetail dbDetail in ItemNeedDelete_Detail) //{ // dbItem.DraftPackingListDetail.Remove(dbDetail); //} //MAP foreach (DTO.DraftPackingListDetail dtoDetail in dtoItem.DraftPackingListDetail) { DraftPackingListDetail dbDetail; if (dtoDetail.DraftPackingListDetailID < 0) { dbDetail = new DraftPackingListDetail(); dbItem.DraftPackingListDetail.Add(dbDetail); } else { dbDetail = dbItem.DraftPackingListDetail.FirstOrDefault(o => o.DraftPackingListDetailID == dtoDetail.DraftPackingListDetailID); } if (dbDetail != null) { AutoMapper.Mapper.Map <DTO.DraftPackingListDetail, DraftPackingListDetail>(dtoDetail, dbDetail); } } foreach (DTO.DraftPackingListSparepartDetail dtoSparePartDetail in dtoItem.DraftPackingListSparepartDetail) { DraftPackingListDetail dbDetail; if (dtoSparePartDetail.DraftPackingListDetailID < 0) { dbDetail = new DraftPackingListDetail(); dbItem.DraftPackingListDetail.Add(dbDetail); } else { dbDetail = dbItem.DraftPackingListDetail.FirstOrDefault(o => o.DraftPackingListDetailID == dtoSparePartDetail.DraftPackingListDetailID); } if (dbDetail != null) { AutoMapper.Mapper.Map <DTO.DraftPackingListSparepartDetail, DraftPackingListDetail>(dtoSparePartDetail, dbDetail); } } } AutoMapper.Mapper.Map <DTO.DraftPackingListDTO, DraftPackingList>(dtoItem, dbItem); }
public bool Delete(int id, out Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (DraftPackingListMngEntities context = CreateContext()) { DraftPackingList dbItem = context.DraftPackingList.FirstOrDefault(o => o.DraftPackingListID == id); if (dbItem == null) { notification.Message = "Invoice not found!"; return(false); } else { List <DraftPackingListDetail> need_delete_details = new List <DraftPackingListDetail>(); foreach (DraftPackingListDetail dbDetail in dbItem.DraftPackingListDetail) { need_delete_details.Add(dbDetail); } foreach (var item in need_delete_details) { context.DraftPackingListDetail.Remove(item); } context.DraftPackingList.Remove(dbItem); context.SaveChanges(); return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; notification.DetailMessage.Add(ex.Message); if (ex.GetBaseException() != null) { notification.DetailMessage.Add(ex.GetBaseException().Message); } return(false); } }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification) { notification = new Notification { Type = NotificationType.Success }; DTO.DraftPackingListDTO dtoDraftPackingList = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.DraftPackingListDTO>(); try { using (DraftPackingListMngEntities context = CreateContext()) { DraftPackingList dbItem = null; if (id == 0) { dbItem = new DraftPackingList(); context.DraftPackingList.Add(dbItem); } else { dbItem = context.DraftPackingList.FirstOrDefault(o => o.DraftPackingListID == id); } if (dbItem == null) { notification.Message = "Invoice not found!"; return(false); } else { converter.DTO2DB_DraftPackingList(dtoDraftPackingList, ref dbItem); if (id == 0) { dbItem.CreatedBy = userId; dbItem.CreatedDate = DateTime.Now; } else { dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; } context.DraftPackingListDetail.Local.Where(o => o.DraftPackingList == null).ToList().ForEach(o => context.DraftPackingListDetail.Remove(o)); context.SaveChanges(); Hashtable filter = new Hashtable(); filter["id"] = dbItem.DraftPackingListID; dtoItem = GetData(filter, out notification); return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; if (ex.InnerException != null && !string.IsNullOrEmpty(ex.InnerException.Message)) { notification.DetailMessage.Add(ex.InnerException.Message); } return(false); } }