Расширить модель CatalogueProduct в проекте KatlaSport.DataAccess, добавив новые поля Description, ManufacturerCode и Price. Для этого шага в коде есть подсказки "TODO STEP 2".
- Создать новую ветку с именем "step2":
$ git checkout -b step2
$ git branch
master
step1
* step2
- Добавить новые поля Description, ManufacturerCode и Price в класс EF-модели CatalogueProduct:
/// <summary>
/// Gets or sets a product description.
/// </summary>
public string Description { get; set; }
/// <summary>
/// Gets or sets a product manufacturer code.
/// </summary>
public string ManufacturerCode { get; set; }
/// <summary>
/// Gets or sets a product price.
/// </summary>
public decimal? Price { get; set; }
- Добавить описание свойства Description, ManufacturerCode и Price в файл конфигурации класса CatalogueProductConfiguration.
Ограничения:
- Description - максимальная длина строки 300 символов, необязательное поле.
- ManufacturerCode - максимальная длина строки 10 символов, обязательное поле.
- Price - обязательное поле.
Property(i => i.Description).HasColumnName("product_description").HasMaxLength(300);
Property(i => i.ManufacturerCode).HasColumnName("product_manufacturer_code").HasMaxLength(10);
Property(i => i.Price).HasColumnName("product_price").IsOptional();
- Добавить новые поля Description, ManufacturerCode и Price в класс API-модели UpdateProductRequest:
/// <summary>
/// Gets or sets a product description.
/// </summary>
public string Description { get; set; }
/// <summary>
/// Gets or sets a product manufacturer code.
/// </summary>
public string ManufacturerCode { get; set; }
/// <summary>
/// Gets or sets a product price.
/// </summary>
public decimal Price { get; set; }
- Добавить новые правила в класс валидатора UpdateProductRequestValidator для FluentValidation:
RuleFor(r => r.Description).Length(0, 300);
RuleFor(r => r.ManufacturerCode).Length(4, 10);
RuleFor(r => r.Price).GreaterThanOrEqualTo(0);
- Изменить маппинг в классе ProductManagementMappingProfile:
Было:
CreateMap<DataAccessProduct, Product>();
Стало:
CreateMap<DataAccessProduct, Product>()
.ForMember(li => li.Description, opt => opt.MapFrom(p => p.Description == null ? string.Empty : p.Description))
.ForMember(li => li.ManufacturerCode, opt => opt.MapFrom(p => p.ManufacturerCode == null ? string.Empty : p.ManufacturerCode));
- Добавить новую миграцию с именем "AddProductDescriptionManCodePrice":
PM> Add-Migration -Name AddProductDescriptionManCodePrice
-
Собрать проект и исправить ошибки и предупреждения.
-
Обновить базу данных.
PM> Update-Database
- Сохранить изменения как отдельный коммит.
$ git status
$ git diff
$ git add *
$ git commit -m "Add Description, ManufacturerCode and Price property to CatalogueProduct and UpdateProductRequest model. Create a new migration AddProductDescriptionManCodePrice. Add new rules to UpdateProductRequestValidator."
$ git log
$ git push -u origin step2
- Слить ветку "step2" с веткой "master".
$ git checkout master
$ git branch
* master
step1
step2
$ git merge --squash step2
$ git commit -m "Merge step2. Add Description, ManufacturerCode and Price property to CatalogueProduct and UpdateProductRequest model. Create a new migration AddProductDescriptionManCodePrice. Add new rules to UpdateProductRequestValidator."
$ git push
Материалы для самостоятельного изучения:
- Введение в Entity Framework
- Fluent API и аннотации
- FluentValidation
- Creating a Validator Class
- Built in Validators