Пример #1
0
        public bool UpdateQARemarkData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.QARemark.RemarkDTO dtoRemark = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.QARemark.RemarkDTO>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (Sample3MngEntities context = CreateContext())
                {
                    SampleQARemark dbItem;
                    if (id <= 0)
                    {
                        dbItem = new SampleQARemark();
                        context.SampleQARemark.Add(dbItem);
                        dbItem.UpdatedBy   = userId;
                        dbItem.UpdatedDate = DateTime.Now;
                    }
                    else
                    {
                        dbItem = context.SampleQARemark.FirstOrDefault(o => o.SampleQARemarkID == id);
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "QA Remark not found!";
                        return(false);
                    }
                    else
                    {
                        // check permission
                        if (userId != dbItem.UpdatedBy)
                        {
                            throw new Exception("Can not edit remark from other user!");
                        }
                        if ((DateTime.Now - dbItem.UpdatedDate.Value).TotalDays > 1)
                        {
                            throw new Exception("Too late to edit this remark, remark can only be updatable within 2 days from the last update!");
                        }
                        converter.DTO2DB_QARemark(dtoRemark, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\");
                        context.SaveChanges();
                    }

                    dtoItem = converter.DB2DTO_QARemark_Remark(context.Sample3Mng_QARemark_Remark_View
                                                               .Include("Sample3Mng_QARemark_RemarkImage_View")
                                                               .FirstOrDefault(o => o.SampleQARemarkID == dbItem.SampleQARemarkID));

                    return(true);
                }
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message, Type = Library.DTO.NotificationType.Error
                };
                return(false);
            }
        }
Пример #2
0
        public bool DeleteQARemarkData(int userId, int id, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (Sample3MngEntities context = CreateContext())
                {
                    SampleQARemark dbItem = context.SampleQARemark.FirstOrDefault(o => o.SampleQARemarkID == id);
                    if (dbItem == null)
                    {
                        throw new Exception("QA Remark not found!");
                    }
                    else
                    {
                        // check permission
                        if (userId != dbItem.UpdatedBy)
                        {
                            throw new Exception("Can not delete remark from other user!");
                        }
                        if ((DateTime.Now - dbItem.UpdatedDate.Value).TotalDays > 1)
                        {
                            throw new Exception("Too late to delete this remark, remark can only be deleted within 2 days from the last update!");
                        }

                        foreach (SampleQARemarkImage dbImage in dbItem.SampleQARemarkImage.ToArray())
                        {
                            if (!string.IsNullOrEmpty(dbImage.FileUD))
                            {
                                fwFactory.RemoveImageFile(dbImage.FileUD);
                            }
                            context.SampleQARemarkImage.Remove(dbImage);
                        }
                        context.SampleQARemark.Remove(dbItem);
                        context.SaveChanges();
                    }
                    return(true);
                }
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message, Type = Library.DTO.NotificationType.Error
                };
                return(false);
            }
        }
Пример #3
0
        public void DTO2DB_QARemark(DTO.QARemark.RemarkDTO dtoItem, ref SampleQARemark dbItem, string TmpFile)
        {
            AutoMapper.Mapper.Map <DTO.QARemark.RemarkDTO, SampleQARemark>(dtoItem, dbItem);

            // remark image
            foreach (SampleQARemarkImage dbImage in dbItem.SampleQARemarkImage.ToArray())
            {
                if (!dtoItem.RemarkImageDTOs.Select(o => o.SampleQARemarkImageID).Contains(dbImage.SampleQARemarkImageID))
                {
                    // delete files
                    if (!string.IsNullOrEmpty(dbImage.FileUD))
                    {
                        fwFactory.RemoveImageFile(dbImage.FileUD);
                    }
                    dbItem.SampleQARemarkImage.Remove(dbImage);
                }
            }
            foreach (DTO.QARemark.RemarkImageDTO dtoImage in dtoItem.RemarkImageDTOs)
            {
                SampleQARemarkImage dbImage;
                if (dtoImage.SampleQARemarkImageID <= 0)
                {
                    dbImage = new SampleQARemarkImage();
                    dbItem.SampleQARemarkImage.Add(dbImage);
                }
                else
                {
                    dbImage = dbItem.SampleQARemarkImage.FirstOrDefault(o => o.SampleQARemarkImageID == dtoImage.SampleQARemarkImageID);
                }

                if (dbItem != null)
                {
                    AutoMapper.Mapper.Map <DTO.QARemark.RemarkImageDTO, SampleQARemarkImage>(dtoImage, dbImage);
                    if (dtoImage.HasChanged && !string.IsNullOrEmpty(dtoImage.NewFile))
                    {
                        dbImage.FileUD = fwFactory.CreateNoneImageFilePointer(TmpFile, dtoImage.NewFile, dbImage.FileUD, dtoImage.FriendlyName);
                    }
                }
            }
        }