コード例 #1
0
        public void _Call_CategoryService_GetCategoryByName(
            string userId,
            string title,
            string text,
            string coverPicture,
            CategoryEnum categoryEnum)
        {
            //Arrange
            var mockedNewsRepository   = new Mock <IRepository <News> >();
            var mockedUserService      = new Mock <IUserService>();
            var mockedUnitOfWork       = new Mock <IUnitOfWork>();
            var mockedNewsFactory      = new Mock <INewsFactory>();
            var mockedCategoryService  = new Mock <ICategoryService>();
            var mockedDateTimeProvider = new Mock <IDateTimeProvider>();

            var newsService = new NewsService(
                mockedNewsRepository.Object,
                mockedUserService.Object,
                mockedUnitOfWork.Object,
                mockedNewsFactory.Object,
                mockedCategoryService.Object,
                mockedDateTimeProvider.Object);

            //Act
            newsService.CreateNews(userId, title, text, coverPicture, categoryEnum);

            //Assert
            mockedCategoryService.Verify(c => c.GetCategoryByName(categoryEnum), Times.Once);
        }
コード例 #2
0
        public IActionResult CreateNews([FromBody] NewsItemInputModel body)
        {
            if (!ModelState.IsValid)
            {
                throw new ModelFormatException("Model not properly formatted");
            }
            var entity = _newsService.CreateNews(body);

            return(CreatedAtRoute("GetNewsItemById", new { newsItemId = entity.Id }, null));
        }
コード例 #3
0
        public IActionResult CreateNews([FromBody] NewsItemInputModel news)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Model is not properly formatted."));
            }
            var entity = _newsService.CreateNews(news);

            return(CreatedAtAction("GetNewsById", new { id = entity.Id }, null));
        }
コード例 #4
0
        public void _Call_ReturnCorrect_News_Commit(
            string userId,
            string title,
            string text,
            string coverPicture,
            CategoryEnum categoryEnum)
        {
            //Arrange
            var user     = new User();
            var date     = new DateTime();
            var category = new Category();
            var news     = new News();

            var mockedNewsRepository = new Mock <IRepository <News> >();

            var mockedUserService = new Mock <IUserService>();

            mockedUserService.Setup(m => m.GetUserById(It.IsAny <string>())).Returns(user);

            var mockedUnitOfWork = new Mock <IUnitOfWork>();

            var mockedNewsFactory = new Mock <INewsFactory>();

            mockedNewsFactory.Setup(f => f.CreateNews(title, text, coverPicture, user, category, date)).Returns(news);

            var mockedCategoryService = new Mock <ICategoryService>();

            mockedCategoryService.Setup(c => c.GetCategoryByName(categoryEnum)).Returns(category);

            var mockedDateTimeProvider = new Mock <IDateTimeProvider>();

            mockedDateTimeProvider.Setup(d => d.GetCurrentDate()).Returns(date);

            var newsService = new NewsService(
                mockedNewsRepository.Object,
                mockedUserService.Object,
                mockedUnitOfWork.Object,
                mockedNewsFactory.Object,
                mockedCategoryService.Object,
                mockedDateTimeProvider.Object);

            //Act
            var newsRes = newsService.CreateNews(userId, title, text, coverPicture, categoryEnum);

            //Assert
            Assert.AreSame(news, newsRes);
        }
コード例 #5
0
        public void CreateNewsSaveNewsInDb()
        {
            var options = new DbContextOptionsBuilder <DealershipDbContext>()
                          .UseInMemoryDatabase(databaseName: "Create_News")
                          .Options;

            var db = new DealershipDbContext(options);

            var service = new NewsService(db);

            var inputModel = this.GetNewsCreateInputModel();

            var news = service.CreateNews(inputModel, NewsAuthorId);

            Assert.True(db.News.Count() == 1);
            Assert.True(db.News.First().Title == NewsTitle);
        }
コード例 #6
0
 public ActionResult CreateNews(News news)
 {
     if (IsAdmin() != true)
     {
         return(RedirectToAction("Index", "Dashboard"));
     }
     try
     {
         news.DateAndTime = DateTime.Now.ToString();
         _newsService.CreateNews(news);
         return(RedirectToAction("News"));
     }
     catch
     {
         return(View());
     }
 }
コード例 #7
0
ファイル: NewsController.cs プロジェクト: hedinnh/Radapi
        public IActionResult CreateNews([FromBody] NewsItemInputModel newsItemInput)
        {
            string authHeader = Request.Headers["Authorization"];

            if (secretKey != authHeader)
            {
                return(Unauthorized());
            }
            ;
            if (!ModelState.IsValid)
            {
                return(StatusCode(412, newsItemInput));
            }

            var id = _newsService.CreateNews(newsItemInput);

            return(CreatedAtRoute("GetNewsItemById", new { id }, null));
        }
コード例 #8
0
        public IHttpActionResult Create(CreateNewsDTO dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var instance = Mapper.Map <CreateNewsDTO, News>(dto);

            instance.AddTime = timeService.GetLocalDateTime(LocalDateTimeService.CHINA_STANDARD_TIME);
            var result = newsService.CreateNews(instance);

            if (!result.Success)
            {
                return(BadRequest(result.Message.ToString()));
            }
            return(Created(new Uri(Request.RequestUri + "/" + instance.Id), instance));
        }
