Skip to content

Latest commit

 

History

History
143 lines (108 loc) · 4.74 KB

step02.md

File metadata and controls

143 lines (108 loc) · 4.74 KB

KatlaSport Project

Шаг 2. CatalogueProduct

Задание

Расширить модель CatalogueProduct в проекте KatlaSport.DataAccess, добавив новые поля Description, ManufacturerCode и Price. Для этого шага в коде есть подсказки "TODO STEP 2".

Выполнение

  1. Создать новую ветку с именем "step2":
$ git checkout -b step2
$ git branch
  master
  step1
* step2
  1. Добавить новые поля 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; }
  1. Добавить описание свойства 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();
  1. Добавить новые поля 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; }
  1. Добавить новые правила в класс валидатора UpdateProductRequestValidator для FluentValidation:
RuleFor(r => r.Description).Length(0, 300);
RuleFor(r => r.ManufacturerCode).Length(4, 10);
RuleFor(r => r.Price).GreaterThanOrEqualTo(0);
  1. Изменить маппинг в классе 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));
  1. Добавить новую миграцию с именем "AddProductDescriptionManCodePrice":
PM> Add-Migration -Name AddProductDescriptionManCodePrice
  1. Собрать проект и исправить ошибки и предупреждения.

  2. Обновить базу данных.

PM> Update-Database
  1. Сохранить изменения как отдельный коммит.
$ 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
  1. Слить ветку "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

Проверка

Материалы

Материалы для самостоятельного изучения: