Exemplo n.º 1
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.TransportOfferDTO dtoTransportOffer = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.TransportOfferDTO>();
     try
     {
         using (TransportOfferEntities context = CreateContext())
         {
             TransportOffer dbItem = null;
             if (id > 0)
             {
                 dbItem = context.TransportOffer.Where(o => o.TransportOfferID == id).FirstOrDefault();
             }
             else
             {
                 dbItem = new TransportOffer();
                 context.TransportOffer.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 (dtoTransportOffer.File_HasChange.HasValue && dtoTransportOffer.File_HasChange.Value)
                 {
                     dtoTransportOffer.FileUD = fwFactory.CreateFilePointer(tempFolder, dtoTransportOffer.File_NewFile, dtoTransportOffer.FileUD, dtoTransportOffer.FriendlyName);
                 }
                 //convert dto to db
                 converter.DTO2DB_TransportOffer(dtoTransportOffer, ref dbItem);
                 dbItem.UpdatedBy = userId;
                 //remove orphan
                 context.TransportOfferCostDetail.Local.Where(o => o.TransportOffer == null).ToList().ForEach(o => context.TransportOfferCostDetail.Remove(o));
                 context.TransportOfferConditionDetail.Local.Where(o => o.TransportOffer == null).ToList().ForEach(o => context.TransportOfferConditionDetail.Remove(o));
                 //save data
                 context.SaveChanges();
                 //get return data
                 dtoItem = GetData(userId, dbItem.TransportOfferID, 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);
     }
 }
Exemplo n.º 2
0
 public void DTO2DB_TransportOffer(DTO.TransportOfferDTO dtoItem, ref TransportOffer dbItem)
 {
     if (dtoItem.TransportOfferConditionDetailDTOs != null)
     {
         foreach (var item in dbItem.TransportOfferConditionDetail.ToArray())
         {
             if (!dtoItem.TransportOfferConditionDetailDTOs.Select(s => s.TransportOfferConditionDetailID).Contains(item.TransportOfferConditionDetailID))
             {
                 dbItem.TransportOfferConditionDetail.Remove(item);
             }
         }
         foreach (var item in dtoItem.TransportOfferConditionDetailDTOs)
         {
             if (item.POLID.HasValue && item.POLID.Value == -1)
             {
                 item.POLID = null;
             }
             if (item.PODID.HasValue && item.PODID == -1)
             {
                 item.PODID = null;
             }
             TransportOfferConditionDetail dbDetail = new TransportOfferConditionDetail();
             if (item.TransportOfferConditionDetailID < 0)
             {
                 if (item.TransportConditionItemID.HasValue)
                 {
                     dbItem.TransportOfferConditionDetail.Add(dbDetail);
                 }
             }
             else
             {
                 dbDetail = dbItem.TransportOfferConditionDetail.Where(o => o.TransportOfferConditionDetailID == item.TransportOfferConditionDetailID).FirstOrDefault();
             }
             if (dbDetail != null)
             {
                 AutoMapper.Mapper.Map <DTO.TransportOfferConditionDetailDTO, TransportOfferConditionDetail>(item, dbDetail);
             }
         }
     }
     if (dtoItem.TransportOfferCostDetailDTOs != null)
     {
         foreach (var item in dbItem.TransportOfferCostDetail.ToArray())
         {
             if (!dtoItem.TransportOfferCostDetailDTOs.Select(s => s.TransportOfferCostDetailID).Contains(item.TransportOfferCostDetailID))
             {
                 dbItem.TransportOfferCostDetail.Remove(item);
             }
         }
         foreach (var item in dtoItem.TransportOfferCostDetailDTOs)
         {
             if (item.POLID.HasValue && item.POLID.Value == -1)
             {
                 item.POLID = null;
             }
             if (item.PODID.HasValue && item.PODID == -1)
             {
                 item.PODID = null;
             }
             TransportOfferCostDetail dbDetail = new TransportOfferCostDetail();
             if (item.TransportOfferCostDetailID < 0)
             {
                 if (item.TransportCostItemID.HasValue)
                 {
                     dbItem.TransportOfferCostDetail.Add(dbDetail);
                 }
             }
             else
             {
                 dbDetail = dbItem.TransportOfferCostDetail.Where(o => o.TransportOfferCostDetailID == item.TransportOfferCostDetailID).FirstOrDefault();
             }
             if (dbDetail != null)
             {
                 AutoMapper.Mapper.Map <DTO.TransportOfferCostDetailDTO, TransportOfferCostDetail>(item, dbDetail);
             }
         }
     }
     AutoMapper.Mapper.Map <DTO.TransportOfferDTO, TransportOffer>(dtoItem, dbItem);
     dbItem.ValidFrom   = dtoItem.ValidFrom.ConvertStringToDateTime();
     dbItem.ValidTo     = dtoItem.ValidTo.ConvertStringToDateTime();
     dbItem.UpdatedDate = DateTime.Now;
 }