Exemplo n.º 1
0
        public ActionResult Edit(int id, ArticleEditModel model)
        {
            if (model.Title == null)
            {
                ModelState.AddModelError(nameof(model.Title), "* 文章标题不能为空");
                return(View(model));
            }
            if (model.Body == null)
            {
                ModelState.AddModelError(nameof(model.Body), "* 文章正文不能为空");
                return(View(model));
            }
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            ArticleEditModel editModel = _articleService.GetArticleEditModelById(id);

            editModel.PublishTime = DateTime.Now;
            editModel.Title       = model.Title;
            editModel.Body        = model.Body;
            _articleService.SaveArticleEditModel(editModel);
            //ArticleEditModel ArticleUpdateModel = _articleService.GetArticleEditModelById(id);
            return(RedirectToAction("Single", new { id = id }));
        }
Exemplo n.º 2
0
        public IActionResult AddOrUpdateArticle([FromBody] ArticleEditModel model)
        {
            var customerId = _caller.GetCustomerId();

            if (model.CustomerId == 0)
            {
                model.CustomerId = customerId;
            }

            if (model.CustomerId != customerId && !this.IsAdmin())
            {
                return(Unauthorized());
            }

            var tagIds = GetTagsIds(model.TagsDisplayNames).Distinct();

            var entry = _appDbContext.Articles.Add(new Article
            {
                ArticleId  = model.ArticleId,
                Category   = model.Category,
                Content    = model.Content,
                CustomerId = model.CustomerId,
                Summary    = model.Summary,
                Title      = model.Title,
                Tags       = tagIds.Select(x => new ArticleAndTag {
                    ArticleTagId = x
                }).ToArray()
            });

            _appDbContext.SaveChanges();

            return(Ok(new { entry.Entity.ArticleId }));
        }
