Exemple #1
0
        public void DTO2DB_FactoryStepNorm(DTO.FactoryStepNorm dtoItem, ref FactoryStepNorm dbItem)
        {
            if (dtoItem.FactoryStepNormDetails != null)
            {
                foreach (var item in dbItem.FactoryStepNormDetail.ToArray())
                {
                    if (!dtoItem.FactoryStepNormDetails.Select(s => s.FactoryStepNormDetailID).Contains(item.FactoryStepNormDetailID))
                    {
                        dbItem.FactoryStepNormDetail.Remove(item);
                    }
                }

                foreach (var item in dtoItem.FactoryStepNormDetails)
                {
                    //read db image field
                    FactoryStepNormDetail dbDetail;
                    if (item.FactoryStepNormDetailID < 0)
                    {
                        dbDetail = new FactoryStepNormDetail();
                        dbItem.FactoryStepNormDetail.Add(dbDetail);
                    }
                    else
                    {
                        dbDetail = dbItem.FactoryStepNormDetail.Where(o => o.FactoryStepNormDetailID == item.FactoryStepNormDetailID).FirstOrDefault();
                    }

                    //map from dto image field to db image field
                    if (dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.FactoryStepNormDetail, FactoryStepNormDetail>(item, dbDetail);
                    }
                }
            }
            AutoMapper.Mapper.Map <DTO.FactoryStepNorm, FactoryStepNorm>(dtoItem, dbItem);
        }
        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.FactoryStepNorm dtoFactoryStepNorm = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryStepNorm>();
            try
            {
                using (FactoryStepNormEntities context = CreateContext())
                {
                    FactoryStepNorm dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new FactoryStepNorm();
                        context.FactoryStepNorm.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.FactoryStepNorm.Where(o => o.FactoryStepNormID == id).FirstOrDefault();
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "data not found!";
                        return(false);
                    }
                    else
                    {
                        //convert dto to db
                        converter.DTO2DB_FactoryStepNorm(dtoFactoryStepNorm, ref dbItem);
                        //remove orphan item
                        context.FactoryStepNormDetail.Local.Where(o => o.FactoryStepNorm == null).ToList().ForEach(o => context.FactoryStepNormDetail.Remove(o));
                        //save data
                        context.SaveChanges();
                        //get return data
                        dtoItem = GetData(dbItem.FactoryStepNormID, 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);
            }
        }