public IActionResult AddUserMinistryPreference([FromQuery] string[] ministryKeys)
        {
            try
            {
                var header = Request.Headers["Authorization"];
                var email  = GetEmailAddressFromAuthorizationHeader(Request.Headers["Authorization"]);
                var dbUserMinistryPrefs = dbContext.UserMinistryPreference.Include(m => m.Ministry).Where(p => p.Email == email).ToList();
                dbContext.RemoveRange(dbUserMinistryPrefs);

                var userPrefs = new List <UserMinistryPreference>();
                foreach (var key in ministryKeys)
                {
                    var ministry = dbContext.Ministry.SingleOrDefault(m => m.Key == key);
                    userPrefs.Add(new UserMinistryPreference {
                        Email = email, Ministry = ministry
                    });
                }
                dbContext.AddRange(userPrefs);

                dbContext.SaveChanges();
                return(CreatedAtRoute("GetUserMinistryPreferences", ministryKeys));
            }
            catch (Exception ex)
            {
                return(BadRequest("Failed to create user ministry preferences", ex));
            }
        }
예제 #2
0
        public void GetPostLogs_ShouldReturnSuccess()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            NewsRelease post = TestData.CreateDbPost();

            context.NewsRelease.Add(post);
            // line 52 caused the test to fail after the update to EF core 3.0
            // post.NewsReleaseLog = TestData.CreateDbPostLogs(post);
            // lines 54 to 56 replace the problematic line 52 by saving the logs before adding them to the parent post
            var logs = TestData.CreateDbPostLogs(post);

            context.AddRange(logs);
            context.SaveChanges();
            post.NewsReleaseLog = logs;
            var expectedLogEntry = post.NewsReleaseLog.FirstOrDefault();
            var expectedCount    = post.NewsReleaseLog.Count;

            context.SaveChanges();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            var result = controller.GetPostLogs(post.Key) as ObjectResult;

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            result.Should().BeOfType(typeof(OkObjectResult), "because the read operation should go smoothly");
            var actual         = result.Value as IEnumerable <Models.PostLog>;
            var actualLogEntry = actual.FirstOrDefault();

            actual.Count().Should().Be(expectedCount);
            actualLogEntry.PostKey.Should().Be(expectedLogEntry.Release.Key);
            actualLogEntry.Description.Should().Be(expectedLogEntry.Description);
        }