Exemplo n.º 3
0
        public bool Edit(long id, ArticleEditModel model)
        {
            try
            {
                var article = this.Data.Articles.All().FirstOrDefault(a => a.Id == id);
                Mapper.Map(model, article);

                this.Data.Articles.Update(article);
                this.Data.SaveChanges();

                if (model.CoverPhoto != null)
                {
                    this.PhotoService.UploadCoverPhoto(model.CoverPhoto, article.Id);
                }

                if (model.ArticlePhotos.Count > 1)
                {
                    foreach (var photo in model.ArticlePhotos)
                    {
                        var photoId = this.PhotoService.UploadPhoto(photo, article.Id);
                        var dbPhoto = this.PhotoService.GetDbPhoto(photoId);
                        article.Photos.Add(dbPhoto);
                    }

                    this.Data.SaveChanges();
                }
                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Edit(ArticleEditModel articleEditModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(articleEditModel));
            }

            var article = await this._articleService.GetById(articleEditModel.Id);

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

            var category = await this._categoryService.GetById(articleEditModel.CategoryId);

            if (category == null)
            {
                this.ModelState.AddModelError(string.Empty, "Invalid category selected.");
                return(this.View(articleEditModel));
            }

            if (article.CreatorId != await this.GetLoggedUserId())
            {
                this.ModelState.AddModelError(string.Empty, "You cannot edit somebody else's articles.");
                return(this.View(articleEditModel));
            }

            await this._articleService.Edit(articleEditModel);

            this.TempData.AddSerialized(WebConstants.AlertKey, new Alert(AlertType.Success, string.Format(DalConstants.SuccessMessage, "edited", "article")));

            return(this.RedirectToAction(nameof(Search)));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Edit(string id)
        {
            var articleViewModel = await this._articleService.GetById(id);

            if (articleViewModel == null)
            {
                return(this.NotFound());
            }

            if (articleViewModel.CreatorId != await this.GetLoggedUserId())
            {
                return(this.BadRequest());
            }

            var articleEditModel = new ArticleEditModel
            {
                Id         = articleViewModel.Id,
                Title      = articleViewModel.Title,
                Content    = articleViewModel.Content,
                CategoryId = articleViewModel.CategoryId,
                CoverUrl   = articleViewModel.CoverUrl
            };

            return(this.View(articleEditModel));
        }
Exemplo n.º 6
0
        public async Task <RequestWithPayloadResult <Article> > UpdateAsync(ArticleEditModel updatedArticleModel, string userId)
        {
            var article = await _context.Articles.Include(x => x.TagList).FirstOrDefaultAsync(x => x.Id == updatedArticleModel.Id);

            if (article == null)
            {
                return(new RequestWithPayloadResult <Article>
                {
                    Errors = new Dictionary <string, string> {
                        { "Id", "Such article does not exist" }
                    }
                });
            }

            if (article.AuthorId != userId)
            {
                return(new RequestWithPayloadResult <Article>
                {
                    Errors = new Dictionary <string, string> {
                        { "UserId", "This article is not owned by user with the specified Id." }
                    }
                });
            }

            article.Body        = updatedArticleModel.Body;
            article.Description = updatedArticleModel.Description;
            article.Title       = updatedArticleModel.Title;

            if (updatedArticleModel.FeaturedImage != null)
            {
                article.FeaturedImage = await ImageUtilities.ProcessUploadedFile(updatedArticleModel.FeaturedImage, _imageSettings.FeaturedImage.Location,
                                                                                 _imageSettings.FeaturedImage.Format, _imageSettings.FeaturedImage.Width, _imageSettings.FeaturedImage.Height);
            }

            article.LastUpdatedDate = DateTime.UtcNow;

            var tagsFromDatabase = await _tagService.CreateRangeAsync(updatedArticleModel.Tags.Split(","));

            UpdateArticleTags(article, tagsFromDatabase);

            var savedSuccessfully = await _context.SaveChangesAsync() > 0;

            if (savedSuccessfully)
            {
                return(new RequestWithPayloadResult <Article>
                {
                    Success = true,
                    Payload = article
                });
            }

            return(new RequestWithPayloadResult <Article>()
            {
                Errors = { { "Database", "Couldn't save changes." } }
            });
        }
Exemplo n.º 7
0
        public HttpResponseMessage PostArticle(PostArticleModel dataToPost)
        {
            ArticleEditModel model = new ArticleEditModel(_context, dataToPost);

            model.AddNew();

            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);

            return(response);
        }
Exemplo n.º 8
0
        public ActionResult Edit(long id, ArticleEditModel model)
        {
            if (ModelState.IsValid)
            {
                var result = this.ArticleService.Edit(id, model);
                if (result)
                {
                    return(this.RedirectToIndex());
                }
            }

            ViewBag.Categories = this.DropDownListService.GetCategoriesList();
            return(this.View(model));
        }
Exemplo n.º 9
0
 public async Task AddAsync(ArticleEditModel model)
 {
     await _articleRepository.AddAsync(new Article()
     {
         Id             = model.Id,
         ArticleTitleEn = model.ArticleTitleEn,
         ArticleTitleRu = model.ArticleTitleRu,
         DescriptionRu  = model.DescriptionRu,
         DescriptionEn  = model.DescriptionEn,
         RawHtmlEn      = model.RawHtmlEn,
         RawHtmlRu      = model.RawHtmlRu,
         CreationDate   = model.CreationDate,
         CoverId        = model.CoverId
     });
 }
Exemplo n.º 10
0
        public ActionResult ArticlesDelete([DataSourceRequest]DataSourceRequest request, ArticleEditModel article)
        {
            if (this.ModelState.IsValid)
            {
                var originalArticle = this.articles.GetAll().FirstOrDefault(a => a.Id == article.Id);
                if (originalArticle != null)
                {
                    originalArticle.IsDeleted = true;
                }

                this.articles.Update(originalArticle);
            }

            return this.Json(new[] { article }.ToDataSourceResult(request, this.ModelState));
        }
