public void Post_ShouldReturnSuccess()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            NewsRelease post = TestData.CreateDbPost();

            context.NewsRelease.Add(post);
            context.SaveChanges();

            var releaseLogToCreate = new Models.PostLog
            {
                Description = "toto",
                DateTime    = DateTime.Now,
                PostKey     = post.Key
            };

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            var result = controller.AddPostLog(releaseLogToCreate) as ObjectResult;

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            result.Should().BeOfType <CreatedAtRouteResult>("because the create operation should go smoothly");
            result.StatusCode.Should().Be(201, "because HTTP Status 201 should be returned upon creation of new entry");
            var model = result.Value as Models.PostLog;

            model.Description.Should().Be("toto");
        }
示例#2
0
 public IActionResult AddPostLog([FromBody] Models.PostLog logEntry)
 {
     try
     {
         var dbPost    = dbContext.NewsRelease.Include(p => p.NewsReleaseLog).FirstOrDefault(p => p.Key == logEntry.PostKey);
         var dbPostLog = new NewsReleaseLog {
             DateTime = DateTimeOffset.Now
         };
         dbContext.Entry(dbPostLog).CurrentValues.SetValues(logEntry);
         dbPost.NewsReleaseLog.Add(dbPostLog);
         dbContext.SaveChanges();
         return(CreatedAtRoute("GetPostLogs", new { key = logEntry.PostKey }, mapper.Map <Models.PostLog>(dbPostLog)));
     }
     catch (Exception ex)
     {
         return(BadRequest("Failed to save a new post log entry", ex));
     }
 }