Exemple #1
0
        public async Task GetPostByIdShouldReturnModel()
        {
            // Arange
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            //Act
            var db     = new ApplicationDbContext(options);
            var config = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile <ApplicationProfile>();
            });
            var mapper      = new Mapper(config);
            var postService = new PostService(db, mapper);
            var user        = new ApplicationUser
            {
                UserName = "******",
                Email    = "*****@*****.**"
            };
            await db.Users.AddAsync(user);

            var model = new PostInputViewModel
            {
                CreatedOn   = DateTime.UtcNow,
                Description = "d1",
                ImageUrl    = "URL",
                Title       = "some title"
            };
            var id = await postService.AddPostAsync(model, user.Id);

            var modelOutput = postService.GetPostById(id);

            //assert
            Assert.Equal(modelOutput.Id, id);
        }
Exemple #2
0
        public async Task <IActionResult> AddPost([FromBody] AddPostViewModel viewModel)
        {
            bool success = await _postService.AddPostAsync(viewModel.TopicId, viewModel.Message);

            return(Json(new
            {
                success,
            }));
        }
Exemple #3
0
        public async Task RemoveByIdShouldWorkCorrectlyIfIdIsValid()
        {
            // Arange
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            //Act
            var db     = new ApplicationDbContext(options);
            var config = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile <ApplicationProfile>();
            });
            var mapper      = new Mapper(config);
            var postService = new PostService(db, mapper);
            var user        = new ApplicationUser
            {
                UserName = "******",
                Email    = "*****@*****.**"
            };
            await db.Users.AddAsync(user);

            var model = new PostInputViewModel
            {
                CreatedOn   = DateTime.UtcNow,
                Description = "d1",
                ImageUrl    = "URL",
                Title       = "some title"
            };
            var id = await postService.AddPostAsync(model, user.Id);

            await postService.RemovePostById(id);

            var post = await db.Posts.FirstOrDefaultAsync();

            //assert
            Assert.True(post.IsDeleted);
        }
Exemple #4
0
 public async Task AddPostAsync(string token, Post postViewModel)
 {
     var postRepository = new PostRepository(serializeService, token);
     var postService    = new PostService(postRepository);
     await postService.AddPostAsync(postViewModel);
 }
Exemple #5
0
        public async Task <IActionResult> Add(PostDto post)
        {
            await _postService.AddPostAsync(post);

            return(Created("Add", post));
        }