public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; DTO.FactoryOrderNorm dtoFactoryOrderNorm = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryOrderNorm>(); try { using (FactoryOrderNormEntities context = CreateContext()) { FactoryOrderNorm dbItem = null; if (id > 0) { dbItem = context.FactoryOrderNorm.Where(o => o.FactoryOrderNormID == id).FirstOrDefault(); } else { dbItem = new FactoryOrderNorm(); context.FactoryOrderNorm.Add(dbItem); } if (dbItem == null) { notification.Message = "data not found!"; return(false); } else { //convert dto to db converter.DTO2DB_FactoryOrderNorm(dtoFactoryOrderNorm, ref dbItem); //remove orphan item context.FactoryMaterialOrderNorm.Local.Where(o => o.FactoryFinishedProductOrderNorm == null).ToList().ForEach(o => context.FactoryMaterialOrderNorm.Remove(o)); context.FactoryFinishedProductOrderNorm.Local.Where(o => o.FactoryOrderNorm == null).ToList().ForEach(o => context.FactoryFinishedProductOrderNorm.Remove(o)); //save data context.SaveChanges(); //get return data dtoItem = GetData(dbItem.FactoryOrderNormID, out notification).Data; 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 DTO.FactoryOrderNorm DB2DTO_FactoryOrderNorm(FactoryOrderNormMng_FactoryOrderNorm_View dbItem) { DTO.FactoryOrderNorm orderNorm = AutoMapper.Mapper.Map <FactoryOrderNormMng_FactoryOrderNorm_View, DTO.FactoryOrderNorm>(dbItem); DTO.FactoryFinishedProductOrderNorm parentNormItem; foreach (var item in orderNorm.FactoryFinishedProductOrderNorms.ToArray()) { if (item.ParentNormID != null) { //get parent norm item parentNormItem = orderNorm.FactoryFinishedProductOrderNorms.Where(o => o.FactoryFinishedProductOrderNormID == item.ParentNormID).FirstOrDefault(); //push sub component norm to parent norm item if (parentNormItem.FactoryFinishedProductOrderNorms == null) { parentNormItem.FactoryFinishedProductOrderNorms = new List <DTO.FactoryFinishedProductOrderNorm>(); } parentNormItem.FactoryFinishedProductOrderNorms.Add(item); //remove item orderNorm.FactoryFinishedProductOrderNorms.Remove(item); } } return(orderNorm); }
public void DTO2DB_FactoryOrderNorm(DTO.FactoryOrderNorm dtoItem, ref FactoryOrderNorm dbItem) { //if (dtoItem.FactoryFinishedProductOrderNorms != null) //{ // //user remove factory finished product in dto => check to remove in db // foreach (var item in dbItem.FactoryFinishedProductOrderNorm.ToArray()) // { // if (!dtoItem.FactoryFinishedProductOrderNorms.Select(s => s.FactoryFinishedProductOrderNormID).Contains(item.FactoryFinishedProductOrderNormID)) // { // foreach (var mItem in item.FactoryMaterialOrderNorm.ToArray()) // { // item.FactoryMaterialOrderNorm.Remove(mItem); // } // dbItem.FactoryFinishedProductOrderNorm.Remove(item); // } // else // { // //user remove factory material in dto => check to remove in db // foreach (var mItem in item.FactoryMaterialOrderNorm.ToArray()) // { // var x = dtoItem.FactoryFinishedProductOrderNorms.Where(o => o.FactoryFinishedProductOrderNormID == item.FactoryFinishedProductOrderNormID).FirstOrDefault().FactoryMaterialOrderNorms; // if (!x.Select(s => s.FactoryMaterialOrderNormID).Contains(mItem.FactoryMaterialOrderNormID)) // { // item.FactoryMaterialOrderNorm.Remove(mItem); // } // } // } // } // //read factory finished product // FactoryFinishedProductOrderNorm dbFinishedProductNorm; // FactoryMaterialOrderNorm dbMaterialNorm; // foreach (var item in dtoItem.FactoryFinishedProductOrderNorms) // { // if (item.FactoryFinishedProductOrderNormID > 0) // { // dbFinishedProductNorm = dbItem.FactoryFinishedProductOrderNorm.Where(o => o.FactoryFinishedProductOrderNormID == item.FactoryFinishedProductOrderNormID).FirstOrDefault(); // if (dbFinishedProductNorm != null && item.FactoryMaterialOrderNorms != null) // { // foreach (var mItem in item.FactoryMaterialOrderNorms) // { // if (mItem.FactoryMaterialOrderNormID > 0) // { // dbMaterialNorm = dbFinishedProductNorm.FactoryMaterialOrderNorm.Where(o => o.FactoryMaterialOrderNormID == mItem.FactoryMaterialOrderNormID).FirstOrDefault(); // } // else // { // dbMaterialNorm = new FactoryMaterialOrderNorm(); // dbFinishedProductNorm.FactoryMaterialOrderNorm.Add(dbMaterialNorm); // } // if (dbMaterialNorm != null) // { // AutoMapper.Mapper.Map<DTO.FactoryMaterialOrderNorm, FactoryMaterialOrderNorm>(mItem, dbMaterialNorm); // } // } // } // } // else // { // dbFinishedProductNorm = new FactoryFinishedProductOrderNorm(); // dbItem.FactoryFinishedProductOrderNorm.Add(dbFinishedProductNorm); // if (item.FactoryMaterialOrderNorms != null) // { // foreach (var mItem in item.FactoryMaterialOrderNorms) // { // dbMaterialNorm = new FactoryMaterialOrderNorm(); // dbFinishedProductNorm.FactoryMaterialOrderNorm.Add(dbMaterialNorm); // AutoMapper.Mapper.Map<DTO.FactoryMaterialOrderNorm, FactoryMaterialOrderNorm>(mItem, dbMaterialNorm); // } // } // } // //mapping // if (dbFinishedProductNorm != null) // { // AutoMapper.Mapper.Map<DTO.FactoryFinishedProductOrderNorm, FactoryFinishedProductOrderNorm>(item, dbFinishedProductNorm); // } // } //} AutoMapper.Mapper.Map <DTO.FactoryOrderNorm, FactoryOrderNorm>(dtoItem, dbItem); }