public void GetAllSamplesByGivenSearchParams_FilteredBacteriaName_ShouldReturnRelevantSample() { // Arrange var db = this.GetDatabase(); var mapper = this.GetAutoMapper(); var searchParams = new SearchParametersBindingModel() { BacteriaName = "New Sample" }; var samples = new Sample() { Name = "New Sample" }; db.Samples.Add(samples); db.SaveChanges(); var service = new SampleService(db, mapper); // Act var result = service.GetAllSamplesByGivenSearchParams(searchParams); // Assert Assert.NotNull(result); Assert.IsType <List <SampleViewModel> >(result); var sampleName = result[0].Name; Assert.Equal("New Sample", sampleName); }
public void Get_GetListOfSamples_ShouldReturnListOfSamples() { // Arrange var searchParams = new SearchParametersBindingModel() { BacteriaName = "Test 1", Tags = new string[] { "Test 1" }, Groups = new string[] { "Test 1" }, NutrientAgarPlates = new string[] { "Test 1" } }; List <SampleViewModel> listOfsamples = new List <SampleViewModel>() { new SampleViewModel() { Id = new Guid(), Name = "Test 1", Description = "Test Description", Images = null, CreatedOn = DateTime.Now }, new SampleViewModel() { Id = new Guid(), Name = "Test 2", Description = "Test Description", Images = null, CreatedOn = DateTime.Now } }; var serviceMock = new Mock <ISampleService>(); serviceMock .Setup(s => s.GetAllSamplesByGivenSearchParams(searchParams)) .Returns(listOfsamples); var loggerMock = new Mock <ILogger>(); var controller = new SampleController(serviceMock.Object, null, null, loggerMock.Object); // Act var response = controller.GetListOfSamples(searchParams); // Assert Assert.NotNull(response); var okObjectResult = response as OkObjectResult; Assert.NotNull(okObjectResult); var models = okObjectResult.Value as List <SampleViewModel>; Assert.NotNull(models); Assert.Equal(2, models.Count()); }
public IActionResult GetListOfSamples(SearchParametersBindingModel searchParams) { if (searchParams.BacteriaName == null && searchParams.Tags == null && searchParams.Groups == null && searchParams.NutrientAgarPlates == null) { return(BadRequest("No search params has been sent.")); } var sampleViewModels = this.service.GetAllSamplesByGivenSearchParams(searchParams); logger.LogSuccess("All samples has been send"); return(Ok(sampleViewModels)); }
private List <Sample> FindSampleDependingOnGivenParams(SearchParametersBindingModel searchParams) { var samples = new List <Sample>(); // Searching by given name if (searchParams.Tags == null && searchParams.Groups == null && searchParams.BacteriaName != null && searchParams.NutrientAgarPlates == null) { samples = this.Context .Samples .Include(s => s.Images) .Where(sample => sample.Name.StartsWith(searchParams.BacteriaName)) .Take(100) .ToList(); } // Searching by Name plus any other provided search params else if (searchParams.BacteriaName != null && (searchParams.Tags != null || searchParams.Groups != null || searchParams.NutrientAgarPlates != null)) { var samplesFromDB = this.Context .Samples .AsNoTracking() .Include(s => s.Images) .Include(s => s.SampleTags) .ThenInclude(st => st.Tag) .Include(s => s.SampleGroups) .ThenInclude(sg => sg.Group) .Include(s => s.SampleNutrientAgarPlates) .ThenInclude(sn => sn.NutrientAgarPlate) .Where(sample => sample.Name.StartsWith(searchParams.BacteriaName)) .Take(100) .ToList(); // Filtering the samples containing certan name by any other given search param foreach (var sample in samplesFromDB) { var IsMatch = true; if (searchParams.Tags != null) { foreach (var sampleTag in sample.SampleTags) { if (!searchParams.Tags.Contains(sampleTag.Tag.Name)) { IsMatch = false; break; } } } if (searchParams.Groups != null) { foreach (var sampleGroup in sample.SampleGroups) { if (!searchParams.Groups.Contains(sampleGroup.Group.Name)) { IsMatch = false; break; } } } if (searchParams.NutrientAgarPlates != null) { foreach (var sampleNutrient in sample.SampleNutrientAgarPlates) { if (!searchParams.NutrientAgarPlates.Contains(sampleNutrient.NutrientAgarPlate.Name)) { IsMatch = false; break; } } } if (IsMatch) { samples.Add(sample); } } } return(samples); }
public List <SampleViewModel> GetAllSamplesByGivenSearchParams(SearchParametersBindingModel searchParams) { var samples = FindSampleDependingOnGivenParams(searchParams); return(this.mapper.Map <List <SampleViewModel> >(samples)); }