Пример #1
0
        public void DTO2DB_PriceListClientCharge(DTO.PriceListClientChargeDto dtoItem, PriceListClientCharge dbItem)
        {
            if (dtoItem.PriceListClientChargeDetail != null)
            {
                foreach (var item in dbItem.PriceListClientChargeDetail.ToList())
                {
                    if (!dtoItem.PriceListClientChargeDetail.Select(s => s.PriceListClientChargeDetailID).Contains(item.PriceListClientChargeDetailID))
                    {
                        dbItem.PriceListClientChargeDetail.Remove(item);
                    }
                }

                foreach (var dto in dtoItem.PriceListClientChargeDetail.ToList())
                {
                    PriceListClientChargeDetail item = null;

                    if (dto.PriceListClientChargeDetailID < 0)
                    {
                        item = new PriceListClientChargeDetail();

                        // Add item in PriceListClientChargeDetail
                        dbItem.PriceListClientChargeDetail.Add(item);
                    }
                    else
                    {
                        item = dbItem.PriceListClientChargeDetail.FirstOrDefault(o => o.PriceListClientChargeDetailID == dto.PriceListClientChargeDetailID);
                    }

                    if (item != null)
                    {
                        AutoMapper.Mapper.Map <DTO.PriceListClientChargeDetailDto, PriceListClientChargeDetail>(dto, item);
                    }
                }
            }

            // Convert string to date
            dbItem.StartDate = dtoItem.StartDate.ConvertStringToDateTime();
            dbItem.EndDate   = dtoItem.EndDate.ConvertStringToDateTime();

            AutoMapper.Mapper.Map <DTO.PriceListClientChargeDto, PriceListClientCharge>(dtoItem, dbItem);
        }
Пример #2
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification)
        {
            DTO.PriceListClientChargeDto dtoConvert = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.PriceListClientChargeDto>();

            // Define type of notification, convert object
            notification = new Notification {
                Type = NotificationType.Success
            };

            try
            {
                using (var context = CreateContext())
                {
                    PriceListClientCharge dbItem;

                    if (id > 0)
                    {
                        dbItem = context.PriceListClientCharge.FirstOrDefault(o => o.PriceListClientChargeID == id);
                    }
                    else
                    {
                        dbItem = new PriceListClientCharge();

                        context.PriceListClientCharge.Add(dbItem);

                        dbItem.CreatedBy   = userId;
                        dbItem.CreatedDate = DateTime.Now;
                    }

                    if (dbItem == null)
                    {
                        notification.Type    = NotificationType.Error;
                        notification.Message = string.Format("Price List Client Charge [id = {0}] not found!", id);

                        return(false);
                    }

                    // Call method convert object to database
                    converter.DTO2DB_PriceListClientCharge(dtoConvert, dbItem);

                    dbItem.UpdatedBy   = userId;
                    dbItem.UpdatedDate = DateTime.Now;

                    // remove orphan detail
                    context.PriceListClientChargeDetail.Local.Where(o => o.PriceListClientCharge == null).ToList()
                    .ForEach(o => context.PriceListClientChargeDetail.Remove(o));

                    context.SaveChanges();

                    // Load again data
                    dtoItem = GetData(dbItem.PriceListClientChargeID, out notification);

                    return(true);
                }
            }
            catch (Exception ex)
            {
                notification.Type    = NotificationType.Error;
                notification.Message = ex.Message;

                return(false);
            }
        }