public object UpdateEvent(int userID, int id, System.Collections.Hashtable filters, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification(); notification.Type = Library.DTO.NotificationType.Success; DTO.BifaEvent dtoItem = ((Newtonsoft.Json.Linq.JObject)filters["view"]).ToObject <DTO.BifaEvent>(); try { using (var context = CreateContext()) { BifaEvent dbItem; if (id == 0) { dbItem = new BifaEvent(); context.BifaEvent.Add(dbItem); } else { dbItem = context.BifaEvent.FirstOrDefault(o => o.BifaEventID == id); } if (dbItem == null) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = "Can not found data"; return(null); } converter.DTO2DB_BifaEvent(userID, dtoItem, ref dbItem); context.BifaEventFile.Local.Where(o => o.BifaEvent == null).ToList().ForEach(o => context.BifaEventFile.Remove(o)); context.BifaEventParticipant.Local.Where(o => o.BifaEvent == null).ToList().ForEach(o => context.BifaEventParticipant.Remove(o)); context.SaveChanges(); dtoItem = AutoMapper.Mapper.Map <BifaCompanyMng_BifaEvent_View, DTO.BifaEvent>(context.BifaCompanyMng_BifaEvent_View.FirstOrDefault(o => o.BifaEventID == dbItem.BifaEventID)); } } catch (System.Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = Helper.GetInnerException(ex).Message; } return(dtoItem); }
public void DTO2DB_BifaEvent(int userID, DTO.BifaEvent dtoItem, ref BifaEvent dbItem) { AutoMapper.Mapper.Map <DTO.BifaEvent, BifaEvent>(dtoItem, dbItem); dbItem.BifaCompanyID = dtoItem.BifaCompanyID; dbItem.BifaEventUD = dtoItem.BifaEventUD; dbItem.StartDate = dtoItem.StartDate.ConvertStringToDateTime(); dbItem.EndDate = dtoItem.EndDate.ConvertStringToDateTime(); dbItem.UpdatedBy = userID; dbItem.UpdatedDate = System.DateTime.Now; if (dtoItem.BifaEventParticipants != null) { foreach (var item in dbItem.BifaEventParticipant.ToArray()) { if (!dtoItem.BifaEventParticipants.Select(s => s.BifaEventParticipantID).Contains(item.BifaEventParticipantID)) { dbItem.BifaEventParticipant.Remove(item); } } foreach (var item in dtoItem.BifaEventParticipants.ToArray()) { BifaEventParticipant dbItemDetail; if (item.BifaEventParticipantID < 0) { dbItemDetail = new BifaEventParticipant(); dbItem.BifaEventParticipant.Add(dbItemDetail); } else { dbItemDetail = dbItem.BifaEventParticipant.FirstOrDefault(s => s.BifaEventParticipantID == item.BifaEventParticipantID); } if (dbItemDetail != null) { AutoMapper.Mapper.Map <DTO.BifaEventParticipant, BifaEventParticipant>(item, dbItemDetail); dbItemDetail.UpdatedBy = userID; dbItemDetail.UpdatedDate = System.DateTime.Now; } } } if (dtoItem.BifaEventFiles != null) { Framework.DAL.DataFactory fwFactory = new Framework.DAL.DataFactory(); foreach (var item in dbItem.BifaEventFile.ToList()) { if (!dtoItem.BifaEventFiles.Select(s => s.BifaEventFileID).Contains(item.BifaEventFileID)) { dbItem.BifaEventFile.Remove(item); } } foreach (var item in dtoItem.BifaEventFiles.ToList()) { BifaEventFile dbItemDetail; if (item.BifaEventFileID < 0) { dbItemDetail = new BifaEventFile(); dbItem.BifaEventFile.Add(dbItemDetail); } else { dbItemDetail = dbItem.BifaEventFile.FirstOrDefault(s => s.BifaEventFileID == item.BifaEventFileID); } if (dbItemDetail != null) { AutoMapper.Mapper.Map <DTO.BifaEventFile, BifaEventFile>(item, dbItemDetail); if (item.HasChange) { if (string.IsNullOrEmpty(item.NewFile)) { fwFactory.RemoveImageFile(item.FileUD); } else { dbItemDetail.FileUD = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userID.ToString() + @"\", item.NewFile, item.FileUD); } } } } } }