Example #1
0
        public async Task UpdateAnimalWithNeeds_Success()
        {
            var animal = new AnimalDto()
            {
                Id = 1
            };
            var animalNeeds = new HashSet <AnimalNeeds>()
            {
                new AnimalNeeds()
                {
                    Animal = new Animal()
                    {
                        Id = 1
                    },
                    AnimalId = 1,
                    NeedsId  = 1,
                    Needs    = new Needs()
                    {
                        Id = 1
                    }
                }
            };
            var model = new Animal()
            {
                Id = 1
            };
            var func = new System.Func <AnimalNeeds, long>(x => x.NeedsId);

            _animalNeedsRepositoryMock.Setup(x => x.TryUpdateManyToMany(animalNeeds,
                                                                        animalNeeds, func));
            _animalNeedsRepositoryMock.Setup(x => x.SaveAsync());

            await _service.UpdateAnimalWithNeeds(animal, model);

            _animalNeedsRepositoryMock.Verify(x => x.TryUpdateManyToMany(It.IsAny <IEnumerable <AnimalNeeds> >(),
                                                                         It.IsAny <IEnumerable <AnimalNeeds> >(),
                                                                         It.IsAny <System.Func <AnimalNeeds, long> >()),
                                              Times.Once());
            _animalNeedsRepositoryMock.Verify(x => x.SaveAsync(), Times.Once());
        }
Example #2
0
        public async Task <AnimalDto> UpdateAnimal(AnimalDto animal)
        {
            var model = _repository.Entities
                        .Include(animal => animal.AnimalNeeds)
                        .ThenInclude(need => need.Needs)
                        .AsNoTracking()
                        .Include(animal => animal.AnimalVaccinations)
                        .ThenInclude(vac => vac.Vaccination)
                        .AsNoTracking()
                        .Include(animal => animal.AnimalProcessings)
                        .ThenInclude(proc => proc.Processing)
                        .AsNoTracking()
                        .Include(animal => animal.AnimalKeepings)
                        .ThenInclude(keep => keep.Keeping)
                        .AsNoTracking()
                        .Include(animal => animal.AnimalAttitudes)
                        .ThenInclude(att => att.AttitudeTo)
                        .AsNoTracking()
                        .Include(animal => animal.AnimalDefects)
                        .ThenInclude(defect => defect.Defect)
                        .AsNoTracking()
                        .Include(animal => animal.Description)
                        .AsNoTracking()
                        //.Include(animal => animal.Breed)
                        //.AsNoTracking()
                        .Include(animal => animal.Images)
                        .FirstOrDefault(x => x.Id == animal.Id);

            var isLocationNew = animal.AddressId != model.AddressId;

            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                await _keepingService.UpdateAnimalWithKeepings(animal, model);

                await _needsService.UpdateAnimalWithNeeds(animal, model);

                await _vaccinationService.UpdateAnimalWithVaccination(animal, model);

                await _processingService.UpdateAnimalWithProcessing(animal, model);

                await _attitudesToService.UpdateAnimalWithAttitudes(animal, model);

                await _defectService.UpdateAnimalWithDefects(animal, model);

                _mapper.Map(animal, model);
                await _breedService.UpdateAnimalWithBreed(animal, model);

                _isNewService.UpdateIsNewCheckbox(animal, model);
                _repository.Update(model);

                if (isLocationNew)
                {
                    await _locationHistoryService.WriteAnimalLocationHistory(model);
                }
                await _repository.SaveAsync();

                scope.Complete();
            }

            var returnModel = GetById(model.Id);

            return(returnModel);
        }