public virtual async Task <IActionResult> Update(MaterialRequestModel materialData)
        {
            if (!ModelState.IsValid)
            {
                var ers = ModelState.Values.SelectMany(v => v.Errors);
                return(BadRequest(string.Join(",\n ", ers.Select(x => x.ErrorMessage))));
            }

            Material material = await materialsManager.GetAsync(materialData.Id);

            if (material == null)
            {
                return(BadRequest());
            }

            if (!await materialsAuthorization.CanUpdateAsync(User, material))
            {
                return(Unauthorized());
            }

            var newCategory = categoriesCache.GetCategory(materialData.CategoryName);

            if (newCategory == null)
            {
                return(BadRequest());
            }

            material.Title    = materialData.Title;
            material.Text     = materialData.text;
            material.EditDate = DateTime.UtcNow;

            var result = await SetNameAsync(material, materialData.Name);

            if (result.Failed)
            {
                return(BadRequest(result.Error));
            }

            bool isDescriptionEditable = newCategory.IsDescriptionEditable();

            material.Description = isDescriptionEditable ? materialData.Description : null;

            // Если категория новая, то обновляем
            if (material.CategoryId != newCategory.Id &&
                materialsAuthorization.CanMove(User,
                                               categoriesCache.GetCategory(material.CategoryId),
                                               newCategory))
            {
                material.CategoryId = newCategory.Id;
            }

            await materialsManager.UpdateAsync(material, materialData.Tags, isDescriptionEditable);

            return(Ok());
        }
        public virtual async Task <IActionResult> Create(MaterialRequestModel materialData)
        {
            var category = categoriesCache.GetCategory(materialData.CategoryName);

            if (category == null)
            {
                return(BadRequest());
            }

            if (!materialsAuthorization.CanCreate(User.Roles, category))
            {
                return(Unauthorized());
            }

            var now = DateTime.UtcNow;

            Material material = new Material
            {
                Title        = materialData.Title,
                Text         = materialData.text,
                PublishDate  = now,
                LastActivity = now,
                CategoryId   = category.Id,
                AuthorId     = User.UserId
            };

            var result = await SetNameAsync(material, materialData.Name);

            if (result.Failed)
            {
                return(BadRequest(result.Error));
            }

            bool isDescriptionEditable = category.IsDescriptionEditable();

            if (isDescriptionEditable)
            {
                material.Description = materialData.Description;
            }

            contentCache.InvalidateCache(category.Id);

            await materialsManager.CreateAsync(material, materialData.Tags, isDescriptionEditable);

            return(Ok());
        }