コード例 #1
0
        public async void AddAsync(Nivel nivel)
        {
            IEdificioRepository edificioRepository = new EdificioRepository();

            var filterEdificio = Builders <Edificio> .Filter.Eq(x => x.Id, nivel.EdificioId);

            var resultEdificio = edificioRepository.GetByIdAsync(nivel.EdificioId);

            if (resultEdificio != null)
            {
                await Niveis.InsertOneAsync(nivel);

                if (resultEdificio.ListNivel == null)
                {
                    resultEdificio.InstanciarListNivel();
                }

                var filterNivel = Builders <Nivel> .Filter.Eq(x => x.EdificioId, resultEdificio.Id);

                var resultNivel = await Niveis.FindAsync(filterNivel);

                while (await resultNivel.MoveNextAsync())
                {
                    var batch = resultNivel.Current;
                    foreach (var document in batch)
                    {
                        resultEdificio.AdicionarNivel(document);
                    }
                }

                var updateEdificio = Builders <Edificio> .Update
                                     .Set(x => x.ListNivel, resultEdificio.ListNivel);

                await edificioRepository.UpdateAsync(filterEdificio, updateEdificio);
            }
        }