Exemplo n.º 1
0
        public async Task <ActionResult> Create(CreateArticleInputModel model)
        {
            var articleDto = await this.articlesService
                             .CreateArticleAsync(model);

            return(Ok(articleDto));
        }
        // GET: Administration/BlogArticles/Create
        // public IActionResult Create()
        // {
        //    this.ViewData["AuthorId"] = new SelectList(this.dataRepository.Users, "Id", "Id");
        //    this.ViewData["CategoryId"] = new SelectList(this.dataRepository.BlogCategories, "Id", "Id");
        //    return this.View();
        // }

        //// POST: Administration/BlogArticles/Create
        //// To protect from overposting attacks, enable the specific properties you want to bind to.
        //// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        // [HttpPost]
        // [ValidateAntiForgeryToken]
        // public async Task<IActionResult> Create([Bind("CategoryId,Title,Content,AuthorId,IsDeleted,DeletedOn,Id,CreatedOn,ModifiedOn")] BlogArticle blogArticle)
        // {
        //    if (this.ModelState.IsValid)
        //    {
        //        this.dataRepository.Add(blogArticle);
        //        await this.dataRepository.SaveChangesAsync();
        //        return this.RedirectToAction(nameof(this.Index));
        //    }

        // this.ViewData["AuthorId"] = new SelectList(this.dataRepository.Users, "Id", "Id", blogArticle.AuthorId);
        //    this.ViewData["CategoryId"] = new SelectList(this.dataRepository.BlogCategories, "Id", "Id", blogArticle.CategoryId);
        //    return this.View(blogArticle);
        // }
        public IActionResult Create()
        {
            var viewModel = new CreateArticleInputModel();

            viewModel.Categories = this.categoriesService.GetAllAsKeyValuePairs();
            return(this.View(viewModel));
        }
Exemplo n.º 3
0
        public async Task <ArticleDto> CreateArticleAsync(CreateArticleInputModel model)
        {
            var currentUserId = this.currentUserService
                                .UserId;

            var articleForDb = model
                               .MapTo <Article>();

            articleForDb.AuthorId = currentUserId;

            var messageData = new ArticlesCreatedMessage {
            };

            var message = new Message(messageData);

            await this.Save(articleForDb, message);

            await this.publisher.Publish(messageData);

            await this.MarkMessageAsPublished(message.Id);

            var articleToReturn = articleForDb
                                  .MapTo <ArticleDto>();

            return(articleToReturn);
        }