コード例 #9
0
        public ActionResult CreateNews(CreateNewsViewModel model)
        {
            if (!ModelState.IsValid)
            {
                TempData["AddNewsStatus"] = "alert('標題長度限制為25字以內');";
            }
            else
            {
                var instance = Mapper.Map <CreateNewsViewModel, News>(model);
                var result   = newsService.CreateNews(instance);

                if (!result.Success)
                {
                    TempData["AddNewsStatus"] = "alert('新增失敗, 請稍後再試');";
                }
            }
            return(RedirectToAction("Index"));
        }
コード例 #10
0
        public void CreateCommentWorksCorrectly()
        {
            var options = new DbContextOptionsBuilder <DealershipDbContext>()
                          .UseInMemoryDatabase(databaseName: "Create_Comment")
                          .Options;

            var db = new DealershipDbContext(options);

            var newsService     = new NewsService(db);
            var commentsService = new CommentsService(db);

            var news = newsService.CreateNews(GetNewsCreateInputModel(), AuthorId);

            var comment = commentsService.Create(AuthorId, news.Id, Content);

            Assert.True(db.News.First().Comments.Count() == 1);
            Assert.True(db.Comments.Count() == 1);
            Assert.True(db.Comments.First().Id == comment.Id);
        }
コード例 #11
0
        public void GetAllNewsWorksCorrectly()
        {
            var options = new DbContextOptionsBuilder <DealershipDbContext>()
                          .UseInMemoryDatabase(databaseName: "Get_All_News")
                          .Options;

            var db = new DealershipDbContext(options);

            var service = new NewsService(db);

            var inputModel = this.GetNewsCreateInputModel();

            var news = service.CreateNews(inputModel, NewsAuthorId);

            var wantedNews = service.GetAllNews();

            Assert.True(wantedNews.Count() == 1);
            Assert.True(wantedNews.First().Id == news.Id);
        }
コード例 #12
0
        public void DeleteNewsRemovesTheCorrectNewsFromDb()
        {
            var options = new DbContextOptionsBuilder <DealershipDbContext>()
                          .UseInMemoryDatabase(databaseName: "Delete_News")
                          .Options;

            var db = new DealershipDbContext(options);

            var service = new NewsService(db);

            var inputModel = this.GetNewsCreateInputModel();

            var news = service.CreateNews(inputModel, NewsAuthorId);

            Assert.True(db.News.Count() == 1);

            service.DeleteNews(news.Id);

            Assert.True(db.News.Count() == 0);
        }
コード例 #13
0
        public void EditCommentWorksCorrectly()
        {
            var options = new DbContextOptionsBuilder <DealershipDbContext>()
                          .UseInMemoryDatabase(databaseName: "Edit_Comment")
                          .Options;

            var db = new DealershipDbContext(options);

            var commentsService = new CommentsService(db);
            var newsService     = new NewsService(db);

            var news    = newsService.CreateNews(GetNewsCreateInputModel(), AuthorId);
            var comment = commentsService.Create(AuthorId, news.Id, Content);

            Assert.True(comment.Content == Content);

            commentsService.EditComment(comment.Id, "Changed Content!");

            Assert.True(comment.Content == "Changed Content!");
        }
コード例 #14
0
ファイル: UnitTest1.cs プロジェクト: ghostdog87/WoWJunkyard
        public async void TestCreateNews_TestWithCorrectData_ShouldReturnCorrectResult()
        {
            var options = new DbContextOptionsBuilder <WoWDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var context = new WoWDbContext(options);

            var newsService = new NewsService(context);


            var actualData = await newsService.CreateNews(new News()
            {
                Description = "<b>testing description</b>",
                Image       = "\\img\\news\\1.jpg",
                PostedOn    = new DateTime(),
                Title       = "goshko"
            });

            Assert.True(actualData == 1);
        }
コード例 #15
0
        public void DeleteCommentDeletesTheWriteComment()
        {
            var options = new DbContextOptionsBuilder <DealershipDbContext>()
                          .UseInMemoryDatabase(databaseName: "Delete_Comment")
                          .Options;

            var db = new DealershipDbContext(options);

            var newsService     = new NewsService(db);
            var commentsService = new CommentsService(db);

            var news = newsService.CreateNews(GetNewsCreateInputModel(), AuthorId);

            Assert.True(news.Comments.Count() == 0);

            var comment = commentsService.Create(AuthorId, news.Id, Content);

            Assert.True(news.Comments.Count() == 1);

            commentsService.DeleteComment(comment.Id);

            Assert.True(news.Comments.Count() == 0);
        }
コード例 #16
0
 public ActionResult <IdDto> CreateNews([FromBody] CreateNewsDto createUserDto)
 {
     return(new OkObjectResult(new { Id = _newsService.CreateNews(createUserDto) }));
 }
コード例 #17
0
 public NewsDTO CreateNews([FromBody] NewsDTO news, int userId)
 {
     service = Service.NewsService;
     return(service.CreateNews(news, userId));
 }