/// <summary> /// 创建文章 /// </summary> /// <param name="title">标题</param> /// <param name="content">内容</param> /// <param name="categoryIds">所属栏目</param> /// <param name="userId">创建用户</param> /// <returns></returns> public async Task CreateArticle(string title, string content, Guid[] categoryIds, Guid userId) { using (var articleService = new ArticleService()) { var article = new Article() { Title = title, Content = content, UserId = userId }; await articleService.CreateAsync(article); Guid articleId = article.Id; using (var articleToCategoryService = new ArticleToCategoryService()) { //遍历栏目 foreach (var categoryId in categoryIds) { await articleToCategoryService.CreateAsync(new ArticleToCategory() { ArticleId = articleId, CategoryId = categoryId }, false); } await articleToCategoryService.Save(); } } }
public async Task CreateArticle(string title, string content, Guid userId, Guid[] categoryIds) { using (var articleSvc = new ArticleService()) { Article article = new Article() { Title = title, Content = content, UserId = userId }; await articleSvc.CreateAsync(article); Guid id = article.Id; using (var articleTocategory = new ArticleToCategoryService()) { foreach (var item in categoryIds) { await articleTocategory.CreateAsync(new ArticleToCategory() { ArticleCategoryId = item, ArticleId = id }, autoSave : false); } await articleTocategory.Save(); } } }
public async Task <IActionResult> Create(ArticleModel model) { await _articleService.CreateAsync(model.CategorySlug, model.Date !.Value.Date, model.Headline, model.Description, model.Excerpt, model.EventDate, model.Image?.Url, model.Image?.Width, model.Image?.Height); return(Ok()); }
public async Task register_async_should_invoke_add_async_on_repository() { var ArticleRepositoryMock = new Mock <IArticleRepository>(); var mapperMock = new Mock <IMapper>(); var userService = new ArticleService(ArticleRepositoryMock.Object, mapperMock.Object); await userService.CreateAsync("articel about IT", "Bla bla bla bal", "Juzio"); ArticleRepositoryMock.Verify(x => x.AddAsync(It.IsAny <Article>()), Times.Once); }
public async Task <IActionResult> PostAsync([FromBody] Article article) { var newArticle = await _articleService.CreateAsync(article); if (newArticle == null) { return(BadRequest()); } var uri = new Uri("/articles/" + article.Id, UriKind.Relative); return(Created(uri, article)); }
public async Task CreateAsync_AlreadyExists() { var service = new ArticleService(_unitOfWorkMock.Object); var article = new ArticleDto { ID = 0, Barcode = "AAA" }; var result = await service.CreateAsync(article, 1); Assert.IsNotNull(result); Assert.AreEqual(result.Status, ServiceResponseStatus.AlreadyExists); Assert.AreEqual(result.Result, 0); }
public async Task CreateAsync_Success() { var service = new ArticleService(_unitOfWorkMock.Object); var article = new ArticleDto { ID = 4, Barcode = "DDD", Recommendations = new List <RecommendationDto>(), Attachments = new List <AttachmentDto>() }; var result = await service.CreateAsync(article, 1); Assert.IsNotNull(result); Assert.AreEqual(result.Status, ServiceResponseStatus.Success); Assert.AreEqual(result.Result, 4); }
public async Task CreateMethodShouldAddCorrectNewArticleToDb() { var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var dbContext = new ApplicationDbContext(optionsBuilder.Options); var articleService = new ArticleService(dbContext, null); var articleToAdd = new AddArticleInputModel { Content = "testContent", Title = "testTitle", }; await articleService.CreateAsync(articleToAdd, "Icaka99"); Assert.NotNull(dbContext.Articles.FirstOrDefaultAsync()); Assert.Equal("testContent", dbContext.Articles.FirstAsync().Result.Content); Assert.Equal("testTitle", dbContext.Articles.FirstAsync().Result.Title); Assert.Equal("Icaka99", dbContext.Articles.FirstAsync().Result.UserId); }
public async Task CreateShouldAddArticleToDatabaseSuccessfully() { this.SetupSqlite(); await this.SeedDatabase(); using var context = new ApplicationDbContext(this.ContextOptions); var repository = new EfDeletableEntityRepository <Article>(context); var articleService = new ArticleService(repository); var articleToAdd = new ArticleServiceModel { Title = "Test", Content = "Test", ImageUrl = "Test", UserId = "1", }; var articleId = await articleService.CreateAsync(articleToAdd); Assert.True(articleId != 0); }
public async Task CreateCreatesSuccesssully() { //Arrange var dbOptions = new DbContextOptionsBuilder <UndergroundStationDbContext>() .UseInMemoryDatabase("UndergroundStationTestDb") .Options; var db = new UndergroundStationDbContext(dbOptions); var articleService = new ArticleService(db); await db.SaveChangesAsync(); //Act var success = await articleService .CreateAsync("Title", "Content", "1", 2, DateTime.UtcNow, 1); //Assert success .Should() .Equals(true); }