public void DTO2DB(DTO.PLC dtoItem, ref PLC dbItem, string _tempFolder) { // map fields AutoMapper.Mapper.Map <DTO.PLC, PLC>(dtoItem, dbItem); dbItem.UpdatedDate = DateTime.Now; // map images if (dtoItem.PLCImages != null) { // check for child rows deleted foreach (PLCImage dbImage in dbItem.PLCImage.ToArray()) { if (!dtoItem.PLCImages.Select(o => o.PLCImageID).Contains(dbImage.PLCImageID)) { if (dbImage.ImageFile != string.Empty) { (new Module.Framework.DAL.DataFactory()).RemoveImageFile(dbImage.ImageFile); } dbItem.PLCImage.Remove(dbImage); } } // map child rows foreach (DTO.PLCImage dtoImage in dtoItem.PLCImages) { PLCImage dbImage; if (dtoImage.PLCImageID <= 0) { dbImage = new PLCImage(); dbItem.PLCImage.Add(dbImage); } else { dbImage = dbItem.PLCImage.FirstOrDefault(o => o.PLCImageID == dtoImage.PLCImageID); } if (dbImage != null) { AutoMapper.Mapper.Map <DTO.PLCImage, PLCImage>(dtoImage, dbImage); if (dtoImage.ImageFile_HasChange) { dbImage.ImageFile = (new Module.Framework.DAL.DataFactory()).CreateFilePointer(_tempFolder, dtoImage.ImageFile_NewFile, dtoImage.ImageFile); } } } } }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.PLC dtoPLC = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.PLC>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (FactoryPLCMngEntities context = CreateContext()) { // check if user can update this information int?permissionResult = context.FactoryPLCMng_function_CheckUserPermission(userId, dtoPLC.OfferLineID, dtoPLC.OfferLineSparepartID, dtoPLC.BookingID).FirstOrDefault(); if (!permissionResult.HasValue && permissionResult.Value == 0) { throw new Exception("Current user dont have permission to edit this data (data permission validation failed)"); } var checkImg = dtoPLC.PLCImages.FirstOrDefault(o => o.ImageTypeID == 15); if (checkImg == null) { throw new Exception("Please add Overral Image !!!"); } PLC dbItem = null; if (id == 0) { if (!dtoPLC.BookingID.HasValue || !dtoPLC.FactoryID.HasValue) { throw new Exception("You have to select Booking and Factory before create PLC"); } int bookingID = dtoPLC.BookingID.Value; int factoryID = dtoPLC.FactoryID.Value; if (dtoPLC.OfferLineID.HasValue) { int offerLineID = dtoPLC.OfferLineID.Value; if (context.PLC.FirstOrDefault(o => o.OfferLineID == offerLineID && o.BookingID == bookingID && o.FactoryID == factoryID) != null) { throw new Exception("Item already has PLC"); } } else if (dtoPLC.OfferLineSparepartID.HasValue) { int offerLineSparepartID = dtoPLC.OfferLineSparepartID.Value; if (context.PLC.FirstOrDefault(o => o.OfferLineSparepartID == offerLineSparepartID && o.BookingID == bookingID && o.FactoryID == factoryID) != null) { throw new Exception("Item already has PLC"); } } else if (dtoPLC.OfferLineSampleProductID.HasValue) { int offerLineSampleProductID = dtoPLC.OfferLineSampleProductID.Value; if (context.PLC.FirstOrDefault(o => o.OfferLineSampleProductID == offerLineSampleProductID && o.BookingID == bookingID && o.FactoryID == factoryID) != null) { throw new Exception("Item already has PLC"); } } dbItem = new PLC(); context.PLC.Add(dbItem); } else { dbItem = context.PLC.FirstOrDefault(o => o.PLCID == id); } if (dbItem == null) { notification.Message = "PLC not found!"; return(false); } else { // check concurrency if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoPLC.ConcurrencyFlag))) { throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT); } converter.DTO2DB(dtoPLC, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\"); dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; // remove orphan context.PLCImage.Local.Where(o => o.PLC == null).ToList().ForEach(o => context.PLCImage.Remove(o)); context.SaveChanges(); dtoItem = GetData(userId, dbItem.PLCID, -1, -1, -1, -1, -1, out notification).Data; return(true); } } } catch (Exception ex) { notification = new Library.DTO.Notification() { Message = ex.Message, Type = Library.DTO.NotificationType.Error }; return(false); } }