Esempio n. 1
0
        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.FactoryMaterial dtoFactoryMaterial = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryMaterial>();
            try
            {
                using (FactoryMaterialEntities context = CreateContext())
                {
                    FactoryMaterial dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new FactoryMaterial();
                        context.FactoryMaterial.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.FactoryMaterial.Where(o => o.FactoryMaterialID == id).FirstOrDefault();
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "data not found!";
                        return(false);
                    }
                    else
                    {
                        //convert dto to db
                        converter.DTO2DB_FactoryMaterial(dtoFactoryMaterial, ref dbItem, this._tempFolder);
                        //remove orphan item
                        context.FactoryMaterialImage.Local.Where(o => o.FactoryMaterial == null).ToList().ForEach(o => context.FactoryMaterialImage.Remove(o));
                        //save data
                        context.SaveChanges();
                        //get return data
                        dtoItem = GetData(dbItem.FactoryMaterialID, 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);
            }
        }
Esempio n. 2
0
        public void DTO2DB_FactoryMaterial(DTO.FactoryMaterial dtoItem, ref FactoryMaterial dbItem, string tempFolder)
        {
            Module.Framework.DAL.DataFactory fwFactory = new Module.Framework.DAL.DataFactory();
            if (dtoItem.FactoryMaterialImages != null)
            {
                foreach (var item in dbItem.FactoryMaterialImage.ToArray())
                {
                    if (!dtoItem.FactoryMaterialImages.Select(s => s.FactoryMaterialImageID).Contains(item.FactoryMaterialImageID))
                    {
                        dbItem.FactoryMaterialImage.Remove(item);
                    }
                }

                foreach (var item in dtoItem.FactoryMaterialImages)
                {
                    //modify dto image field
                    if (item.ImageFile_HasChange.HasValue && item.ImageFile_HasChange.Value)
                    {
                        item.ImageFile = fwFactory.CreateFilePointer(tempFolder, item.ImageFile_NewFile, item.ImageFile);
                    }

                    //read db image field
                    FactoryMaterialImage dbDetail;
                    if (item.FactoryMaterialImageID < 0)
                    {
                        dbDetail = new FactoryMaterialImage();
                        dbItem.FactoryMaterialImage.Add(dbDetail);
                    }
                    else
                    {
                        dbDetail = dbItem.FactoryMaterialImage.Where(o => o.FactoryMaterialImageID == item.FactoryMaterialImageID).FirstOrDefault();
                    }

                    //map from dto image field to db image field
                    if (dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.FactoryMaterialImage, FactoryMaterialImage>(item, dbDetail);
                    }
                }
            }
            AutoMapper.Mapper.Map <DTO.FactoryMaterial, FactoryMaterial>(dtoItem, dbItem);
        }