Exemplo n.º 1
0
        static void Main(string[] args)
        {
            // Article
            var AddArticle = ArticleService.AddArticle(new Article {
                AuthorId = 1, CategoryId = 1, IsActive = true, Like = 1, Title = "TestRequest", CreatedDate = new DateTime(2019, 10, 10), Content = " - ", Description = " - ",
            });

            var UpdateArticle = ArticleService.UpdateArticle(new Article {
                Id = 1, AuthorId = 1, CategoryId = 1, IsActive = true, Like = 1, Title = "TestRequestUpdate", CreatedDate = new DateTime(2019, 10, 10), Content = " - ", Description = " - ", UpdatedDate = new DateTime(2019, 10, 11)
            });

            var FindArticle = ArticleService.FindArticle(new BaseRequest {
                Value = "Test"
            });

            var GetListOfCategory = ArticleService.GetListOfCategory(new BaseRequest {
                Id = 1
            });

            //Category
            var AddCategory = CategoryService.AddCategory(new Category {
                Name = "Yazılım", IsActive = true, CreatedDate = new DateTime(2019, 10, 10)
            });

            var UpdateCategory = CategoryService.UpdateCategory(new Category {
                Id = 2, Name = "Yazılım", IsActive = true, CreatedDate = new DateTime(2019, 10, 10), UpdatedDate = new DateTime(2019, 10, 11),
            });
        }
Exemplo n.º 2
0
        public void UpdateArticle_Test()
        {
            var dbArticles = new List <Article>();

            this._sut = new ArticleService(_fakeArticleUnitOfWork.Object, _mapper);

            CreateArticleDTO article = FakeArticles.CreateArticleWithTwoTagsDto();

            _fakeArticleRepository.Setup(m => m.Add(It.IsAny <Article>()))
            .Callback((Article a) => { dbArticles.Add(a); }).Verifiable();

            _fakeArticleUnitOfWork.Setup(setup => setup.Commit()).Returns(1).Verifiable();

            _fakeArticleRepository.Setup(setup => setup.Update(It.IsAny <Article>())).Callback((Article a) =>
            {
                dbArticles.Remove(a);
                dbArticles.Add(a);
            });

            _sut.AddArticle(article);

            GetArticleDTO getArticleDto = FakeArticles.CreateGetArticle();

            _sut.UpdateArticle(getArticleDto);

            Assert.AreEqual(dbArticles[0].Header, getArticleDto.Header);
        }
Exemplo n.º 3
0
        public IActionResult AddArticle([FromForm] AddArticleViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var authorExists = authorService.VerifyIfAuthorExistsByEmail(model.Email);

            if (authorExists)
            {
                MatchCollection extractedHashtags = tagService.FilterHashtags(model.Tags);

                tagService.AddTags(extractedHashtags);

                string imageUploadDirectory = Path.Combine(webHostEnvironment.WebRootPath, "ArticleImages");

                articleService.AddArticle(model.Title, model.Content, model.Email, model.Image, imageUploadDirectory);

                var recentlyCreatedArticle = articleService.GetNewestAddedArticle(model.Title, model.Content, model.Email);

                foreach (var hashtag in extractedHashtags)
                {
                    var tag = tagService.GetTagByName(hashtag.ToString());

                    articleTagsService.Add(tag, recentlyCreatedArticle);
                }
            }

            var newestArticle = articleService.GetNewestAddedArticle(model.Title, model.Content, model.Email);

            return(Redirect(Url.Action("ViewArticle", "Article", new { id = newestArticle.Id })));
        }
        public async Task <ActionResult <Article> > Create(Article article)
        {
            if (article.UserName != null)
            {
                userService.RegisterUser(article.UserName);
                articleService.AddArticle(article);
            }

            return(CreatedAtAction(nameof(GetById), new { id = article.Id }, article));
        }
