public async Task <long> CreateAnimalWithBreed(AnimalForCreationDto animal, Animal model) { if (animal.Breed == null) { animal.Breed = new BreedForCreationDto { LanguageEn = "none", LanguageUa = "none" } } ; var breed = _breedRepository.GetAllQueryable().FirstOrDefault( x => x.BreedEnglish.ToLower() == animal.Breed.LanguageEn.ToLower() && x.BreedUkrainian.ToLower() == animal.Breed.LanguageUa.ToLower()); long breedId; if (breed != null) { breedId = breed.Id; } else { await _breedRepository.AddAsync(new Breed { BreedEnglish = animal.Breed.LanguageEn, BreedUkrainian = animal.Breed.LanguageUa }); var breedTemp = _breedRepository.GetAllQueryable().FirstOrDefault( x => x.BreedEnglish.ToLower() == animal.Breed.LanguageEn.ToLower() && x.BreedUkrainian.ToLower() == animal.Breed.LanguageUa.ToLower()); breedId = breedTemp.Id; } await _breedRepository.SaveAsync(); return(breedId); }
private DateTime SetNextVaccinationDate(AnimalForCreationDto animal, VaccinationFullForCreationDto vaccination) { var animalYears = DateTime.Now - animal.DateOfBirth; var nextDate = (animalYears < DateTime.Now.AddYears(1) - DateTime.Now) ? (vaccination.VaccinationDate.Date.AddDays(21)) : (vaccination.VaccinationDate.Date.AddYears(1)); return(nextDate); }
public async Task <AnimalDto> CreateAnimal(AnimalForCreationDto animal) { var model = new Animal(); _mapper.Map(animal, model); using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { model.BreedId = await _breedService.CreateAnimalWithBreed(animal, model); await _repository.AddAsync(model); await _descriptionService.CreateAnimalWithDescription(animal, model); await _locationHistoryService.WriteAnimalLocationHistory(model); await _isNewService.CreateCheckNewOld(animal, model); await _keepingService.CreateAnimalWithKeepings(animal, model); await _needsService.CreateAnimalWithNeeds(animal, model); await _vaccinationService.CreateAnimalWithVaccination(animal, model); await _processingService.CreateAnimalWithProcessing(animal, model); await _attitudesToService.CreateAnimalWithAttitudes(animal, model); await _defectService.CreateAnimalWithDefects(animal, model); scope.Complete(); } var returnModel = GetById(model.Id); return(returnModel); }
public async Task CreateCheckNewOld(AnimalForCreationDto animalDto, Animal model) { if (animalDto.IsNew && DateTime.Now - animalDto.FoundDate >= DateTime.Now.AddDays(int.Parse(_configuration["DurationAnimalNewStatus"])) - DateTime.Now) { model.FoundDate = DateTime.Now; await _animalRepository.SaveAsync(); } }
public AnimalModelValidator(AnimalForCreationDto model) { if (model == null) { throw new ArgumentNullException(nameof(model), ExceptionStrings.NullArgumentException); } Model = model; StringValidator = new StringValidator(model.Name, MIN_LENGTH, MAX_LENGTH, ValidationStrings.AnimalNameLength); }
public async Task CreateAnimalWithDescription(AnimalForCreationDto animal, Animal model) { if (animal.Description == null) { return; } await _descriptionRepository.AddAsync(new AnimalDescription { LanguageEn = animal.Description.LanguageEn, LanguageUa = animal.Description.LanguageUa, AnimalId = model.Id }); await _descriptionRepository.SaveAsync(); }
public async Task CreateAnimalWithKeepings(AnimalForCreationDto animal, Animal model) { if (animal.Keepings == null) { animal.Keepings = new HashSet <KeepingDto>(); } _animalKeepingRepository.TryCreateManyToMany(animal.Keepings.Select(x => x.Id) .Select(x => new AnimalKeeping { KeepingId = x, AnimalId = model.Id, })); await _animalKeepingRepository.SaveAsync(); }
public async Task CreateAnimalWithDefects(AnimalForCreationDto animal, Animal model) { if (animal.Defects == null) { animal.Defects = new HashSet <DefectDto>(); } _animalDefectRepository.TryCreateManyToMany(animal.Defects .Select(x => new AnimalDefects { DefectsId = x.Id, AnimalId = model.Id, })); await _animalDefectRepository.SaveAsync(); }
public async Task CreateAnimalWithNeeds(AnimalForCreationDto animal, Animal model) { if (animal.Needs == null) { animal.Needs = new HashSet <NeedsDto>(); } _animalNeedsRepository.TryCreateManyToMany(animal.Needs.Select(x => x.Id) .Select(x => new AnimalNeeds { NeedsId = x, AnimalId = model.Id })); await _animalNeedsRepository.SaveAsync(); }
public async Task CreateAnimalWithAttitudes(AnimalForCreationDto animal, Animal model) { if (animal.Attitudes == null) { animal.Attitudes = new HashSet <AttitudeToDto>(); } _animalAttitudeToRepository.TryCreateManyToMany(animal.Attitudes .Select(x => new AnimalAttitudeTo { AttitudeId = x.Id, AnimalId = model.Id, Mark = x.Mark })); await _animalAttitudeToRepository.SaveAsync(); }
public async Task CreateAnimalWithVaccination(AnimalForCreationDto animal, Animal model) { if (animal.Vaccinations == null) { animal.Vaccinations = new HashSet <VaccinationFullForCreationDto>(); } _animalVaccinationRepository.TryCreateManyToMany(animal.Vaccinations .Select(x => new AnimalVaccination { VaccinationId = x.Id, AnimalId = model.Id, VaccinationDate = x.VaccinationDate, NextVaccinationDate = SetNextVaccinationDate(animal, x) })); await _animalVaccinationRepository.SaveAsync(); }
public async Task CreateAnimalWithVaccination_Success() { var animal = new AnimalForCreationDto() { Vaccinations = new HashSet <VaccinationFullForCreationDto>() { new VaccinationFullForCreationDto() { Id = 1, Name = "testName", Type = "testType", VaccinationDate = new System.DateTime(2020, 4, 20) } } }; 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 }; _animalVaccinationRepositoryMock.Setup(x => x.TryCreateManyToMany(animalVaccinations)); _animalVaccinationRepositoryMock.Setup(x => x.SaveAsync()); await _service.CreateAnimalWithVaccination(animal, model); _animalVaccinationRepositoryMock.Verify(x => x.TryCreateManyToMany(It.IsAny <IEnumerable <AnimalVaccination> >()), Times.Once()); _animalVaccinationRepositoryMock.Verify(x => x.SaveAsync(), Times.Once()); }
public async Task CreateAnimalWithDefeects_Success() { var animal = new AnimalForCreationDto() { Defects = new HashSet <DefectDto>() { new DefectDto() { Id = 1, Type = "testType" } } }; var animalDefects = new HashSet <AnimalDefects>() { new AnimalDefects() { AnimalId = 1, Animal = new Animal() { Id = 1 }, DefectsId = 1, Defect = new Defect() { Id = 1, Type = "testType" } } }; var model = new Animal() { Id = 1 }; _animalDefectRepositoryMock.Setup(x => x.TryCreateManyToMany(animalDefects)); _animalDefectRepositoryMock.Setup(x => x.SaveAsync()); await _service.CreateAnimalWithDefects(animal, model); _animalDefectRepositoryMock.Verify(x => x.TryCreateManyToMany(It.IsAny <IEnumerable <AnimalDefects> >()), Times.Once()); _animalDefectRepositoryMock.Verify(x => x.SaveAsync(), Times.Once()); }
public async Task CreateAnimalWithProcessing(AnimalForCreationDto animal, Animal model) { if (animal.Processings == null) { animal.Processings = new HashSet <ProcessingDto>(); } _animalProcessingRepository.TryCreateManyToMany(animal.Processings .Select(x => new AnimalProcessing { ProcessingId = x.Id, AnimalId = model.Id, ProcessingDate = x.ProcessingDate, NextProcessingDate = x.IsRepeat ? GetNextProcessingDateForCreate(_processingRepository.Find(p => p.Id == x.Id).FirstOrDefault().Type , x.ProcessingDate, x.NextProcessingDate) : DateTime.MinValue, IsRepeat = x.IsRepeat })); await _animalProcessingRepository.SaveAsync(); }
public async Task CreateAnimalWithProcessing_Success() { var animal = new AnimalForCreationDto() { Processings = new HashSet <ProcessingDto>() { new ProcessingDto() { Id = 1 } } }; var animalProcessing = new HashSet <AnimalProcessing>() { new AnimalProcessing() { Animal = new Animal() { Id = 1 }, AnimalId = 1, ProcessingId = 1, Processing = new Processing() { Id = 1 } } }; var model = new Animal() { Id = 1 }; _animalProcessingRepositoryMock.Setup(x => x.TryCreateManyToMany(animalProcessing)); _animalProcessingRepositoryMock.Setup(x => x.SaveAsync()); await _service.CreateAnimalWithProcessing(animal, model); _animalProcessingRepositoryMock.Verify(x => x.TryCreateManyToMany(It.IsAny <IEnumerable <AnimalProcessing> >()), Times.Once()); _animalProcessingRepositoryMock.Verify(x => x.SaveAsync(), Times.Once()); }
public async Task CreateAnimalWithNeeds_Success() { var animal = new AnimalForCreationDto() { Needs = new HashSet <NeedsDto>() { new NeedsDto() { 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 }; _animalNeedsRepositoryMock.Setup(x => x.TryCreateManyToMany(animalNeeds)); _animalNeedsRepositoryMock.Setup(x => x.SaveAsync()); await _service.CreateAnimalWithNeeds(animal, model); _animalNeedsRepositoryMock.Verify(x => x.TryCreateManyToMany(It.IsAny <IEnumerable <AnimalNeeds> >()), Times.Once()); _animalNeedsRepositoryMock.Verify(x => x.SaveAsync(), Times.Once()); }
public async Task CreateAnimalWithAttitudes_Success() { var animal = new AnimalForCreationDto() { Attitudes = new HashSet <AttitudeToDto>() { new AttitudeToDto() { 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 }; _animalAttitudeToRepositoryMock.Setup(x => x.TryCreateManyToMany(animalAttitude)); _animalAttitudeToRepositoryMock.Setup(x => x.SaveAsync()); await _service.CreateAnimalWithAttitudes(animal, model); _animalAttitudeToRepositoryMock.Verify(x => x.TryCreateManyToMany(It.IsAny <IEnumerable <AnimalAttitudeTo> >()), Times.Once()); _animalAttitudeToRepositoryMock.Verify(x => x.SaveAsync(), Times.Once()); }
public async Task <ActionResult <AnimalDto> > Create([FromBody] AnimalForCreationDto animalModel) { try { if (animalModel == null) { return(BadRequest()); } new AnimalModelValidator(animalModel).ValidateModel(); var animal = await _animalService.CreateAnimal(animalModel); return(Ok(animal)); } catch (ValidationException ex) { return(BadRequest(ex.ToString())); } catch (Exception ex) { return(BadRequest(ex.Message)); } }