Example #1
0
        /// <summary>
        /// Updating hostingUnit details
        /// </summary>
        /// <param name="unitKey"></param>
        /// <param name="accommodationKey"></param>
        /// <param name="hostKey"></param>
        /// <param name="detailsInUnitUpdate"></param>
        /// <param name="newValue">can be sum of orders, elements in unit, price, etc'...</param>
        public void HostingUnitUpdate(int unitKey, int accommodationKey, string hostKey, enums.UnitUpdate detailsInUnitUpdate,
                                      object newValue)
        {
            switch (detailsInUnitUpdate)
            {
            case enums.UnitUpdate.Price:
                if ((decimal)newValue <= 0)
                {
                    throw new Exception("the price must be more than zero");
                }

                _idalImp.HostingUnitUpdate(unitKey, accommodationKey, hostKey, enums.UnitUpdate.Price, newValue.Clone());
                break;

            case enums.UnitUpdate.ElementsUpdate:
                _idalImp.HostingUnitUpdate(unitKey, accommodationKey, hostKey, enums.UnitUpdate.ElementsUpdate, newValue.Clone());
                break;

            case enums.UnitUpdate.OrderCompleted:
                _idalImp.HostingUnitUpdate(unitKey, accommodationKey, hostKey, enums.UnitUpdate.OrderCompleted, newValue.Clone());
                break;

            case enums.UnitUpdate.OrderOffer:
                _idalImp.HostingUnitUpdate(unitKey, accommodationKey, hostKey, enums.UnitUpdate.OrderOffer, newValue.Clone());
                break;

            default:
                throw new Exception("This value cannot be updated");
            }
        }
        public void HostingUnitUpdate(int unitKey, int accommodationKey, string hostKey, enums.UnitUpdate detailsInUnitUpdate,
                                      object newValue)
        {
            if (_hosts.All(x => x.Id != hostKey))
            {
                throw new HostsException("Host doesn't exist");
            }

            if (_hosts.SelectMany(x => x.HostAccommodationsList).All(y => y.AccommodationKey != accommodationKey))
            {
                throw new AccommodationsException("Accommodation doesn't exist");
            }

            if (_hosts.SelectMany(x => x.HostAccommodationsList).SelectMany(y => y.ListOfAllUnits)
                .All(z => z.HostingUnitKey != unitKey))
            {
                throw new HostingUnitsException("host unit doesn't exist");
            }


            foreach (var z in from x in _hosts
                     from y in x.HostAccommodationsList
                     from z in y.ListOfAllUnits
                     where z.HostingUnitKey == unitKey && z.AccommodationKey == accommodationKey && z.HostId == hostKey
                     select z)
            {
                switch (detailsInUnitUpdate)
                {
                case enums.UnitUpdate.Price:
                    z.UnitPrice = (decimal)newValue.Clone();
                    Tools.SaveToXML(_hosts, XmlConfigurations.HostsPath);
                    return;

                case enums.UnitUpdate.ElementsUpdate:
                    z.UnitOptions = (FilterElements)newValue.Clone();
                    Tools.SaveToXML(_hosts, XmlConfigurations.HostsPath);
                    return;

                case enums.UnitUpdate.OrderCompleted:
                    z.SumCompletedOrders++;
                    Tools.SaveToXML(_hosts, XmlConfigurations.HostsPath);
                    return;

                case enums.UnitUpdate.OrderOffer:
                    Tools.SaveToXML(_hosts, XmlConfigurations.HostsPath);
                    return;

                default:
                    throw new Exception("This value cannot be updated");
                }
            }

            throw new HostsException("Host not found");
        }