public async Task <IReadOnlyList <Category> > Handle(GetCategoryListQuery request, CancellationToken cancellationToken) { var categories = await this.repository.GetCategoriesAsync(); return(categories .Select(c => this.mapper.Map(c)) .ToList() .AsReadOnly()); }
public void TestExecuteShouldReturnListOfCategories() { //Arrange const int expectedCount = 3; var mockCategoryRepository = new Mock <ICategoryRepository>(); var categoryModel = _fixture.CreateMany <Category>(expectedCount); mockCategoryRepository.Setup(c => c.GetAll()).Returns(categoryModel.AsQueryable()); var sut = new GetCategoryListQuery(mockCategoryRepository.Object, _mapper); //Act var result = sut.Execute(); //Assert Assert.Equal(expectedCount, result.Count); }
public async Task GetCategoyListQueryHandler_WhenCalled_ReturnGetCategoryByIdLookupModel() { var returnModel = new GetCategoryListReturnModel { Categories = new List <GetCategoryListLookupModel> { new GetCategoryListLookupModel { CategoryId = 1, Name = "Category1" }, new GetCategoryListLookupModel { CategoryId = 2, Name = "Category2" } } }; _categoryServiceMock .Setup(x => x.GetAllCategoriesAsync(CancellationToken.None)) .ReturnsAsync(returnModel); var query = new GetCategoryListQuery(); var container = Registrations(); await using var scope = container.BeginLifetimeScope(); var options = scope.Resolve <DbContextOptions <TechnicalTestDbContext> >(); await using var context = new TechnicalTestDbContext(options); await SeedMockDataAsync(context); var handler = new GetCategoyListQueryHandler(_categoryServiceMock.Object); var result = await handler.Handle(query, CancellationToken.None); Assert.AreEqual(2, result.Categories.Count); Assert.AreEqual("Category1", result.Categories[0].Name); Assert.AreEqual("Category2", result.Categories[1].Name); }
public async Task <ActionResult <CategoryListViewModel> > GetAll([FromQuery] GetCategoryListQuery query) { return(Ok(await Mediator.Send(query))); }
public async Task <List <CategoryListVm> > Handle(GetCategoryListQuery request, CancellationToken cancellationToken) { var allCategories = (await _categoryRepository.ListAllAsnc()).OrderBy(x => x.Name); return(_mapper.Map <List <CategoryListVm> >(allCategories)); }