public bool DeleteAppointment(int userId, int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (SCMAgendaMngEntities context = CreateContext()) { SCMAppointment dbItem = context.SCMAppointment.FirstOrDefault(o => o.SCMAppointmentID == id); if (dbItem == null) { throw new Exception("Event not found!"); } if (dbItem.MeetingLocationID.Value != 5 && dbItem.UserID.Value != userId) { throw new Exception("You can not delete this event, please contact the event owner!"); } // delete the event foreach (SCMAppointmentAttachedFile dbFile in dbItem.SCMAppointmentAttachedFile.ToArray()) { if (!string.IsNullOrEmpty(dbFile.FileUD)) { fwFactory.RemoveImageFile(dbFile.FileUD); } dbItem.SCMAppointmentAttachedFile.Remove(dbFile); context.SCMAppointmentAttachedFile.Remove(dbFile); } // delete the user foreach (SCMAppointmentUser dbUser in dbItem.SCMAppointmentUser.ToArray()) { dbItem.SCMAppointmentUser.Remove(dbUser); context.SCMAppointmentUser.Remove(dbUser); } context.SCMAppointment.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 override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.SCMAppointmentDTO dtoAppointment = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.SCMAppointmentDTO>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (SCMAgendaMngEntities context = CreateContext()) { SCMAppointment dbItem = null; if (id <= 0) { dbItem = new SCMAppointment(); if (dtoAppointment.MeetingLocationID == 5) // trip to VN { dbItem.UserID = dtoAppointment.UserID; } else { dbItem.UserID = userId; } context.SCMAppointment.Add(dbItem); } else { dbItem = context.SCMAppointment.FirstOrDefault(o => o.SCMAppointmentID == id); if (dbItem == null) { throw new Exception("Event not found!"); } if (dtoAppointment.MeetingLocationID == 5) // trip to VN { dbItem.UserID = dtoAppointment.UserID; } //if (dbItem.UserID.Value != userId) //{ // throw new Exception("You can not delete this event, please contact the event owner!"); //} } if (dtoAppointment.MeetingLocationID == 5) // trip to VN { dtoAppointment.StartTime = "00:00"; dtoAppointment.EndTime = "23:59"; } converter.DTO2DB(dtoAppointment, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", userId); context.SCMAppointmentUser.Local.Where(o => o.SCMAppointment == null).ToList().ForEach(o => context.SCMAppointmentUser.Remove(o)); // updated by, updated date, meeting minute. dbItem.MeetingMinute = dtoAppointment.MeetingMinute; dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; context.SaveChanges(); // generate agenda code if (id <= 0) { using (DbContextTransaction scope = context.Database.BeginTransaction()) { context.Database.ExecuteSqlCommand("SELECT * FROM SCMAppointment WITH (TABLOCKX, HOLDLOCK)"); try { dbItem.SCMAppointmentUD = dbItem.SCMAppointmentID.ToString("D10"); context.SaveChanges(); } catch (Exception ex) { throw ex; } finally { scope.Commit(); } } } else { 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(DTO.SCMAppointmentDTO dtoItem, ref SCMAppointment dbItem, string TmpFile, int userId) { if (!string.IsNullOrEmpty(dtoItem.StartDate) && !string.IsNullOrEmpty(dtoItem.StartTime)) { if (DateTime.TryParse(dtoItem.StartDate + " " + dtoItem.StartTime, nl, System.Globalization.DateTimeStyles.None, out tmpDate)) { dbItem.StartTime = tmpDate; } } if (!string.IsNullOrEmpty(dtoItem.EndDate) && !string.IsNullOrEmpty(dtoItem.EndTime)) { if (DateTime.TryParse(dtoItem.EndDate + " " + dtoItem.EndTime, nl, System.Globalization.DateTimeStyles.None, out tmpDate)) { dbItem.EndTime = tmpDate; } } if (!string.IsNullOrEmpty(dtoItem.RemindDate) && !string.IsNullOrEmpty(dtoItem.RemindTime)) { if (DateTime.TryParse(dtoItem.RemindDate + " " + dtoItem.RemindTime, nl, System.Globalization.DateTimeStyles.None, out tmpDate)) { dbItem.RemindTime = tmpDate; } } // attached files foreach (SCMAppointmentAttachedFile dbFile in dbItem.SCMAppointmentAttachedFile.ToArray()) { if (!dtoItem.SCMAppointmentAttachedFileDTOs.Select(o => o.SCMAppointmentAttachedFileID).Contains(dbFile.SCMAppointmentAttachedFileID)) { if (!string.IsNullOrEmpty(dbFile.FileUD)) { // remove image file fwFactory.RemoveImageFile(dbFile.FileUD); } dbItem.SCMAppointmentAttachedFile.Remove(dbFile); } } foreach (DTO.SCMAppointmentAttachedFileDTO dtoFile in dtoItem.SCMAppointmentAttachedFileDTOs) { SCMAppointmentAttachedFile dbFile; if (dtoFile.SCMAppointmentAttachedFileID <= 0) { dbFile = new SCMAppointmentAttachedFile(); dbItem.SCMAppointmentAttachedFile.Add(dbFile); } else { dbFile = dbItem.SCMAppointmentAttachedFile.FirstOrDefault(o => o.SCMAppointmentAttachedFileID == dtoFile.SCMAppointmentAttachedFileID); } if (dbFile != null) { // change or add images if (dtoFile.HasChanged) { dbFile.FileUD = fwFactory.CreateNoneImageFilePointer(TmpFile, dtoFile.NewFile, dtoFile.FileUD, dtoFile.FriendlyName); } AutoMapper.Mapper.Map <DTO.SCMAppointmentAttachedFileDTO, SCMAppointmentAttachedFile>(dtoFile, dbFile); // updated by, updated date. if (dtoFile.HasChanged) { dbFile.UpdatedBy = userId; dbFile.UpdatedDate = DateTime.Now; } } } if (dtoItem.SCMAppointmentUserDTOs != null) { foreach (var item in dbItem.SCMAppointmentUser.ToArray()) { if (!dtoItem.SCMAppointmentUserDTOs.Select(o => o.SCMAppointmentUserID).Contains(item.SCMAppointmentUserID)) { dbItem.SCMAppointmentUser.Remove(item); } } //map child row foreach (var item in dtoItem.SCMAppointmentUserDTOs) { SCMAppointmentUser dbSCMAppointmentUser; if (item.SCMAppointmentUserID <= 0) { dbSCMAppointmentUser = new SCMAppointmentUser(); dbItem.SCMAppointmentUser.Add(dbSCMAppointmentUser); } else { dbSCMAppointmentUser = dbItem.SCMAppointmentUser.FirstOrDefault(o => o.SCMAppointmentUserID == item.SCMAppointmentUserID); } if (dbSCMAppointmentUser != null) { AutoMapper.Mapper.Map <DTO.SCMAppointmentUserDTO, SCMAppointmentUser>(item, dbSCMAppointmentUser); } } } if (dtoItem.SCMInspectionDTOs != null) { // Inspection report foreach (SCMInspection dbInspection in dbItem.SCMInspection.ToArray()) { if (!dtoItem.SCMInspectionDTOs.Select(o => o.SCMInspectionID).Contains(dbInspection.SCMInspectionID)) { dbItem.SCMInspection.Remove(dbInspection); } } //map child row foreach (var item in dtoItem.SCMInspectionDTOs) { SCMInspection dbSCMInspection; if (item.SCMInspectionID <= 0) { dbSCMInspection = new SCMInspection(); dbItem.SCMInspection.Add(dbSCMInspection); } else { dbSCMInspection = dbItem.SCMInspection.FirstOrDefault(o => o.SCMInspectionID == item.SCMInspectionID); } if (dbSCMInspection != null) { AutoMapper.Mapper.Map <DTO.SCMInspectionDTO, SCMInspection>(item, dbSCMInspection); } } } AutoMapper.Mapper.Map <DTO.SCMAppointmentDTO, SCMAppointment>(dtoItem, dbItem); dbItem.UpdatedDate = DateTime.Now; }