/// <inheritdoc /> public ExistingArticleDto CreateArticle(NewArticleDto newArticleDto) { var newArticle = Mapper.Map <Article>(newArticleDto); var createdArticle = ArticleDbAccess.AddArticle(newArticle); return(Mapper.Map <ExistingArticleDto>(createdArticle)); }
public void CreateArticle() { var newArticleDto = new NewArticleDto("Cheese", new ExistingArticleGroupDto(3, "Diary"), true); var articleDbAccessMock = new Mock <IArticleDbAccess>(); var testee = new ArticleAction(articleDbAccessMock.Object, TestMapper.Create()); testee.CreateArticle(newArticleDto); articleDbAccessMock.Verify(x => x.AddArticle(It.Is <Article>(a => a.Name == "Cheese")), Times.Once); }
public void CreateArticle() { using var context = new InMemoryDbContext(); var newArticleDto = new NewArticleDto("Cheese", new ExistingArticleGroupDto(3, "Diary"), true); var articleActionMock = new Mock <IArticleAction>(); var testee = CreateTestee(articleActionMock, context); testee.CreateArticle(newArticleDto); context.Articles.Should().Contain(x => x.Name == "Cheese"); }
public ExistingArticleDto CreateArticle(NewArticleDto newArticleDto) { // TODO mu88: Try to avoid this manual mapping logic var articleGroup = SimpleCrudHelper.Find <ArticleGroup>(newArticleDto.ArticleGroup.ArticleGroupId); var newArticle = new Article { Name = newArticleDto.Name, ArticleGroup = articleGroup, IsInventory = newArticleDto.IsInventory }; var createdArticle = Context.Articles.Add(newArticle); Context.SaveChanges(); return(Mapper.Map <ExistingArticleDto>(createdArticle.Entity)); }
public async Task <string> PostArticle([FromBody] NewArticleDto newArticleDto) { //获取到文章后,首先将文章内容存储到对应路径的硬盘上 //文章内容以test-editormd-markdown-doc=开头,格式为Json字符串 //切割文章内容 string splitString = SplitString.SplitStringWithStart(newArticleDto.ArticleContent); if (splitString.IsNullOrWhiteSpace()) { _logger.LogError("文章内容为空"); return("文章内容为空"); } var result = await FileOperate.StoreToTxtFileAsync(splitString, newArticleDto.ArticleName, _logger); if (result != "") { //不为空,表示存储成功,返回了存储路径 newArticleDto.ArticleUrl = result; //AutoMapper映射 Article newArticle = newArticleDto.MapTo <Article>(); //默认浏览次数为1 newArticle.PageView = 1; //存储数据库 try { //存入数据库,并获取文章Id var ArticleId = await _articleRepository.InsertAndGetIdAsync(newArticle); //将该文章Id和RedisArticle组合,作为Redis键名 string redisArticleKey = PostConsts.ArticleBaseKey + ArticleId; //存入Redis缓存 await _cacheManager.GetCache(PostConsts.RedisForArticleStore).SetAsync(redisArticleKey, newArticle); return(ArticleId.ToString()); } catch (Exception ex) { _logger.LogError(exception: ex, "错误信息:" + ex.Message); return("发布文章出现异常"); } } else { _logger.LogError("存储文章到硬盘时出现错误"); return("发布文章失败"); } }