public bool UpdateBuildingProcessData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.BuildingProcess.ProgressDTO dtoProgress = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.BuildingProcess.ProgressDTO>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (Sample3MngEntities context = CreateContext()) { SampleProgress dbItem; if (id <= 0) { dbItem = new SampleProgress(); context.SampleProgress.Add(dbItem); dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; } else { dbItem = context.SampleProgress.FirstOrDefault(o => o.SampleProgressID == id); } if (dbItem == null) { notification.Message = "Progress not found!"; return(false); } else { // check permission if (userId != dbItem.UpdatedBy) { throw new Exception("Can not edit progress created by other user!"); } if ((DateTime.Now - dbItem.UpdatedDate.Value).TotalDays > 1) { throw new Exception("Too late to edit this progress, progress can only be updatable within 2 days from the last update!"); } converter.DTO2DB_BuildingProcess(dtoProgress, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\"); context.SaveChanges(); } dtoItem = converter.DB2DTO_BuildingProcess_Progress(context.Sample3Mng_BuildingProcess_Progress_View .Include("Sample3Mng_BuildingProcess_ProgressImage_View") .FirstOrDefault(o => o.SampleProgressID == dbItem.SampleProgressID)); return(true); } } catch (Exception ex) { notification = new Library.DTO.Notification() { Message = ex.Message, Type = Library.DTO.NotificationType.Error }; return(false); } }
public bool DeleteBuildingProcessData(int userId, int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (Sample3MngEntities context = CreateContext()) { SampleProgress dbItem = context.SampleProgress.FirstOrDefault(o => o.SampleProgressID == id); if (dbItem == null) { throw new Exception("Progress not found!"); } else { // check permission if (userId != dbItem.UpdatedBy) { throw new Exception("Can not delete progress created by other user!"); } if ((DateTime.Now - dbItem.UpdatedDate.Value).TotalDays > 1) { throw new Exception("Too late to delete this progress, remark can only be deleted within 2 days from the last update!"); } foreach (SampleProgressImage dbImage in dbItem.SampleProgressImage.ToArray()) { if (!string.IsNullOrEmpty(dbImage.FileUD)) { fwFactory.RemoveImageFile(dbImage.FileUD); } context.SampleProgressImage.Remove(dbImage); } context.SampleProgress.Remove(dbItem); context.SaveChanges(); } return(true); } } catch (Exception ex) { notification = new Library.DTO.Notification() { Message = ex.Message, Type = Library.DTO.NotificationType.Error }; return(false); } }
public void DTO2DB_BuildingProcess(DTO.BuildingProcess.ProgressDTO dtoItem, ref SampleProgress dbItem, string TmpFile) { AutoMapper.Mapper.Map <DTO.BuildingProcess.ProgressDTO, SampleProgress>(dtoItem, dbItem); // remark image foreach (SampleProgressImage dbImage in dbItem.SampleProgressImage.ToArray()) { if (!dtoItem.ProgressImageDTOs.Select(o => o.SampleProgressImageID).Contains(dbImage.SampleProgressImageID)) { // delete files if (!string.IsNullOrEmpty(dbImage.FileUD)) { fwFactory.RemoveImageFile(dbImage.FileUD); } dbItem.SampleProgressImage.Remove(dbImage); } } foreach (DTO.BuildingProcess.ProgressImageDTO dtoImage in dtoItem.ProgressImageDTOs) { SampleProgressImage dbImage; if (dtoImage.SampleProgressImageID <= 0) { dbImage = new SampleProgressImage(); dbItem.SampleProgressImage.Add(dbImage); } else { dbImage = dbItem.SampleProgressImage.FirstOrDefault(o => o.SampleProgressImageID == dtoImage.SampleProgressImageID); } if (dbItem != null) { AutoMapper.Mapper.Map <DTO.BuildingProcess.ProgressImageDTO, SampleProgressImage>(dtoImage, dbImage); if (dtoImage.HasChanged && !string.IsNullOrEmpty(dtoImage.NewFile)) { dbImage.FileUD = fwFactory.CreateNoneImageFilePointer(TmpFile, dtoImage.NewFile, dbImage.FileUD); } } } }