Exemplo n.º 5
0
        public async Task <ActionResult <Article> > Create(Article article)
        {
            if (article.UserName != null)
            {
                this.userService.RegisterUserByName(article.UserName);

                articleService.AddArticle(article);
            }

            var check = CreatedAtAction(nameof(GetById), new { id = article.Id }, article);

            return(check);
        }
Exemplo n.º 6
0
        [ValidateInput(false)]//可以提交富文本
        public ActionResult Add(Articles art)
        {
            int count = ArticleService.AddArticle(art);

            if (count >= 1)
            {
                return(Json(new UIResult(true, "新增成功")));
            }
            else
            {
                return(Json(new UIResult(false, "新增失败")));
            }
        }
        public ActionResult Save(EditArticleViewModel viewModel)
        {
            if (viewModel.Content.Id.HasValue)
            {
                ArticleService.EditArticle(viewModel.Content, viewModel.Tags);
            }
            else
            {
                viewModel.Content.AuthorId = User.UserId;
                ArticleService.AddArticle(viewModel.Content, viewModel.Tags);
            }

            return(Json(new ProcessResult(new { ContentId = viewModel.Content.Id })));
        }
Exemplo n.º 8
0
        public void ThrowArgumentNullExceptionWhenPlaceIsNull()
        {
            // Arrange
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var mockedRepository = new Mock <IEfGenericRepository <Article> >();

            var articleService = new ArticleService(mockedRepository.Object, mockedUnitOfWork.Object);

            // Act
            var message = Assert.Throws <ArgumentNullException>(() => articleService.AddArticle(null));

            // Assert
            StringAssert.IsMatch("article", message.ParamName);
        }
Exemplo n.º 9
0
        public void CallUnitOfWorkMethodCommitOnce()
        {
            // Arrange
            var mockedUnitOfWork = new Mock <IUnitOfWork>();
            var mockedRepository = new Mock <IEfGenericRepository <Article> >();

            var articleService = new ArticleService(mockedRepository.Object, mockedUnitOfWork.Object);
            var mockedArticle  = new Mock <Article>();

            // Act
            articleService.AddArticle(mockedArticle.Object);

            // Assert
            mockedUnitOfWork.Verify(x => x.Commit(), Times.Once);
        }
Exemplo n.º 10
0
        public HttpResponseMessage AddArticle(HttpRequestMessage request, ArticleViewModel articleVM)
        {
            HttpResponseMessage response = null;

            if (!ModelState.IsValid)
            {
                response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
            }
            else
            {
                bool checkData = articleService.AddArticle(articleVM);
                response = request.CreateResponse(HttpStatusCode.Created, checkData);
            }

            return(response);
        }
Exemplo n.º 11
0
 public ResultModel Add([FromBody] Article request)
 {
   if (request != null)
   {
     try
     {
       ResultModel result = ArticleService.AddArticle(request);
       return result;
     }
     catch (Exception ex)
     {
       return new ResultModel { Data = null, Status = ResultStatus.ServerInternalError, Message = "Hata oluştu" };
     }
   }
   else
     return new ResultModel { Data = null, Status = ResultStatus.BadRequest, Message = "Geçersiz değer" };
 }
Exemplo n.º 12
0
        public void DeleteArticle_Test()
        {
            var dbArticles = new List <Article>();

            CreateArticleDTO article = FakeArticles.CreateArticleWithTwoTagsDto();

            _fakeArticleUnitOfWork.Setup(setup => setup.Commit()).Returns(1).Verifiable();

            _fakeArticleRepository.Setup(setup => setup.GetById(2)).Returns(FakeArticles.CreateArticle());

            _fakeArticleRepository.Setup(setup => setup.Delete(It.IsAny <Article>())).Callback((Article a) =>
                                                                                               { dbArticles.Remove(a); });

            _sut.AddArticle(article);

            _sut.DeleteArticle(article.Id);

            Assert.IsTrue(!dbArticles.Any());
        }
