コード例 #1
0
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var listing = await _context.Listings
                          .Include(l => l.Subcategory)
                          .Include(l => l.User)
                          .Include(l => l.Subcategory.Category)
                          .Include(l => l.Comments)
                          .ThenInclude((Comments c) => c.User)
                          .FirstOrDefaultAsync(m => m.Id == id);

            if (listing == null)
            {
                return(NotFound());
            }

            var listingAndComment = new ListingAndComment {
                Listing = listing, Comment = new Comments()
            };

            return(View(listingAndComment));
        }
コード例 #2
0
        public async Task <IActionResult> CreateAjax(int id, ListingAndComment listingAndComment)
        {
            if (ModelState.IsValid)
            {
                var comment = listingAndComment.Comment;
                comment.Listingid = id;
                comment.Userid    = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
                comment.Date      = DateTime.Now;

                _context.Add(comment);
                await _context.SaveChangesAsync();

                return(Ok());
            }

            // turetu sito nepasiekti
            return(RedirectToAction("Index", "Home"));
        }
コード例 #3
0
        public async Task CreateAjax_Comment_ShouldCreateComment()
        {
            // Arrange
            var commentController = this.CreateCommentController(true);
            int id;

            //create new listing for comment
            Listings listing = new Listings()
            {
                Name        = "good Car",
                Description = "easy free car :(",
                Userid      = fakeUser.Id
            };

            mockadvert_siteContext.Listings.Add(listing);
            mockadvert_siteContext.SaveChanges();

            //setup data
            id = listing.Id;
            ListingAndComment listingAndComment = new ListingAndComment()
            {
                Comment = new Comments()
                {
                    Text = "GREAT CAR"
                },
                Listing = listing
            };

            // Act
            var result = (OkResult)await commentController.CreateAjax(id, listingAndComment);

            var addedComment = await mockadvert_siteContext.Comments.FirstOrDefaultAsync(c => c.Id == listingAndComment.Comment.Id);


            // Assert
            Assert.NotNull(addedComment);
            Assert.Equal(listingAndComment.Comment.Text, addedComment.Text);
            Assert.Equal(fakeUser.Id, addedComment.Userid);
            Assert.Equal(200, result.StatusCode);
        }
コード例 #4
0
        public async Task CreateAsync_InvalidModelState_RedirectToMainPage()
        {
            // Arrange
            var commentController = this.CreateCommentController(true);

            commentController.ModelState.AddModelError("key", "message");

            int id;

            //create new listing for comment
            Listings listing = new Listings()
            {
                Name        = "good Car",
                Description = "easy free car :(",
                Userid      = fakeUser.Id
            };

            mockadvert_siteContext.Listings.Add(listing);
            mockadvert_siteContext.SaveChanges();

            //setup data
            id = listing.Id;
            ListingAndComment listingAndComment = new ListingAndComment()
            {
                Comment = new Comments()
                {
                    Text = "GREAT CAR"
                },
                Listing = listing
            };

            var result = await commentController.CreateAjax(id, listingAndComment);

            var resultView = (RedirectToActionResult)result;

            Assert.IsType <RedirectToActionResult>(result);
            Assert.Equal("Index", resultView.ActionName);
            Assert.Equal("Home", resultView.ControllerName);
        }