Exemplo n.º 4
0
        public int CreateArticle(CreateArticleInputModel inputModel)
        {
            Article newArticle = new Article();
            newArticle.Title = inputModel.Title;
            newArticle.Summary = inputModel.Summary;
            newArticle.Content = inputModel.Content;
            newArticle.DateAdded = DateTime.Now;
            newArticle.DisplayOrder = inputModel.DisplayOrder;

            this.Data.Articles.Add(newArticle);
            this.Data.SaveChanges();

            Image defaultImage = new Image
            {
                ImageExtension = "jpg",
                ImagePath = "Content\\images\\noimage\\no-image",
                IsPrimary = true,
                DateAdded = DateTime.Now
            };

            newArticle.Image = defaultImage;
            this.Data.SaveChanges();

            return newArticle.Id;
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Create(CreateArticleInputModel model)
        {
            var result = await this.articlesService
                         .CreateArticle(model);

            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 6
0
        public IActionResult Create()
        {
            var inputModel = new CreateArticleInputModel
            {
                Categories = this.dbContext.Categories.ToList()
            };

            return(this.View(inputModel));
        }
        public async Task <IActionResult> Create()
        {
            var model = new CreateArticleInputModel()
            {
                Categories = await this.categoriesService.GetAllAsSelectListItemAsync(),
            };

            return(this.View(model));
        }
Exemplo n.º 8
0
        public async Task <IActionResult> Create(CreateArticleInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }
            var id = await this.articlesService.Create(input.Title, input.ArticleDescription, input.Content, input.ImgUrl, input.CategoryId);

            return(RedirectToAction("Details", new { id = id }));
        }
Exemplo n.º 9
0
        public IActionResult Create()
        {
            var articleCategories = this.articlesCategoriesService.GetAll <ArticlesCategoryDropDowwViewModel>();
            var viewModel         = new CreateArticleInputModel
            {
                ArticlesCategories = articleCategories,
            };

            return(this.View(viewModel));
        }
Exemplo n.º 10
0
        public ActionResult Edit(int id)
        {
            CreateArticleInputModel model = new CreateArticleInputModel();

            if (this.ArticlesService.ArticleExists(id))
            {
                model = this.ArticlesService.GetArticleInputModelById(id);
            }

            return View(model);
        }
Exemplo n.º 11
0
        public void Setup()
        {
            this.testArticle = new CreateArticleInputModel
            {
                Title      = "test",
                CategoryId = 1,
                Content    = "testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest",
            };

            this.articleList = new List <BlogArticle>();
            AutoMapperConfig.RegisterMappings(typeof(ArticleViewModel).Assembly, typeof(BlogArticle).Assembly);
        }
        public async Task <IActionResult> Create(CreateArticleInputModel input)
        {
            if (this.User.IsInRole("Administrator"))
            {
                if (!this.ModelState.IsValid)
                {
                    return(this.View(input));
                }

                var id = await this.articlesService.Create(input.CategoryId, input.Content, input.Title);

                return(this.RedirectToAction("Details", new { id = id }));
            }
            return(this.RedirectToAction("AllByCategory", new { id = input.CategoryId }));
        }
Exemplo n.º 13
0
        public void TestIf_Create_AddsAnArticle()
        {
            Assert.Empty(this.context.Articles);

            var article = new CreateArticleInputModel();

            this.context.Users.Add(new ParagraphUser
            {
                UserName = "******"
            });
            this.context.SaveChanges();

            this.articleService.Create(article, "test");

            Assert.Single(this.context.Articles);
        }
Exemplo n.º 14
0
        public ActionResult Create(CreateArticleInputModel inputModel)
        {
            if (ModelState.IsValid)
            {
                int newPageId = this.ArticlesService.CreateArticle(inputModel);
                if (newPageId > 0)
                {
                    TempData["message"] = "Статията беше добавена успешно!";
                    TempData["messageType"] = "success";
                    return RedirectToAction("Index");
                }
            }

            TempData["message"] = "Невалидни данни за статията!<br/> Моля попълнете <strong>всички</strong> задължителни полета!";
            TempData["messageType"] = "danger";
            return View(inputModel);
        }
        public async Task <IActionResult> Create(CreateArticleInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                input.Categories = await this.categoriesService.GetAllAsSelectListItemAsync();

                return(this.View(input));
            }

            var stylistId = this.userManager.GetUserId(this.User);

            string articleId = await this.articlesService.CreateAsync(input.Title, input.Content, input.CategoryId, input.Picture, stylistId);

            this.TempData["InfoMessage"] = GlobalMessages.SuccessCreateMessage;

            return(this.RedirectToAction(nameof(this.GetAllForStylist)));
        }
        public async Task CreateAsync(CreateArticleInputModel input, string userId)
        {
            var item = new BlogArticle()
            {
                AuthorId   = userId,
                CategoryId = input.CategoryId,
                Title      = input.Title,
                Content    = input.Content,
            };

            var category = this.categoryRepository.All().FirstOrDefault(x => x.Id == input.CategoryId);

            item.Category = category;

            await this.articleRepository.AddAsync(item);

            await this.articleRepository.SaveChangesAsync();
        }
Exemplo n.º 17
0
        public async Task <IActionResult> Create(CreateArticleInputModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(inputModel));
            }

            ArticleServiceModel articleServiceModel = inputModel.To <ArticleServiceModel>();

            string pictureUrl = await this.cloudinaryService
                                .UploadPictureAsync(inputModel.ImageFormFile, inputModel.Title);

            ArticleServiceModel productFromDb = await this.articleService.CreateArticleAsync(articleServiceModel, this.User.FindFirstValue(ClaimTypes.NameIdentifier));

            await this.imageService.CreateWithArticleAsync(pictureUrl, productFromDb.Id);

            return(this.RedirectToAction("All"));
        }
Exemplo n.º 18
0
        public IActionResult Create(CreateArticleInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                this.ViewData["Categories"] = categoryService.GetCategories()
                                              .Select(p => new SelectListItem
                {
                    Value = p.Id.ToString(),
                    Text  = p.Name
                });

                return(this.View(model));
            }

            var user = this.User.Identity.Name;
            int id   = this.articleService.Create(model, user);

            return(this.RedirectToAction("Details", "Article", new { id = id }));
        }
Exemplo n.º 19
0
        public int Create(CreateArticleInputModel model, string username)
        {
            var category = this.categoryRepository.All().SingleOrDefault(p => p.Id == model.CategoryId);
            var author   = this.userRepository.All().FirstOrDefault(p => p.UserName == username);


            var article = new Data.Models.Article
            {
                Title      = model.Title,
                Content    = model.Content,
                CategoryId = model.CategoryId,
                Author     = author,
            };

            this.articleRepository.AddAsync(article);
            this.articleRepository.SaveChangesAsync();

            return(this.articleRepository.All().FirstOrDefault(p => p.Title == model.Title).Id);
        }
        public ActionResult Create(CreateArticleInputModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var category = categoryService.GetByName(model.Category);

            if (category == null)
            {
                category = new Category
                {
                    Name = model.Category
                };

                categoryService.SaveItem(category);
            }

            var username = User.Identity.Name;

            var author = new Author
            {
                Username = username
            };

            var article = new Article
            {
                Title      = model.Title,
                Author     = author,
                CategoryId = category.Id,
                Content    = model.Content,
                CreatedOn  = DateTime.UtcNow,
            };

            DbContext.Articles.Add(article);
            DbContext.SaveChanges();

            TempData["success"] = SuccessMessages.ArticleCreatedSuccess;

            return(RedirectToAction("All"));
        }
