public async Task UpdateAnimalWithVaccination_Success()
        {
            var animal = new AnimalDto()
            {
                Id = 1
            };
            var animalVaccinations = new HashSet <AnimalVaccination>()
            {
                new AnimalVaccination()
                {
                    AnimalId = 1,
                    Animal   = new Animal()
                    {
                        Id = 1
                    },
                    VaccinationId = 1,
                    Vaccination   = new Vaccination()
                    {
                        Id = 1
                    }
                }
            };
            var model = new Animal()
            {
                Id = 1
            };
            var func  = new System.Func <AnimalVaccination, long>(x => x.VaccinationId);
            var func2 = new System.Func <AnimalVaccination, System.DateTime>(x => x.VaccinationDate);
            var func3 = new System.Func <AnimalVaccination, System.DateTime>(x => x.NextVaccinationDate);

            _animalVaccinationRepositoryMock.Setup(x => x.TryUpdateManyToMany(animalVaccinations,
                                                                              animalVaccinations, func, func2, func3));
            _animalVaccinationRepositoryMock.Setup(x => x.SaveAsync());

            await _service.UpdateAnimalWithVaccination(animal, model);

            _animalVaccinationRepositoryMock.Verify(x => x.TryUpdateManyToMany(It.IsAny <IEnumerable <AnimalVaccination> >(),
                                                                               It.IsAny <IEnumerable <AnimalVaccination> >(),
                                                                               It.IsAny <System.Func <AnimalVaccination, long> >(),
                                                                               It.IsAny <System.Func <AnimalVaccination, System.DateTime> >(),
                                                                               It.IsAny <System.Func <AnimalVaccination, System.DateTime> >()),
                                                    Times.Once());
            _animalVaccinationRepositoryMock.Verify(x => x.SaveAsync(), Times.Once());
        }
Beispiel #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);
        }