Exemplo n.º 13
0
 public ActionResult <BaseResponseModel> PostAddArticle([FromForm] ArticleInsertModel articleInsertModel)
 {
     try
     {
         var result = articleService.AddArticle(articleInsertModel);
         if (result)
         {
             return(new SuccessResponseModel <bool>(true));
         }
         else
         {
             return(new BaseResponseModel(ReadOnlyValues.ArticleNotAdd));
         }
     }
     catch (Exception ex)
     {
         return(new BaseResponseModel(ex.Message));
     }
 }
Exemplo n.º 14
0
        public void AddArticleWithoutTags_Test()
        {
            this._sut = new ArticleService(_fakeArticleUnitOfWork.Object, _mapper);

            var article = FakeArticles.CreateArticleWithTwoTagsDto();

            List <Article> dbArticles = new List <Article>();

            _fakeArticleRepository.Setup(m => m.Add(It.IsAny <Article>()))
            .Callback((Article a) => { dbArticles.Add(a); }).Verifiable();

            _fakeTagRepository.Setup(s => s.GetAll()).Returns(Array.Empty <Tag>()).Verifiable();

            _fakeArticleUnitOfWork.Setup(setup => setup.Commit()).Returns(1);

            _sut.AddArticle(article);

            Assert.NotZero(dbArticles.Count);

            Assert.AreEqual(2, article.Tags.Count);
        }
Exemplo n.º 15
0
        public void AddArticleIfTagsExists_Test()
        {
            var dbArticles = new List <Article>();
            var dbTags     = new List <Tag>();
            var tags       = FakeArticles.CreateTags();

            dbTags.AddRange(tags);

            this._sut = new ArticleService(_fakeArticleUnitOfWork.Object, _mapper);

            _fakeArticleRepository.Setup(m => m.Add(It.IsAny <Article>()))
            .Callback((Article a) => { dbArticles.Add(a); }).Verifiable();

            _fakeArticleUnitOfWork.Setup(setup => setup.Commit()).Returns(1).Verifiable();

            _sut.AddArticle(FakeArticles.CreateArticleWithTwoTagsDto());

            Assert.AreEqual(2, dbArticles[0].Tags.Count);

            Assert.AreSame(dbArticles[0].Tags.ToList()[0], tags[0]);
            Assert.AreSame(dbArticles[0].Tags.ToList()[1], tags[1]);
        }
