public void DTO2DB(DTO.AppointmentDTO dtoItem, ref Appointment dbItem) { // map fields AutoMapper.Mapper.Map <DTO.AppointmentDTO, Appointment>(dtoItem, dbItem); if (!string.IsNullOrEmpty(dtoItem.StartTime_Date) && !string.IsNullOrEmpty(dtoItem.StartTime_Time)) { if (DateTime.TryParse(dtoItem.StartTime_Date + " " + dtoItem.StartTime_Time, nl, System.Globalization.DateTimeStyles.None, out tmpDate)) { dbItem.StartTime = tmpDate; } } if (!string.IsNullOrEmpty(dtoItem.EndTime_Date) && !string.IsNullOrEmpty(dtoItem.EndTime_Time)) { if (DateTime.TryParse(dtoItem.EndTime_Date + " " + dtoItem.EndTime_Time, nl, System.Globalization.DateTimeStyles.None, out tmpDate)) { dbItem.EndTime = tmpDate; } } if (!string.IsNullOrEmpty(dtoItem.RemindTime_Date) && !string.IsNullOrEmpty(dtoItem.RemindTime_Time)) { if (DateTime.TryParse(dtoItem.RemindTime_Date + " " + dtoItem.RemindTime_Time, nl, System.Globalization.DateTimeStyles.None, out tmpDate)) { dbItem.RemindTime = tmpDate; } } }
public void DTO2DB(DTO.AppointmentDTO dtoItem, ref Appointment 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 (AppointmentAttachedFile dbFile in dbItem.AppointmentAttachedFile.ToArray()) { if (!dtoItem.AppointmentAttachedFileDTOs.Select(o => o.AppointmentAttachedFileID).Contains(dbFile.AppointmentAttachedFileID)) { if (!string.IsNullOrEmpty(dbFile.FileUD)) { // remove image file fwFactory.RemoveImageFile(dbFile.FileUD); } dbItem.AppointmentAttachedFile.Remove(dbFile); } } foreach (DTO.AppointmentAttachedFileDTO dtoFile in dtoItem.AppointmentAttachedFileDTOs) { AppointmentAttachedFile dbFile; if (dtoFile.AppointmentAttachedFileID <= 0) { dbFile = new AppointmentAttachedFile(); dbItem.AppointmentAttachedFile.Add(dbFile); } else { dbFile = dbItem.AppointmentAttachedFile.FirstOrDefault(o => o.AppointmentAttachedFileID == dtoFile.AppointmentAttachedFileID); } 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.AppointmentAttachedFileDTO, AppointmentAttachedFile>(dtoFile, dbFile); // updated by, updated date. if (dtoFile.HasChanged) { dbFile.UpdatedBy = userId; dbFile.UpdatedDate = DateTime.Now; } } } if (dtoItem.agendaMngUsers != null) { foreach (var item in dbItem.AgendaMngUser.ToArray()) { if (!dtoItem.agendaMngUsers.Select(o => o.ManagerUserID).Contains(item.ManagerUserID)) { dbItem.AgendaMngUser.Remove(item); } } //map child row foreach (var item in dtoItem.agendaMngUsers) { AgendaMngUser dbAgendaMngUser; if (item.ManagerUserID <= 0) { dbAgendaMngUser = new AgendaMngUser(); dbItem.AgendaMngUser.Add(dbAgendaMngUser); } else { dbAgendaMngUser = dbItem.AgendaMngUser.FirstOrDefault(o => o.ManagerUserID == item.ManagerUserID); } if (dbAgendaMngUser != null) { AutoMapper.Mapper.Map <DTO.AgendaMngUser, AgendaMngUser>(item, dbAgendaMngUser); } } } AutoMapper.Mapper.Map <DTO.AppointmentDTO, Appointment>(dtoItem, dbItem); dbItem.UpdatedDate = DateTime.Now; }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.AppointmentDTO dtoAppointment = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.AppointmentDTO>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (AgendaMngEntities context = CreateContext()) { Appointment dbItem = null; if (id <= 0) { dbItem = new Appointment(); if (dtoAppointment.MeetingLocationID == 5) // trip to VN { dbItem.UserID = dtoAppointment.UserID; } else { dbItem.UserID = userId; } context.Appointment.Add(dbItem); } else { dbItem = context.Appointment.FirstOrDefault(o => o.AppointmentID == 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.AgendaMngUser.Local.Where(o => o.Appointment == null).ToList().ForEach(o => context.AgendaMngUser.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.AppointmentUD = dbItem.AppointmentID.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 override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.AppointmentDTO dtoAppointment = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.AppointmentDTO>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (AppointmentMngEntities context = CreateContext()) { Appointment dbItem = null; if (id == 0) { dbItem = new Appointment(); context.Appointment.Add(dbItem); } else { dbItem = context.Appointment.FirstOrDefault(o => o.AppointmentID == id); } if (dbItem == null) { throw new Exception("Appointment not found!"); } using (DbContextTransaction scope = context.Database.BeginTransaction()) { context.Database.ExecuteSqlCommand("SELECT * FROM Appointment WITH (TABLOCKX, HOLDLOCK)"); try { converter.DTO2DB(dtoAppointment, ref dbItem); dbItem.UserID = userId; context.SaveChanges(); if (id == 0) { dbItem.AppointmentUD = Library.Common.Helper.formatIndex(dbItem.AppointmentID.ToString(), 10, "0"); context.SaveChanges(); } } catch (Exception ex) { throw ex; } finally { scope.Commit(); } } dtoItem = GetData(dbItem.AppointmentID, out notification).Data; return(true); } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } }