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)); }
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); }
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; }
public async Task <IActionResult> Create(CreateArticleInputModel model) { var result = await this.articlesService .CreateArticle(model); return(RedirectToAction(nameof(Index))); }
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)); }
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 })); }
public IActionResult Create() { var articleCategories = this.articlesCategoriesService.GetAll <ArticlesCategoryDropDowwViewModel>(); var viewModel = new CreateArticleInputModel { ArticlesCategories = articleCategories, }; return(this.View(viewModel)); }
public ActionResult Edit(int id) { CreateArticleInputModel model = new CreateArticleInputModel(); if (this.ArticlesService.ArticleExists(id)) { model = this.ArticlesService.GetArticleInputModelById(id); } return View(model); }
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 })); }
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); }
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(); }
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")); }
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 })); }
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")); }
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")); }
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); }
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); }
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; } }
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; }