Exemplo n.º 1
0
        public void DTO2DB_ClientOffer(DTO.ClientOffer dtoItem, ref ClientOffer dbItem)
        {
            if (dtoItem.ClientOfferCostDetailDTOs != null)
            {
                foreach (var item in dbItem.ClientOfferCostDetail.ToArray())
                {
                    if (!dtoItem.ClientOfferCostDetailDTOs.Select(s => s.ClientOfferCostDetailID).Contains(item.ClientOfferCostDetailID))
                    {
                        dbItem.ClientOfferCostDetail.Remove(item);
                    }
                }
                foreach (var item in dtoItem.ClientOfferCostDetailDTOs)
                {
                    ClientOfferCostDetail dbDetail = new ClientOfferCostDetail();
                    if (item.ClientOfferCostDetailID < 0)
                    {
                        if (item.ClientCostItemID.HasValue)
                        {
                            dbItem.ClientOfferCostDetail.Add(dbDetail);
                        }
                    }
                    else
                    {
                        dbDetail = dbItem.ClientOfferCostDetail.Where(o => o.ClientOfferCostDetailID == item.ClientOfferCostDetailID).FirstOrDefault();
                    }
                    if (dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.ClientOfferCostDetailDTO, ClientOfferCostDetail>(item, dbDetail);
                    }
                }
            }

            if (dtoItem.ClientOfferConditionDetailDTOs != null)
            {
                foreach (var item in dbItem.ClientOfferConditionDetail.ToArray())
                {
                    if (!dtoItem.ClientOfferConditionDetailDTOs.Select(s => s.ClientOfferConditionDetailID).Contains(item.ClientOfferConditionDetailID))
                    {
                        dbItem.ClientOfferConditionDetail.Remove(item);
                    }
                }
                foreach (var item in dtoItem.ClientOfferConditionDetailDTOs)
                {
                    ClientOfferConditionDetail dbDetail = new ClientOfferConditionDetail();
                    if (item.ClientOfferConditionDetailID < 0)
                    {
                        if (item.ClientConditionItemID.HasValue)
                        {
                            dbItem.ClientOfferConditionDetail.Add(dbDetail);
                        }
                    }
                    else
                    {
                        dbDetail = dbItem.ClientOfferConditionDetail.Where(o => o.ClientOfferConditionDetailID == item.ClientOfferConditionDetailID).FirstOrDefault();
                    }
                    if (dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.ClientOfferConditionDetailDTO, ClientOfferConditionDetail>(item, dbDetail);
                    }
                }
            }

            if (dtoItem.ClientOfferAdditionalDetailDTOs != null)
            {
                foreach (var item in dbItem.ClientOfferAdditionalDetail.ToArray())
                {
                    if (!dtoItem.ClientOfferAdditionalDetailDTOs.Select(s => s.ClientOfferAdditionalDetailID).Contains(item.ClientOfferAdditionalDetailID))
                    {
                        dbItem.ClientOfferAdditionalDetail.Remove(item);
                    }
                }
                foreach (var item in dtoItem.ClientOfferAdditionalDetailDTOs)
                {
                    ClientOfferAdditionalDetail dbDetail = new ClientOfferAdditionalDetail();
                    if (item.ClientOfferAdditionalDetailID < 0)
                    {
                        dbItem.ClientOfferAdditionalDetail.Add(dbDetail);
                    }
                    else
                    {
                        dbDetail = dbItem.ClientOfferAdditionalDetail.Where(o => o.ClientOfferAdditionalDetailID == item.ClientOfferAdditionalDetailID).FirstOrDefault();
                    }
                    if (dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.ClientOfferAdditionalDetailDTO, ClientOfferAdditionalDetail>(item, dbDetail);
                    }
                }
            }

            AutoMapper.Mapper.Map <DTO.ClientOffer, ClientOffer>(dtoItem, dbItem);
            dbItem.ValidFrom   = dtoItem.ValidFrom.ConvertStringToDateTime();
            dbItem.ValidTo     = dtoItem.ValidTo.ConvertStringToDateTime();
            dbItem.UpdatedDate = DateTime.Now;
        }
Exemplo n.º 2
0
 public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
 {
     notification = new Library.DTO.Notification()
     {
         Type = Library.DTO.NotificationType.Success
     };
     DTO.ClientOffer dtoClientOffer = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.ClientOffer>();
     try
     {
         using (ClientOfferMngEntities context = CreateContext())
         {
             ClientOffer dbItem = null;
             if (id > 0)
             {
                 dbItem = context.ClientOffer.Where(o => o.ClientOfferID == id).FirstOrDefault();
             }
             else
             {
                 dbItem = new ClientOffer();
                 context.ClientOffer.Add(dbItem);
             }
             if (dbItem == null)
             {
                 notification.Message = "data not found!";
                 return(false);
             }
             else
             {
                 Module.Framework.DAL.DataFactory fwFactory = new Module.Framework.DAL.DataFactory();
                 string tempFolder = FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\";
                 if (dtoClientOffer.File_HasChange.HasValue && dtoClientOffer.File_HasChange.Value)
                 {
                     dtoClientOffer.FileUD = fwFactory.CreateFilePointer(tempFolder, dtoClientOffer.File_NewFile, dtoClientOffer.FileUD, dtoClientOffer.FriendlyName);
                 }
                 //convert dto to db
                 converter.DTO2DB_ClientOffer(dtoClientOffer, ref dbItem);
                 dbItem.UpdatedBy = userId;
                 //remove orphan
                 context.ClientOfferCostDetail.Local.Where(o => o.ClientOffer == null).ToList().ForEach(o => context.ClientOfferCostDetail.Remove(o));
                 context.ClientOfferConditionDetail.Local.Where(o => o.ClientOffer == null).ToList().ForEach(o => context.ClientOfferConditionDetail.Remove(o));
                 context.ClientOfferAdditionalDetail.Local.Where(o => o.ClientOffer == null).ToList().ForEach(o => context.ClientOfferAdditionalDetail.Remove(o));
                 //save data
                 context.SaveChanges();
                 //get return data
                 dtoItem = GetData(userId, dbItem.ClientOfferID, 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);
     }
 }