public bool Update(int userId, int id, ref object dtoItem, out Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; DTO.ClientLPDTO dtoClientLP = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.ClientLPDTO>(); try { ////get companyID //Module.Framework.DAL.DataFactory fw_factory = new Framework.DAL.DataFactory(); //int? companyID = fw_factory.GetCompanyID(userId); using (ClientLPMngEntities context = CreateContext()) { ClientLP dbItem = null; dbItem = context.ClientLP.Where(o => o.ClientLPID == id).FirstOrDefault(); if (dbItem == null) { notification.Message = "data not found!"; return(false); } else { // //convert dto to db converter.DTO2DB_ClientLP(dtoClientLP, ref dbItem); //dbItem.CompanyID = companyID; dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; //remove orphan context.ClientLPNotification.Local.Where(o => o.ClientLP == null).ToList().ForEach(o => context.ClientLPNotification.Remove(o)); //save data context.SaveChanges(); //get return data dtoItem = GetEditData(userId, dbItem.ClientLPID, out notification).Data; return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; notification.DetailMessage.Add(ex.Message); if (ex.GetBaseException() != null) { notification.DetailMessage.Add(ex.GetBaseException().Message); } return(false); } }
public void DTO2DB_ClientLP(DTO.ClientLPDTO dtoItem, ref ClientLP dbItem) { if (dtoItem.ClientLPNotificationDTOs != null) { foreach (ClientLPNotification item in dbItem.ClientLPNotification.ToList()) { if (!dtoItem.ClientLPNotificationDTOs.Select(s => s.ClientLPNotificationID).Contains(item.ClientLPNotificationID)) { dbItem.ClientLPNotification.Remove(item); } } foreach (DTO.ClientLPNotificationDTO dto in dtoItem.ClientLPNotificationDTOs) { ClientLPNotification item; if (dto.ClientLPNotificationID < 0) { item = new ClientLPNotification(); dbItem.ClientLPNotification.Add(item); } else { item = dbItem.ClientLPNotification.FirstOrDefault(s => s.ClientLPNotificationID == dto.ClientLPNotificationID); } if (item != null) { AutoMapper.Mapper.Map <DTO.ClientLPNotificationDTO, ClientLPNotification>(dto, item); } } } AutoMapper.Mapper.Map <DTO.ClientLPDTO, ClientLP>(dtoItem, dbItem); dbItem.UpdatedDate = dtoItem.UpdatedDate.ConvertStringToDateTime(); }