Exemplo n.º 16
0
        public JsonResult Add(string Title = "", string Author = "", string TContent = "", string TypeIds = "", string TagIds = "", int GroupType = 1, int Recommend = 0, int HitCount = 9, int Sort = 9, int Status = 0, string DisplayPic = "", string SeoKeywords = "", string Seodescription = "")
        {
            AjaxResponse <object> obj = new AjaxResponse <object>();

            #region 一系列验证
            if (string.IsNullOrEmpty(Title))
            {
                obj.ErrorMessage = "文章标题不能为空!";
                return(Json(obj));
            }
            if (string.IsNullOrEmpty(Author))
            {
                obj.ErrorMessage = "作者不能为空!";
                return(Json(obj));
            }
            if (string.IsNullOrEmpty(TContent))
            {
                obj.ErrorMessage = "文章内容不能为空!";
                return(Json(obj));
            }
            if (string.IsNullOrEmpty(TypeIds))
            {
                obj.ErrorMessage = "请好好选择文章分类!";
                return(Json(obj));
            }
            if (string.IsNullOrEmpty(TagIds))
            {
                obj.ErrorMessage = "请好好选择文章Tag!";
                return(Json(obj));
            }
            if (string.IsNullOrEmpty(SeoKeywords))
            {
                obj.ErrorMessage = "Seo关键词不能为空!";
                return(Json(obj));
            }
            if (string.IsNullOrEmpty(Seodescription))
            {
                obj.ErrorMessage = "Seo头部描述能为空!";
                return(Json(obj));
            }
            if (Title.Length > 50)
            {
                obj.ErrorMessage = "新闻标题50个字以内!";
                return(Json(obj));
            }
            if (Author.Length > 15)
            {
                obj.ErrorMessage = "作者15个字以内!";
                return(Json(obj));
            }
            if (SeoKeywords.Length > 149)
            {
                obj.ErrorMessage = "Seo头关键词149字以内!";
                return(Json(obj));
            }
            if (Seodescription.Length > 249)
            {
                obj.ErrorMessage = "Seo头部描述249字以内!";
                return(Json(obj));
            }
            if (TagIds.Length > 100)
            {
                obj.ErrorMessage = "文章Tag好像有点太多了啊!";
                return(Json(obj));
            }
            if (TypeIds.Length > 100)
            {
                obj.ErrorMessage = "文章分类好像有点那个啥吧!";
                return(Json(obj));
            }
            #endregion

            SeoTKD seoInfo = new SeoTKD()
            {
                SeoKeywords   = SeoKeywords,
                Sedescription = Seodescription,
                Status        = StatusEnum.Normal
            };

            #region 存储前的处理
            TypeIds = TypeIds.Trim(',');
            TagIds  = TagIds.Trim(',');

            //传过来的已经url编码了【带敏感字符的url服务器默认是拒绝请求的】
            //没有url编码的,解码还是他本身
            TContent = HttpUtility.UrlDecode(TContent);
            //必须保证存在数据库里面的文字是安全的
            TContent = HttpUtility.HtmlEncode(TContent);

            //如果没有上传默认展图,就随机展示一个默认展图
            if (string.IsNullOrEmpty(DisplayPic))
            {
                IList <ArticleDisPhoto> disPics = ArticleDisPhotoService.PageLoad(p => p.Status != StatusEnum.Delete).ToList();
                int count = disPics.Count;
                if (count > 0)
                {
                    Random random = new Random();
                    int    index  = random.Next(disPics.Count);
                    DisplayPic = disPics[index].PicUrl;
                }
                else//实在没有的话就给一个默认值
                {
                    DisplayPic = LoT.Common.ConfigHelper.GetValueForConfigAppKey("ArticleTypeDisPlayPic");
                }
            }
            #endregion

            Article article = new Article()
            {
                Title      = Title,
                Author     = Author,
                TContent   = TContent,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
                TypeIds    = TypeIds.Trim(','),
                GroupType  = (GroupEnum)(GroupType == 0 ? 1 : GroupType),
                Recommend  = (RecommendEnum)Recommend,
                HitCount   = HitCount,
                Sort       = Sort,
                TagIds     = TagIds,
                Status     = (ArticleStatusEnum)Status,
                DisplayPic = DisplayPic
            };

            int i = ArticleService.AddArticle(article, seoInfo);
            obj.IsSuccess = i > 0;
            if (i <= 0)
            {
                obj.ErrorMessage = "未知原因添加失败~";
            }
            return(Json(obj));
        }
Exemplo n.º 17
0
 /// <summary>
 /// 添加新文章
 /// </summary>
 /// <param name="article"></param>
 /// <returns></returns>
 public static Article AddArticle(Article article)
 {
     return(ArticleService.AddArticle(article));
 }
Exemplo n.º 18
0
        public IActionResult Post([FromBody] ArticleModel Article)
        {
            var created = ArticleService.AddArticle(Mapper.Map <MakeYourJournal.DAL.Entities.Article>(Article));

            return(Ok(Mapper.Map <ArticleModel>(created)));
        }
Exemplo n.º 19
0
 public async Task <ActionResult <Article> > Create(Article article)
 {
     articleService.AddArticle(article);
     return(CreatedAtAction(nameof(GetById), new { id = article.Id }, article));
 }
Exemplo n.º 20
0
 public ArticleDetailModel CreateArticle([FromBody] ArticleCreateModel article)
 {
     return(_articleService.AddArticle(article));
 }