Beispiel #1
0
        public async Task UpdateAnimalWithAttitudes_Success()
        {
            var animal = new AnimalDto()
            {
                Id = 1
            };
            var animalAttitude = new HashSet <AnimalAttitudeTo>()
            {
                new AnimalAttitudeTo()
                {
                    Animal = new Animal()
                    {
                        Id = 1
                    },
                    AnimalId   = 1,
                    AttitudeId = 1,
                    AttitudeTo = new AttitudeTo()
                    {
                        Id = 1
                    }
                }
            };
            var model = new Animal()
            {
                Id = 1
            };
            var func  = new System.Func <AnimalAttitudeTo, long>(x => x.AttitudeId);
            var func2 = new System.Func <AnimalAttitudeTo, int>(x => x.Mark);

            _animalAttitudeToRepositoryMock.Setup(x => x.TryUpdateManyToMany(animalAttitude,
                                                                             animalAttitude, func, func2));
            _animalAttitudeToRepositoryMock.Setup(x => x.SaveAsync());

            await _service.UpdateAnimalWithAttitudes(animal, model);

            _animalAttitudeToRepositoryMock.Verify(x => x.TryUpdateManyToMany(It.IsAny <IEnumerable <AnimalAttitudeTo> >(),
                                                                              It.IsAny <IEnumerable <AnimalAttitudeTo> >(),
                                                                              It.IsAny <System.Func <AnimalAttitudeTo, long> >(),
                                                                              It.IsAny <System.Func <AnimalAttitudeTo, int> >()),
                                                   Times.Once());
            _animalAttitudeToRepositoryMock.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);
        }