Exemplo n.º 11
0
        public ActionResult ArticlesCreate([DataSourceRequest]DataSourceRequest request, ArticleEditModel article)
        {
            if (this.ModelState.IsValid)
            {
                var newArticle = new Article
                                     {
                                         CategoryId = int.Parse(article.Category),
                                         Content = article.Content,
                                         Excerpt = article.Excerpt,
                                         PrimaryImageUrl = article.PrimaryImageUrl,
                                         Title = article.Title
                                     };

                this.articles.Create(newArticle);
            }

            return this.Json(new[] { article }.ToDataSourceResult(request, this.ModelState));
        }
Exemplo n.º 12
0
        public async Task <IActionResult> Update([FromForm] ArticleEditModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var result = await _articleService.UpdateAsync(model, User.GetId());

            if (result.Success)
            {
                return(NoContent());
            }

            ModelState.AddModelErrors(result.Errors);

            return(BadRequest(new ValidationResultModel(ModelState)));
        }
Exemplo n.º 13
0
        public async Task <ActionResult> EditArticle(ArticleEditModel model)
        {
            try
            {
                if (string.IsNullOrEmpty(model.RawHtmlEn) ||
                    string.IsNullOrEmpty(model.RawHtmlEn))
                {
                    throw new InvalidOperationException("Не заполнено тело статьи");
                }

                await _articleService.UpdateAsync(model);

                return(GetSuccessResponse());
            }
            catch (Exception exception)
            {
                return(GetErrorResponse(exception));
            }
        }
Exemplo n.º 14
0
        public async Task UpdateAsync(ArticleEditModel model)
        {
            var article = GetArticle(model.Id);

            if (article == null)
            {
                await AddAsync(model);

                return;
            }

            article.ArticleTitleEn = model.ArticleTitleEn;
            article.ArticleTitleRu = model.ArticleTitleRu;
            article.DescriptionEn  = model.DescriptionEn;
            article.DescriptionRu  = model.DescriptionRu;
            article.RawHtmlEn      = model.RawHtmlEn;
            article.RawHtmlRu      = model.RawHtmlRu;
            article.CoverId        = model.CoverId;

            await _articleRepository.UpdateAsync(article);
        }
Exemplo n.º 15
0
        public ActionResult AddArticle(ArticleEditModel input)
        {
            if (this.ModelState.IsValid)
            {
                var newArticle = new Article
                                     {
                                         Title = input.Title,
                                         PrimaryImageUrl = input.PrimaryImageUrl,
                                         Excerpt = input.Excerpt,
                                         Content = input.Content,
                                         CategoryId = input.CategoryId,
                                         AuthorId = this.User.Identity.GetUserId()
                                     };

                this.articles.Create(newArticle);
                this.TempData["Success"] = GlobalConstants.ThankYouMessage;

                return this.Redirect("/");
            }

            return this.View(input);
        }
Exemplo n.º 16
0
        public void SaveArticleEditModel(ArticleEditModel articleEditModel)
        {
            Article EditArticle = mapper.Map <Article>(articleEditModel);

            _articleRepository.SaveArticleChanges(EditArticle);
        }
Exemplo n.º 17
0
        //[ValidateInput(enableValidation:false)]
        public ActionResult Edit(int id)
        {
            ArticleEditModel ArticleEditModel = _articleService.GetArticleEditModelById(id);

            return(View(ArticleEditModel));
        }
Exemplo n.º 18
0
        public ActionResult ArticlesUpdate([DataSourceRequest]DataSourceRequest request, ArticleEditModel article)
        {
            if (this.ModelState.IsValid)
            {
                var originalArticle = this.articles.GetAll().FirstOrDefault(a => a.Id == article.Id);
                if (originalArticle != null)
                {
                    originalArticle.CategoryId = article.CategoryId;
                    originalArticle.Content = article.Content;
                    originalArticle.Excerpt = article.Excerpt;
                    originalArticle.PrimaryImageUrl = article.PrimaryImageUrl;
                    originalArticle.Title = article.Title;
                }

                this.articles.Update(originalArticle);
            }

            return this.Json(new[] { article }.ToDataSourceResult(request, this.ModelState));
        }