public bool UpdateProgressData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.SampleProgressDTO dtoProgress = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.SampleProgressDTO>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (SampleItemMngEntities context = CreateContext()) { SampleProgress dbItem = null; if (id <= 0) { dbItem = new SampleProgress(); context.SampleProgress.Add(dbItem); } else { dbItem = context.SampleProgress.FirstOrDefault(o => o.SampleProgressID == id); } dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; if (dbItem == null) { notification.Message = "Sample Progress not found!"; return(false); } else { converter.DTO2DB_SampleProgress(dtoProgress, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", userId); //// notification //SendNotification(context); // remove orphan context.SampleProgressImage.Local.Where(o => o.SampleProgress == null).ToList().ForEach(o => context.SampleProgressImage.Remove(o)); context.SaveChanges(); int newID = dbItem.SampleProgressID; dtoItem = converter.DB2DTO_SampleProgress(context.SampleItemMng_SampleProgress_View.Include("SampleItemMng_SampleProgressImage_View").FirstOrDefault(o => o.SampleProgressID == newID)); return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = Library.Helper.HandleExceptionSingleLine(ex); return(false); } }
public void DTO2DB_SampleProgress(DTO.SampleProgressDTO dtoItem, ref SampleProgress dbItem, string TmpFile, int userId) { // progress AutoMapper.Mapper.Map <DTO.SampleProgressDTO, SampleProgress>(dtoItem, dbItem); // progress image foreach (SampleProgressImage dbImage in dbItem.SampleProgressImage.ToArray()) { if (!dtoItem.SampleProgressImageDTOs.Select(o => o.SampleProgressImageID).Contains(dbImage.SampleProgressImageID)) { if (!string.IsNullOrEmpty(dbImage.FileUD)) { // remove image file fwFactory.RemoveImageFile(dbImage.FileUD); } dbItem.SampleProgressImage.Remove(dbImage); } } foreach (DTO.SampleProgressImageDTO dtoImage in dtoItem.SampleProgressImageDTOs) { SampleProgressImage dbImage; if (dtoImage.SampleProgressImageID <= 0) { dbImage = new SampleProgressImage(); dbItem.SampleProgressImage.Add(dbImage); } else { dbImage = dbItem.SampleProgressImage.FirstOrDefault(o => o.SampleProgressImageID == dtoImage.SampleProgressImageID); } if (dbImage != null) { // change or add images if (dtoImage.HasChanged) { dbImage.FileUD = fwFactory.CreateFilePointer(TmpFile, dtoImage.NewFile, dtoImage.FileUD); } AutoMapper.Mapper.Map <DTO.SampleProgressImageDTO, SampleProgressImage>(dtoImage, dbImage); } } }
public bool DeleteProgress(int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (SampleItemMngEntities context = CreateContext()) { // check if can delete SampleProgress dbItem = context.SampleProgress.FirstOrDefault(o => o.SampleProgressID == id); if (dbItem == null) { throw new Exception("Progress not found"); } // everything ok, delete // remove attached file foreach (SampleProgressImage dbImage in dbItem.SampleProgressImage.ToArray()) { if (!string.IsNullOrEmpty(dbImage.FileUD)) { fwFactory.RemoveImageFile(dbImage.FileUD); } dbItem.SampleProgressImage.Remove(dbImage); } context.SampleProgressImage.Local.Where(o => o.SampleProgress == null).ToList().ForEach(o => context.SampleProgressImage.Remove(o)); context.SampleProgress.Remove(dbItem); context.SaveChanges(); } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = Library.Helper.HandleExceptionSingleLine(ex); return(false); } return(true); }