Exemplo n.º 1
0
        public async Task <IActionResult> Edit(int id, ArticleFormModel model)
        {
            if (!ModelState.IsValid)
            {
                model.Resources = await this.GetResourceListItems();

                model.Tags = await this.GetTagsListItems();

                var returnModel = new ArticleContainerModel
                {
                    ArticleFormModel = model,
                    Resources        = await this.resources.GetForArticleAsync(id),
                    Tags             = await this.tags.GetForArticleAsync(id)
                };

                return(View(returnModel));
            }

            var result = await this.articles.Edit(id, model.Title, model.Description, model.Subject, model.Contents);

            if (result == false)
            {
                return(NotFound());
            }

            await this.LinkResources(id, model.ResourceIds);

            await this.LinkTags(id, model.TagIds);

            await this.LinkNewTags(id, model.NewTags);

            TempData[TempDataSuccessMessageKey] = $"'{model.Title}' was edited successfully.";
            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create(ArticleFormModel model)
        {
            if (!ModelState.IsValid)
            {
                model.Resources = await this.GetResourceListItems();

                model.Tags = await this.GetTagsListItems();

                return(View(model));
            }

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

            var articleId = await this.articles.CreateAsync(
                model.Title,
                model.Description,
                model.Subject,
                model.Contents,
                DateTime.UtcNow,
                currentUserId
                );

            await this.LinkResources(articleId, model.ResourceIds);

            await this.LinkTags(articleId, model.TagIds);

            await this.LinkNewTags(articleId, model.NewTags);

            TempData[TempDataSuccessMessageKey] = $"'{model.Title}' was created successfully.";
            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Delete(int id, ArticleFormModel model)
        {
            var userId = this.userManager.GetUserId(this.User);

            if (userId == null)
            {
                this.TempData.AddErrorMessage(WebConstants.InvalidUserMsg);
                return(this.RedirectToAction(nameof(Index)));
            }

            var exists = await this.articleService.ExistsForAuthorAsync(id, userId);

            if (!exists ||
                id != model.Id)
            {
                this.TempData.AddInfoMessage(WebConstants.ArticleNotFoundForAuthorMsg);
                return(this.RedirectToAction(nameof(Index)));
            }

            var success = await this.articleService.RemoveAsync(id, userId);

            if (!success)
            {
                this.TempData.AddInfoMessage(WebConstants.ArticleDeleteErrorMsg);
                return(this.View(ArticleFormView, model));
            }

            this.TempData.AddSuccessMessage(WebConstants.ArticleDeleteSuccessMsg);

            return(this.RedirectToAction(nameof(Index)));
        }
        [ValidateModelState(ArticleFormView)] // attribute simple model validation replaces: if (!this.ModelState.IsValid) etc.
        public async Task <IActionResult> Edit(int id, ArticleFormModel model)
        {
            var userId = this.userManager.GetUserId(this.User);

            if (userId == null)
            {
                this.TempData.AddErrorMessage(WebConstants.InvalidUserMsg);
                return(this.RedirectToAction(nameof(Index)));
            }

            var exists = await this.articleService.ExistsForAuthorAsync(id, userId);

            if (!exists ||
                id != model.Id)
            {
                this.TempData.AddInfoMessage(WebConstants.ArticleNotFoundForAuthorMsg);
                return(this.RedirectToAction(nameof(Index)));
            }

            var success = await this.articleService.UpdateAsync(id, model.Title, model.Content, userId);

            if (!success)
            {
                this.TempData.AddInfoMessage(WebConstants.ArticleUpdateErrorMsg);
            }
            else
            {
                this.TempData.AddSuccessMessage(WebConstants.ArticleUpdateSuccessMsg);
            }

            return(this.RedirectToAction(nameof(Details), new { id }));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Post([FromBody] ArticleFormModel model, [FromServices] IMapper mapper)
        {
            var command = new CreateArticleCommand(CurrentUser);

            mapper.Map(model, command);
            var articleId = await Mediator.Send(command);

            return(Created(await GetArticleById(articleId)));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Create()
        {
            var model = new ArticleFormModel
            {
                Resources = await this.GetResourceListItems(),
                Tags      = await this.GetTagsListItems()
            };

            return(View(model));
        }
Exemplo n.º 7
0
        public async Task <int> AddAsync(ArticleFormModel articleModel)
        {
            var article = this.mapper.Map <Article>(articleModel);

            await this.dbContext.Articles.AddAsync(article);

            await this.dbContext.SaveChangesAsync();

            return(article.Id);
        }
Exemplo n.º 8
0
        public async Task <IActionResult> Edit(int id, ArticleFormModel model)
        {
            await this.articles.EditAsync(id, model.Title, model.Content, model.ThumbnailUrl);

            TempData.AddSuccessMessage($"Article {model.Title} successfully edited!");

            return(RedirectToAction(
                       nameof(Web.Controllers.NewsController.Index),
                       "News",
                       new { area = string.Empty }));
        }
 public Task <IActionResult> Create(ArticleFormModel article)
 => this.ModelState
 .ToResult()
 .Ensure(ms => ms.IsValid, "Model state is not valid.")
 .Tap(async _ => await this.articleService
      .Add(article.Title, article.Content, this.User.GetId()))
 .Tap(_ => this.TempData.Add(
          ControllerConstants.SuccessMessage,
          "Article created successfully is waiting for approval!"))
 .Match(
     Some: _ => this.RedirectToAction(nameof(this.Mine)),
     None: () => this.View(article));
Exemplo n.º 10
0
        public async Task <IActionResult> Create(ArticleFormModel article)
        {
            if (this.ModelState.IsValid)
            {
                await this.articleService.Add(article.Title, article.Content, this.User.GetId());

                this.TempData.Add(ControllerConstants.SuccessMessage, "Article created successfully it is waiting for approval!");

                return(this.RedirectToAction(nameof(this.Mine)));
            }

            return(this.View(article));
        }
Exemplo n.º 11
0
        public async Task <IActionResult> Create(ArticleFormModel model)
        {
            model.Content = this.html.Sanitize(model.Content);

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

            await this.articles.CreateAsync(model.Title, model.Content, model.ThumbnailUrl, userId);

            return(RedirectToAction(
                       nameof(Web.Controllers.HomeController.Index),
                       "News",
                       new { area = string.Empty }));
        }
Exemplo n.º 12
0
        public IActionResult Add(ArticleFormModel article)
        {
            if (!ModelState.IsValid)
            {
                return(View(article));
            }

            string authorId = this.userManager.GetUserId(HttpContext.User);

            this.articles.Add(authorId, article.Title, article.Content);

            this.GenerateMessage(SuccessArticleCreation, Alert.Success);

            return(RedirectToRoute(Routing.AllArticles));
        }
Exemplo n.º 13
0
        public async Task <bool> EditAsync(ArticleFormModel articleModel)
        {
            var article = this.dbContext.Articles.Find(articleModel.Id);

            if (article == null)
            {
                return(false);
            }

            article.Title   = articleModel.Title;
            article.Content = articleModel.Content;

            await this.dbContext.SaveChangesAsync();

            return(true);
        }
        public async Task <IActionResult> Edit(int id, ArticleFormModel article)
        {
            if (!await this.articleService.IsByUser(id, this.User.GetId()) && !this.User.IsAdministrator())
            {
                return(this.NotFound());
            }

            if (this.ModelState.IsValid)
            {
                await this.articleService.Edit(id, article.Title, article.Content);

                this.TempData.Add(ControllerConstants.SuccessMessage, "Article edited successfully and is waiting for approval!");

                return(this.RedirectToAction(nameof(this.Mine)));
            }

            return(this.View(article));
        }
Exemplo n.º 15
0
        public async Task <IActionResult> Edit(int id, ArticleFormModel model)
        {
            model.Description = this.html.Sanitize(model.Description);

            await this.articles.EditAsync(
                id,
                model.Title,
                model.Description,
                model.ReleaseDate,
                model.Author,
                model.Language,
                model.ShowAtFrontPage
                );

            this.TempData.AddSuccessMessage(string.Format(TempDataEditCommentText, ModelName, EndingLetterA));

            return(this.RedirectToAction(nameof(this.Articles)));
        }
Exemplo n.º 16
0
        public async Task <IActionResult> Put(int id, [FromBody] ArticleFormModel model, [FromServices] IMapper mapper)
        {
            var article = await GetArticleById(id);

            if (article == null)
            {
                return(NotFound());
            }

            if (!HasRights(UserRights.EditArticles) && article.AuthorId != CurrentUser.Id)
            {
                return(Forbid());
            }

            var updateCommand = new UpdateArticleCommand(article);

            mapper.Map(model, updateCommand);
            await Mediator.Send(updateCommand);

            return(Updated(await GetArticleById(id)));
        }
Exemplo n.º 17
0
        public async Task <IActionResult> Create(ArticleFormModel model)
        {
            model.Description = this.html.Sanitize(model.Description);

            if (model.Files == null || model.Files.Count == 0)
            {
                return(Content(NoSelectedFiles));
            }

            var gallery = new List <string>();

            foreach (var file in model.Files)
            {
                var path          = Path.Combine(ImageFolderName, GalleriesImageFolderName, Guid.NewGuid() + file.GetFileType());
                var pathForUpload = Path.Combine(Directory.GetCurrentDirectory(), RootFolderName, path);

                using (var stream = new FileStream(pathForUpload, FileMode.Create))
                {
                    await file.CopyToAsync(stream);
                }

                path = path.Replace("\\", "/");
                gallery.Add(String.Format(SavePath, path));
            }

            await this.articles.CreateAsync(
                model.Title,
                model.Description,
                model.ReleaseDate,
                model.Author,
                model.AddGallery,
                gallery,
                model.Language,
                model.ShowAtFrontPage
                );

            this.TempData.AddSuccessMessage(string.Format(TempDataCreateCommentText, ModelName, EndingLetterA));

            return(this.RedirectToAction(nameof(this.Articles)));
        }
        [ValidateModelState(ArticleFormView)] // attribute simple model validation replaces: if (!this.ModelState.IsValid) etc.
        public async Task <IActionResult> Create(ArticleFormModel model)
        {
            var userId = this.userManager.GetUserId(this.User);

            if (userId == null)
            {
                this.TempData.AddErrorMessage(WebConstants.InvalidUserMsg);
                return(this.View(model));
            }

            // Raw Html Content sanitized in service
            var id = await this.articleService.CreateAsync(model.Title, model.Content, userId);

            if (id < 0)
            {
                this.TempData.AddErrorMessage(WebConstants.ArticleCreateErrorMsg);
                return(this.View(model));
            }

            this.TempData.AddSuccessMessage(WebConstants.ArticleCreateSuccessMsg);
            return(this.RedirectToAction(nameof(Details), new { id }));
        }
Exemplo n.º 19
0
        public IActionResult Operate(ArticleFormModel model, IFormFile image)
        {
            if (ModelState.IsValid)
            {
                if (model.Id == 0 || model.Id == null)
                {
                    try
                    {
                        if (image != null)
                        {
                            string imageBase64Data = ImageOperations.GetBase64FromFile(image);
                            model.Image = imageBase64Data;
                        }
                        Article article = new Article
                        {
                            ColorId     = model.ColorId,
                            Detail      = model.Detail,
                            Image       = model.Image,
                            LongDetail  = model.LongDetail,
                            PageName    = model.PageName,
                            ScreenOrder = model.ScreenOrder,
                            SubTitle    = model.SubTitle,
                            Title       = model.Title
                        };

                        db.Add(article);
                        db.SaveChanges();
                        return(StatusCode(200, "Eklendi"));
                    }
                    catch (Exception e)
                    {
                        ModelState.AddModelError("error", "Error! An error occurred while album creating");
                    }
                }
                else
                {
                    var article = db.Article.Where(q => q.Id == model.Id).FirstOrDefault();
                    if (article == null)
                    {
                        ModelState.AddModelError("error", "Unknown Request!");
                    }
                    else
                    {
                        if (image != null)
                        {
                            string imageBase64Data = ImageOperations.GetBase64FromFile(image);
                            article.Image = imageBase64Data;
                        }
                        article.Title       = model.Title;
                        article.SubTitle    = model.SubTitle;
                        article.ScreenOrder = model.ScreenOrder;
                        article.PageName    = model.PageName;
                        article.LongDetail  = model.LongDetail;
                        article.Detail      = model.Detail;
                        article.ColorId     = model.ColorId;
                        db.Update(article);
                        db.SaveChanges();
                        return(StatusCode(200, "Güncellendi!"));
                    }
                }
            }
            return(BadRequest(new
            {
                Message = "Some error(s) occurred!",
                StatusCode = 400,
                ModelState = ModelState.ToList()
            }));
        }