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;

            await SetNameAsync(material, materialData.Name);

            bool isDescriptionEditable = newCategory.IsDescriptionEditable();

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

            if (material.IsHidden != materialData.IsHidden && materialsAuthorization.CanHide(User.Roles, newCategory))
            {
                material.IsHidden = materialData.IsHidden;
            }

            if (material.IsCommentsBlocked != materialData.IsCommentsBlocked &&
                materialsAuthorization.CanBlockComments(User.Roles, newCategory))
            {
                material.IsCommentsBlocked = materialData.IsCommentsBlocked;
            }

            // Если категория новая, то обновляем
            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());
        }
Exemple #2
0
        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;

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

            await SetNameAsync(material, materialData.Name);

            if (category.IsMaterialsSubTitleEditable)
            {
                material.SubTitle = materialData.SubTitle;
            }

            if (materialData.IsHidden && materialsAuthorization.CanHide(User.Roles, category))
            {
                material.IsHidden = true;
            }

            if (materialData.IsHidden && materialsAuthorization.CanBlockComments(User.Roles, category))
            {
                material.IsCommentsBlocked = true;
            }

            if (materialsAuthorization.CanEditSettingsJson(User.Roles, category))
            {
                material.SettingsJson = materialData.SettingsJson;
            }

            contentCache.InvalidateCache(category.Id);

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

            contentCache.InvalidateCache(category.Id);

            return(Ok());
        }
        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;
            
            await SetNameAsync(material, materialData.Name);

            material.SubTitle = newCategory.MaterialsSubTitleInputType == MaterialsSubTitleInputType.Manual
                ? materialData.SubTitle
                : null;

            if (material.IsHidden != materialData.IsHidden && materialsAuthorization.CanHide(User.Roles, newCategory))
                material.IsHidden = materialData.IsHidden;

            if (material.IsCommentsBlocked != materialData.IsCommentsBlocked &&
                materialsAuthorization.CanBlockComments(User.Roles, newCategory))
                material.IsCommentsBlocked = materialData.IsCommentsBlocked;

            if (material.CategoryId != newCategory.Id
                && materialsAuthorization.CanMove(User, categoriesCache.GetCategory(material.CategoryId), newCategory))
                material.CategoryId = newCategory.Id;

            material.SettingsJson = materialsAuthorization.CanEditSettingsJson(User.Roles, newCategory) 
                ? materialData.SettingsJson 
                : null;
            
            await materialsManager.UpdateAsync(material, materialData.Tags, newCategory);
            return Ok();
        }