public bool UpdateQARemarkData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.SampleQARemarkDTO dtoRemark = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.SampleQARemarkDTO>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (SampleItemMngEntities context = CreateContext()) { SampleProduct dbProduct = context.SampleProduct.FirstOrDefault(o => o.SampleProductID == dtoRemark.SampleProductID); if (dbProduct == null) { throw new Exception("Sample not found"); } SampleQARemark dbItem = new SampleQARemark(); dbProduct.SampleQARemark.Add(dbItem); dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; converter.DTO2DB_SampleQARemark(dtoRemark, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", userId); //// notification //SendNotification(context); // update context.SaveChanges(); int newID = dbItem.SampleQARemarkID; dtoItem = converter.DB2DTO_SampleQARemark(context.SampleItemMng_SampleQARemark_View.Include("SampleItemMng_SampleQARemarkImage_View").FirstOrDefault(o => o.SampleQARemarkID == newID)); return(true); } } catch (Exception ex) { notification = new Library.DTO.Notification() { Message = ex.Message, Type = Library.DTO.NotificationType.Error }; return(false); } }
public void DTO2DB_SampleQARemark(DTO.SampleQARemarkDTO dtoItem, ref SampleQARemark dbItem, string TmpFile, int userId) { // remark AutoMapper.Mapper.Map <DTO.SampleQARemarkDTO, SampleQARemark>(dtoItem, dbItem); // remark image foreach (SampleQARemarkImage dbImage in dbItem.SampleQARemarkImage.ToArray()) { if (!dtoItem.SampleQARemarkImageDTOs.Select(o => o.SampleQARemarkImageID).Contains(dbImage.SampleQARemarkImageID)) { if (!string.IsNullOrEmpty(dbImage.FileUD)) { // remove image file fwFactory.RemoveImageFile(dbImage.FileUD); } dbItem.SampleQARemarkImage.Remove(dbImage); } } foreach (DTO.SampleQARemarkImageDTO dtoImage in dtoItem.SampleQARemarkImageDTOs) { SampleQARemarkImage dbImage; if (dtoImage.SampleQARemarkImageID <= 0) { dbImage = new SampleQARemarkImage(); dbItem.SampleQARemarkImage.Add(dbImage); } else { dbImage = dbItem.SampleQARemarkImage.FirstOrDefault(o => o.SampleQARemarkImageID == dtoImage.SampleQARemarkImageID); } if (dbImage != null) { // change or add images if (dtoImage.HasChanged) { dbImage.FileUD = fwFactory.CreateNoneImageFilePointer(TmpFile, dtoImage.NewFile, dtoImage.FileUD, dtoImage.FriendlyName); } AutoMapper.Mapper.Map <DTO.SampleQARemarkImageDTO, SampleQARemarkImage>(dtoImage, dbImage); } } }
public bool DeleteQARemark(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 SampleQARemark dbItem = context.SampleQARemark.FirstOrDefault(o => o.SampleQARemarkID == id); if (dbItem == null) { throw new Exception("Remark not found"); } // everything ok, delete // remove attached file foreach (SampleQARemarkImage dbImage in dbItem.SampleQARemarkImage.ToArray()) { if (!string.IsNullOrEmpty(dbImage.FileUD)) { fwFactory.RemoveImageFile(dbImage.FileUD); } dbItem.SampleQARemarkImage.Remove(dbImage); } context.SampleQARemarkImage.Local.Where(o => o.SampleQARemark == null).ToList().ForEach(o => context.SampleQARemarkImage.Remove(o)); context.SampleQARemark.Remove(dbItem); context.SaveChanges(); } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } return(true); }