Exemplo n.º 21
0
        public async Task <IActionResult> Create(CreateArticleInputModel input)
        {
            ApplicationUser author = await this.userManager.GetUserAsync(this.User);

            Category category = this.dbContext.Categories.FirstOrDefault(c => c.Id == input.CategoryId);

            var article = new Article
            {
                Title       = input.Title,
                Body        = input.Body,
                PublishedOn = DateTime.UtcNow,
                Views       = 1,
            };

            category.Articles.Add(article);
            author.Articles.Add(article);
            await this.dbContext.SaveChangesAsync();

            return(this.RedirectToAction("Id", new { id = article.Id }));
        }
        public async Task <IActionResult> Create(CreateArticleInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            var user = await this.userManager.GetUserAsync(this.User);

            try
            {
                await this.articlesService.CreateAsync(input, user.Id);
            }
            catch (Exception ex)
            {
                this.ModelState.AddModelError(string.Empty, ex.Message);
                return(this.View(input));
            }

            return(this.Redirect("/Blog/All"));
        }
Exemplo n.º 23
0
        public async Task <IActionResult> Create(CreateArticleInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            var user = await this.userManager.GetUserAsync(this.User);

            try
            {
                await this.articlesService.CreateAsync(input, user.Id, $"{this.environment.WebRootPath}/images");
            }
            catch (Exception ex)
            {
                this.ModelState.AddModelError(string.Empty, ex.Message);
                return(this.View(input));
            }

            return(this.RedirectToAction(nameof(this.All)));
        }
        public async Task <int> CreateAsync(CreateArticleInputModel input, string userId, string imagePath)
        {
            var article = new Article
            {
                Name               = input.Name,
                Description        = input.Description,
                ArticlesCategoryId = input.ArticlesCategoryId,
                CreatedByUserId    = userId,
            };

            Directory.CreateDirectory($"{imagePath}/articles/");
            foreach (var image in input.Images)
            {
                var extension = Path.GetExtension(image.FileName).TrimStart('.');
                if (!this.allowedExtensions.Any(x => extension.EndsWith(x)))
                {
                    throw new Exception($"Invalid image extension {extension}");
                }

                var dbImage = new Image
                {
                    AddedByUserId = userId,
                    Extension     = extension,
                };
                article.Images.Add(dbImage);

                var physicalPath = $"{imagePath}/articles/{dbImage.Id}.{extension}";
                using Stream fileStream = new FileStream(physicalPath, FileMode.Create);
                await image.CopyToAsync(fileStream);
            }

            await this.articlesRepository.AddAsync(article);

            await this.articlesRepository.SaveChangesAsync();

            return(article.Id);
        }
Exemplo n.º 25
0
 public ActionResult Edit(int id, CreateArticleInputModel inputModel)
 {
     if (ModelState.IsValid)
     {
         bool IsUpdateSuccessfull = this.ArticlesService.UpdateArticle(id, inputModel);
         if (IsUpdateSuccessfull)
         {
             TempData["message"] = "Статията беше редактирана успешно!";
             TempData["messageType"] = "success";
             return RedirectToAction("Index");
         }
     }
     TempData["message"] = "Невалидни данни за статията!<br/> Моля попълнете <strong>всички</strong> задължителни полета!";
     TempData["messageType"] = "danger";
     return View(inputModel);
 }
Exemplo n.º 26
0
        public bool UpdateArticle(int id, CreateArticleInputModel inputModel)
        {
            Article dbArticle = this.Data.Articles.Find(id);
            if (dbArticle != null)
            {
                dbArticle.Title = inputModel.Title;
                dbArticle.Summary = inputModel.Summary;
                dbArticle.Content = inputModel.Content;
                dbArticle.DisplayOrder = inputModel.DisplayOrder;

                this.Data.SaveChanges();

                return true;
            }
            else
            {
                return false;
            }
        }
Exemplo n.º 27
0
        private CreateArticleInputModel MapPageInputModel(Article dbArticle)
        {
            CreateArticleInputModel model = new CreateArticleInputModel();
            model.Id = dbArticle.Id;
            model.Title = dbArticle.Title;
            model.Summary = dbArticle.Summary;
            model.Content = dbArticle.Content;
            model.DisplayOrder = dbArticle.DisplayOrder;
            model.Image = dbArticle.Image;

            return model;
        }