示例#1
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());
        }
示例#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;

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