public bool DeleteAppointment(int userId, int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (PurchasingCalendarMngEntities context = CreateContext()) { PurchasingCalendarAppointment dbItem = context.PurchasingCalendarAppointment.FirstOrDefault(o => o.PurchasingCalendarAppointmentID == 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 (PurchasingCalendarAppointmentAttachedFile dbFile in dbItem.PurchasingCalendarAppointmentAttachedFile.ToArray()) { if (!string.IsNullOrEmpty(dbFile.FileUD)) { fwFactory.RemoveImageFile(dbFile.FileUD); } dbItem.PurchasingCalendarAppointmentAttachedFile.Remove(dbFile); context.PurchasingCalendarAppointmentAttachedFile.Remove(dbFile); } context.PurchasingCalendarAppointment.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(PurchasingCalendarAppointmentDTO dtoItem, ref PurchasingCalendarAppointment 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 (PurchasingCalendarAppointmentAttachedFile dbFile in dbItem.PurchasingCalendarAppointmentAttachedFile.ToArray()) { if (!dtoItem.PurchasingCalendarAppointmentAttachedFileDTOs.Select(o => o.PurchasingCalendarAppointmentAttachedFileID).Contains(dbFile.PurchasingCalendarAppointmentAttachedFileID)) { if (!string.IsNullOrEmpty(dbFile.FileUD)) { // remove image file fwFactory.RemoveImageFile(dbFile.FileUD); } dbItem.PurchasingCalendarAppointmentAttachedFile.Remove(dbFile); } } foreach (PurchasingCalendarAppointmentAttachedFileDTO dtoFile in dtoItem.PurchasingCalendarAppointmentAttachedFileDTOs) { PurchasingCalendarAppointmentAttachedFile dbFile; if (dtoFile.PurchasingCalendarAppointmentAttachedFileID <= 0) { dbFile = new PurchasingCalendarAppointmentAttachedFile(); dbItem.PurchasingCalendarAppointmentAttachedFile.Add(dbFile); } else { dbFile = dbItem.PurchasingCalendarAppointmentAttachedFile.FirstOrDefault(o => o.PurchasingCalendarAppointmentAttachedFileID == dtoFile.PurchasingCalendarAppointmentAttachedFileID); } if (dbFile != null) { // change or add images if (dtoFile.HasChanged) { dbFile.FileUD = fwFactory.CreateNoneImageFilePointer(TmpFile, dtoFile.NewFile, dtoFile.FileUD, dtoFile.FriendlyName); } Mapper.Map <PurchasingCalendarAppointmentAttachedFileDTO, PurchasingCalendarAppointmentAttachedFile>(dtoFile, dbFile); // updated by, updated date. if (dtoFile.HasChanged) { dbFile.UpdatedBy = userId; dbFile.UpdatedDate = DateTime.Now; } } } if (dtoItem.PurchasingCalendarUserDTOs != null) { foreach (var item in dbItem.PurchasingCalendarUser.ToArray()) { if (!dtoItem.PurchasingCalendarUserDTOs.Select(o => o.PurchasingCalendarUserID).Contains(item.PurchasingCalendarUserID)) { dbItem.PurchasingCalendarUser.Remove(item); } } //map child row foreach (var item in dtoItem.PurchasingCalendarUserDTOs) { PurchasingCalendarUser dbAgendaMngUser; if (item.PurchasingCalendarUserID <= 0) { dbAgendaMngUser = new PurchasingCalendarUser(); dbItem.PurchasingCalendarUser.Add(dbAgendaMngUser); } else { dbAgendaMngUser = dbItem.PurchasingCalendarUser.FirstOrDefault(o => o.PurchasingCalendarUserID == item.PurchasingCalendarUserID); } if (dbAgendaMngUser != null) { Mapper.Map <PurchasingCalendarUserDTO, PurchasingCalendarUser>(item, dbAgendaMngUser); } } } Mapper.Map <PurchasingCalendarAppointmentDTO, PurchasingCalendarAppointment>(dtoItem, dbItem); dbItem.UpdatedDate = DateTime.Now; }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { PurchasingCalendarAppointmentDTO dtoAppointment = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <PurchasingCalendarAppointmentDTO>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (PurchasingCalendarMngEntities context = CreateContext()) { PurchasingCalendarAppointment dbItem = null; if (id <= 0) { dbItem = new PurchasingCalendarAppointment(); if (dtoAppointment.MeetingLocationID == 5) // trip to VN { dbItem.UserID = dtoAppointment.UserID; } else { dbItem.UserID = userId; } context.PurchasingCalendarAppointment.Add(dbItem); } else { dbItem = context.PurchasingCalendarAppointment.FirstOrDefault(o => o.PurchasingCalendarAppointmentID == 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.PurchasingCalendarUser.Local.Where(o => o.PurchasingCalendarAppointment == null).ToList().ForEach(o => context.PurchasingCalendarUser.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 Appointment WITH (TABLOCKX, HOLDLOCK)"); try { dbItem.PurchasingCalendarAppointmentUD = dbItem.PurchasingCalendarAppointmentID.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); } }