Расширить модель ProductCategoryListItem в проекте KatlaSport.Services.Models, добавив новое поле ProductCount. Добавить в класс ProductCategoryService вычисление значения поля ProductCount. Для этого шага в коде есть подсказки "TODO STEP 3".
- Создать новую ветку с именем "step3":
$ git checkout -b step3
$ git branch
master
step1
step2
* step3
- Добавить новое поле ProductCount в класс API-модели ProductCategoryListItem:
/// <summary>
/// Gets or sets a count of products in a product category.
/// </summary>
public int ProductCount { get; set; }
- Расширить код метода GetCategoriesAsync в классе ProductCategoryService - установить для каждой категории значение поля ProductCount. Обязательно использовать асинхронный метод CountAsync и оператор await для вызова.
foreach (var c in categories)
{
c.ProductCount = await _context.Products.Where(p => p.CategoryId == c.Id).CountAsync();
}
-
Собрать проект, исправить ошибки и предупреждения (если есть).
-
Сохранить изменения как отдельный коммит.
$ git status
$ git diff
$ git add *
$ git commit -m "Add ProductCount to ProductCategoryListItem model. Get ProductCount evaluation logic to ProductCategoryService.GetCategoriesAsync."
$ git log
$ git push -u origin step3
- Слить ветку "step3" с веткой "master".
$ git checkout master
$ git branch
* master
step1
step2
step3
$ git merge --squash step3
$ git commit -m "Merge step3. Add ProductCount to ProductCategoryListItem model. Get ProductCount evaluation logic to ProductCategoryService.GetCategoriesAsync."
$ git push
- Запустить swagger-console.
- Открыть раздел "ProductCategories".
- Запустить "GET /api/categories".
- Найти в "Response Body" json-поле "productCount".
Материалы для самостоятельного изучения:
- Введение в Entity Framework
- LINQ to Entities
- Асинхронность
- Getting Started with LINQ in C#