Ejemplo n.º 1
0
        public async Task <IActionResult> Down(int id, int countMove = 1)
        {
            Material material = await materialManager.GetAsync(id);

            if (material != null)
            {
                if (countMove <= 0 || material.SortNumber - countMove < 1)
                {
                    return(BadRequest("Incorrect count move"));
                }
                if (await materialsAuthorization.CanUpdateAsync(User, material))
                {
                    try
                    {
                        await materialManager.DownAsync(id);

                        return(Ok());
                    }
                    catch
                    {
                    }
                }
                else
                {
                    return(Forbid());
                }
            }
            return(BadRequest("Invalid article ID"));
        }
        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());
        }
Ejemplo n.º 3
0
        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();
        }