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()); }
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(); }