Esempio n. 1
0
        /// <summary>
        /// 5.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool UpdateRealty(int id, string newName, string newDescription, int newRegisterNumber, out string message)
        {
            Realty.EqualsOptions = EqualsOptions.ById;
            var helpRealty = new Realty(id);
            var helpById   = new RealtyById(helpRealty);

            var newRealty = new Realty(newRegisterNumber, newName, newDescription, newRegisterNumber)
            {
                Id = id
            };

            if (DhRealtyById.TryFind(helpById, out var realtyById))
            {
                var index = realtyById.AddressIndex;
                if (RafRealties.BeginUpdate(helpRealty, index, out var oldRealty))
                {
                    switch (DecideRealtyUpdate(oldRealty, newRealty))
                    {
                    case RealtyUpdateChange.None:
                    {
                        //nic sa nemusi diat
                        message = "No changes needed";
                        return(false);
                    }

                    case RealtyUpdateChange.JustDescription:
                    {
                        //v pamati je ulozeny blok v ktorom nie je povodna nehnutelnost, dam novu na index a v subore sa to prepise
                        RafRealties.FinishUpdate(newRealty, index);
                        message = $"Just Description changed from {oldRealty.Description} to {newRealty.Description}";
                        return(true);
                    }

                    case RealtyUpdateChange.Everything:
                    {
                        var helpByName = new RealtyByCadAndRegNumber(oldRealty);
                        if (DhRealtyByNameAndRegNumber.TryRemove(helpByName, out var removedRealtyByName))
                        {
                            var newRealtyByName = new RealtyByCadAndRegNumber(newRealty);
                            newRealtyByName.AddressIndex = index;
                            if (DhRealtyByNameAndRegNumber.Add(newRealtyByName))
                            {
                                RafRealties.FinishUpdate(newRealty, index);
                                message =
                                    $"Name and Register number updated from {oldRealty.UniqueName} and {oldRealty.RegisterNumber} to {newRealty.UniqueName} and {newRealty.RegisterNumber}";
                                return(true);
                            }
                            else
                            {
                                DhRealtyByNameAndRegNumber.Add(removedRealtyByName);
                                message = $"Could not update realty, no changes, realty with name and reg number {newRealty.UniqueName}, {newRealty.RegisterNumber} already exists";
                                return(false);
                            }
                        }
                        break;
                    }
                    }
                }
            }
            message = "Could not update realty";

            return(false);
        }