//UpdateData public void DTO2DB_UpdateSpecificationOfProduct(DTO.SpecificationOfProductDTO dtoItem, ref ProductSpecification dbItem) { //SpecificationCushionImage if (dtoItem.specificationCushionImageDTOs != null) { foreach (var item in dbItem.ProductSpecificationCushionImage.ToArray()) { if (!dtoItem.specificationCushionImageDTOs.Select(s => s.ProductSpecificationCushionImageID).Contains(item.ProductSpecificationCushionImageID)) { dbItem.ProductSpecificationCushionImage.Remove(item); } } foreach (var item in dtoItem.specificationCushionImageDTOs) { ProductSpecificationCushionImage dbSpecificationCushionImage = new ProductSpecificationCushionImage(); if (item.ProductSpecificationCushionImageID < 0) { dbItem.ProductSpecificationCushionImage.Add(dbSpecificationCushionImage); } else { dbSpecificationCushionImage = dbItem.ProductSpecificationCushionImage.Where(s => s.ProductSpecificationCushionImageID == item.ProductSpecificationCushionImageID).FirstOrDefault(); } if (dbSpecificationCushionImage != null) { AutoMapper.Mapper.Map <DTO.SpecificationCushionImageDTO, ProductSpecificationCushionImage>(item, dbSpecificationCushionImage); } } } //SpecificationImage if (dtoItem.specificationImageDTOs != null) { foreach (var item in dbItem.ProductSpecificationImage.ToArray()) { if (!dtoItem.specificationImageDTOs.Select(s => s.ProductSpecificationImageID).Contains(item.ProductSpecificationImageID)) { dbItem.ProductSpecificationImage.Remove(item); } } foreach (var item in dtoItem.specificationImageDTOs) { ProductSpecificationImage dbSpecificationImage = new ProductSpecificationImage(); if (item.ProductSpecificationImageID < 0) { dbItem.ProductSpecificationImage.Add(dbSpecificationImage); } else { dbSpecificationImage = dbItem.ProductSpecificationImage.Where(s => s.ProductSpecificationImageID == item.ProductSpecificationImageID).FirstOrDefault(); } if (dbSpecificationImage != null) { AutoMapper.Mapper.Map <DTO.SpecificationImageDTO, ProductSpecificationImage>(item, dbSpecificationImage); } } } //SpecificationWeavingFile if (dtoItem.specificationWeavingFileDTOs != null) { foreach (var item in dbItem.ProductSpecificationWeavingFile.ToArray()) { if (!dtoItem.specificationWeavingFileDTOs.Select(s => s.ProductSpecificationWeavingFileID).Contains(item.ProductSpecificationWeavingFileID)) { dbItem.ProductSpecificationWeavingFile.Remove(item); } } foreach (var item in dtoItem.specificationWeavingFileDTOs) { ProductSpecificationWeavingFile dbSpecificationWeavingFile = new ProductSpecificationWeavingFile(); if (item.ProductSpecificationWeavingFileID < 0) { dbItem.ProductSpecificationWeavingFile.Add(dbSpecificationWeavingFile); } else { dbSpecificationWeavingFile = dbItem.ProductSpecificationWeavingFile.Where(s => s.ProductSpecificationWeavingFileID == item.ProductSpecificationWeavingFileID).FirstOrDefault(); } if (dbSpecificationWeavingFile != null) { AutoMapper.Mapper.Map <DTO.SpecificationWeavingFileDTO, ProductSpecificationWeavingFile>(item, dbSpecificationWeavingFile); } } } //SpecificationPacking if (dtoItem.specificationPackingDTOs != null) { foreach (var item in dbItem.ProductSpecificationPacking.ToArray()) { if (!dtoItem.specificationPackingDTOs.Select(s => s.ProductSpecificationPackingID).Contains(item.ProductSpecificationPackingID)) { dbItem.ProductSpecificationPacking.Remove(item); } } foreach (var item in dtoItem.specificationPackingDTOs) { ProductSpecificationPacking dbSpecificationPacking = new ProductSpecificationPacking(); if (item.ProductSpecificationPackingID < 0) { dbItem.ProductSpecificationPacking.Add(dbSpecificationPacking); } else { dbSpecificationPacking = dbItem.ProductSpecificationPacking.Where(s => s.ProductSpecificationPackingID == item.ProductSpecificationPackingID).FirstOrDefault(); } if (dbSpecificationPacking != null) { AutoMapper.Mapper.Map <DTO.SpecificationPackingDTO, ProductSpecificationPacking>(item, dbSpecificationPacking); } } } //SpecificationWoodenPart if (dtoItem.specificationWoodenartDTOs != null) { foreach (var item in dbItem.ProductSpecificationWoodenPart.ToArray()) { if (!dtoItem.specificationWoodenartDTOs.Select(s => s.ProductSpecificationWoodenPartID).Contains(item.ProductSpecificationWoodenPartID)) { dbItem.ProductSpecificationWoodenPart.Remove(item); } } foreach (var item in dtoItem.specificationWoodenartDTOs) { ProductSpecificationWoodenPart dbSpecificationWoodenPart = new ProductSpecificationWoodenPart(); if (item.ProductSpecificationWoodenPartID < 0) { dbItem.ProductSpecificationWoodenPart.Add(dbSpecificationWoodenPart); } else { dbSpecificationWoodenPart = dbItem.ProductSpecificationWoodenPart.Where(s => s.ProductSpecificationWoodenPartID == item.ProductSpecificationWoodenPartID).FirstOrDefault(); } if (dbSpecificationWoodenPart != null) { AutoMapper.Mapper.Map <DTO.SpecificationWoodenartDTO, ProductSpecificationWoodenPart>(item, dbSpecificationWoodenPart); } } } AutoMapper.Mapper.Map <DTO.SpecificationOfProductDTO, ProductSpecification>(dtoItem, dbItem); }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification) { notification = new Notification() { Type = NotificationType.Success }; DTO.SpecificationOfProductDTO dtoItemSpec = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.SpecificationOfProductDTO>(); try { using (var context = CreateContext()) { ProductSpecification dbItem; if (id == 0) { dbItem = new ProductSpecification(); context.ProductSpecification.Add(dbItem); } else { dbItem = context.ProductSpecification.Where(o => o.ProductSpecificationID == id).FirstOrDefault(); } if (dbItem == null) { notification.Message = "Data Not Found !"; return(false); } else { //Upload File Module.Framework.DAL.DataFactory fwFactory = new Framework.DAL.DataFactory(); string tempFolder = FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\"; //for SpecificationImage foreach (DTO.SpecificationCushionImageDTO dtoSpecImage in dtoItemSpec.specificationCushionImageDTOs.Where(o => o.ScanHasChange)) { dtoSpecImage.FileUD = fwFactory.CreateFilePointer(tempFolder, dtoSpecImage.ScanNewFile, dtoSpecImage.FileUD, dtoSpecImage.FriendlyName); } //for SpecificationImage foreach (DTO.SpecificationImageDTO dtoSpecImage in dtoItemSpec.specificationImageDTOs.Where(o => o.ScanHasChange)) { dtoSpecImage.FileUD = fwFactory.CreateFilePointer(tempFolder, dtoSpecImage.ScanNewFile, dtoSpecImage.FileUD, dtoSpecImage.FriendlyName); } //for SpecWeavingFile foreach (DTO.SpecificationWeavingFileDTO dtoWeavingFile in dtoItemSpec.specificationWeavingFileDTOs.Where(o => o.ScanHasChange)) { dtoWeavingFile.FileUD = fwFactory.CreateFilePointer(tempFolder, dtoWeavingFile.ScanNewFile, dtoWeavingFile.FileUD, dtoWeavingFile.FriendlyName); } converter.DTO2DB_UpdateSpecificationOfProduct(dtoItemSpec, ref dbItem); //Remove context.ProductSpecificationCushionImage.Local.Where(o => o.ProductSpecification == null).ToList().ForEach(o => context.ProductSpecificationCushionImage.Remove(o)); context.ProductSpecificationImage.Local.Where(o => o.ProductSpecification == null).ToList().ForEach(o => context.ProductSpecificationImage.Remove(o)); context.ProductSpecificationPacking.Local.Where(o => o.ProductSpecification == null).ToList().ForEach(o => context.ProductSpecificationPacking.Remove(o)); context.ProductSpecificationWeavingFile.Local.Where(o => o.ProductSpecification == null).ToList().ForEach(o => context.ProductSpecificationWeavingFile.Remove(o)); context.ProductSpecificationWoodenPart.Local.Where(o => o.ProductSpecification == null).ToList().ForEach(o => context.ProductSpecificationWoodenPart.Remove(o)); dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; context.SaveChanges(); dtoItem = GetDataSpecification(userId, dbItem.ProductSpecificationID, dbItem.ProductID, null, 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); } }