Ejemplo n.º 1
0
 public IActionResult AddMessage(Models.Message message)
 {
     try
     {
         if (message.Id != Guid.Empty)
         {
             throw new ValidationException("Invalid parameter (id)");
         }
         message.Id        = Guid.NewGuid();
         message.Timestamp = DateTime.Now;
         var dbMessage = new Message {
             IsActive = true
         };
         dbContext.Entry(dbMessage).CurrentValues.SetValues(message);
         if (message.IsPublished)
         {
             InsertAndBumpSortOrders(dbMessage);
             EnsureHighlightIsUnique(dbMessage);
         }
         dbContext.Message.Add(dbMessage);
         dbContext.SaveChanges();
         return(CreatedAtRoute("GetMessage", new { id = dbMessage.Id }, mapper.Map <Models.Message>(dbMessage)));
     }
     catch (Exception ex)
     {
         return(BadRequest("Failed to create message", ex));
     }
 }
Ejemplo n.º 2
0
        public void GetByKey_ShouldReturnSuccess()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var _expectedModelReturn = TestData.CreateDbPost();

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

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            var result = Controller().GetPost(_expectedModelReturn.Key);

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            result.Should().BeOfType(typeof(OkObjectResult), "because the read operation should go smoothly");

            var actualValue = ((result as OkObjectResult).Value as Models.Post);

            actualValue.Key.Should().Be(_expectedModelReturn.Key);
            actualValue.PublishDateTime.Should().Be(_expectedModelReturn.PublishDateTime);
            actualValue.Summary.Should().Be(_expectedModelReturn.NewsReleaseLanguage.First().Summary);
        }
        public void GetPostLogs_ShouldReturnSuccess()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            NewsRelease post = TestData.CreateDbPost();

            context.NewsRelease.Add(post);
            post.NewsReleaseLog = TestData.CreateDbPostLogs(post);
            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);
        }
        public IActionResult Post([FromBody] ActivityViewModel activity)
        {
            try
            {
                dbContext.Activity.Add(mapper.Map <ActivityViewModel, Activity>(activity));
                dbContext.SaveChanges();
                return(CreatedAtRoute("GetActivity", new { id = activity.Id }, activity));
            }
            catch (Exception ex)
            {
                logger.LogError($"Failed to save a activities: {ex}");
            }

            return(BadRequest("Failed to save activities"));
        }
        public void GetAllMinistries_ShouldReturnSuccess()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var expectedMinistries = 10;

            for (var i = 0; i < expectedMinistries; i++)
            {
                context.Ministry.Add(TestData.CreateDbMinistries($"Ministry-{i}", Guid.NewGuid()));
            }
            context.SaveChanges();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            var results = Controller().GetAllMinistries();

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            var okObjectResult = results as OkObjectResult;

            Assert.NotNull(okObjectResult);

            var model = (IList <Models.Ministry>)okObjectResult.Value;

            Assert.NotNull(model);

            Assert.Equal(expectedMinistries, model.Count());
        }
        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));
            }
        }
Ejemplo n.º 7
0
 public IActionResult AddActivity([FromBody] Models.Activity activity)
 {
     try
     {
         Activity dbActivity = new Activity {
             CreatedDateTime = DateTime.Now
         };
         dbActivity.UpdateFromModel(activity, dbContext);
         dbContext.Activity.Add(dbActivity);
         dbContext.SaveChanges();
         return(CreatedAtRoute("GetActivity", new { id = activity.Id }, mapper.Map <Models.Activity>(dbActivity)));
     }
     catch (Exception ex)
     {
         return(BadRequest("Failed to save an activity", ex));
     }
 }
        public void Get_ShouldReturnSuccess()
        {
            var email = "*****@*****.**";
            var testDbUserMinistryPreference = TestData.CreateUserMinistryPreference(email);

            context.UserMinistryPreference.Add(testDbUserMinistryPreference);
            context.SaveChanges();

            var token = generateToken(TokenType.Valid, email, "Test User");

            controller.Request.Headers["Authorization"] = $"Bearer {token}";

            var result = controller.GetUserMinistryPreferences() as ObjectResult;

            result.Should().BeOfType <OkObjectResult>();
            result.StatusCode.Should().Be(200);
            result.Value.Should().BeOfType <List <string> >();
        }
Ejemplo n.º 9
0
 public IActionResult Post(MessageViewModel messageVM)
 {
     try
     {
         if (messageVM.Id != Guid.Empty)
         {
             throw new ValidationException("Invalid parameter (id)");
         }
         var message = mapper.Map <MessageViewModel, Message>(messageVM);
         message.Id = Guid.NewGuid();
         dbContext.Message.Add(message);
         dbContext.SaveChanges();
         return(CreatedAtRoute("GetMessage", new { id = message.Id }, mapper.Map <Message, MessageViewModel>(message)));
     }
     catch (Exception ex)
     {
         logger.LogError($"Failed to create message: {ex}");
         return(BadRequest("Failed to create message"));
     }
 }
Ejemplo n.º 10
0
 public IActionResult AddPost(Models.Post post)
 {
     try
     {
         if (post == null)
         {
             throw new ValidationException();
         }
         var dbPost = new NewsRelease {
             Id = Guid.NewGuid(), IsPublished = true
         };
         dbPost.UpdateFromModel(post, dbContext);
         dbContext.NewsRelease.Add(dbPost);
         dbContext.SaveChanges();
         return(CreatedAtRoute("GetPost", new { key = dbPost.Key }, dbPost.ToModel(mapper)));
     }
     catch (Exception ex)
     {
         return(BadRequest("Failed to save a new post", ex));
     }
 }
Ejemplo n.º 11
0
 public IActionResult AddSocialMediaPost(Models.SocialMediaPost socialMediaPost)
 {
     try
     {
         if (socialMediaPost.Id != Guid.Empty)
         {
             throw new ValidationException("Invalid parameter (id)");
         }
         var dbPost = new SocialMediaPost {
             IsActive = true
         };
         socialMediaPost.Id        = Guid.NewGuid();
         socialMediaPost.Timestamp = DateTime.Now;
         dbContext.Entry(dbPost).CurrentValues.SetValues(socialMediaPost);
         dbContext.SocialMediaPost.Add(dbPost);
         dbContext.SaveChanges();
         return(CreatedAtRoute("GetSocialMediaPost", new { id = dbPost.Id }, mapper.Map <Models.SocialMediaPost>(dbPost)));
     }
     catch (Exception ex)
     {
         return(BadRequest("Failed to create social media post", ex));
     }
 }
        public void GetAll_ShouldReturnSuccessAndSorted(int postCount)
        {
            for (var i = 0; i < postCount; i++)
            {
                context.SocialMediaPost.Add(TestData.CreateDbSocialMediaPost("http://facebook.com/post/123", Guid.NewGuid(), true, postCount - i));
            }
            context.SaveChanges();

            var result   = controller.GetAllSocialMediaPosts();
            var okResult = result as ObjectResult;

            okResult.Should().BeOfType <OkObjectResult>();
            okResult.Should().NotBeNull();

            var models = okResult.Value as IList <Models.SocialMediaPost>;

            models.Should().NotBeNull();
            models.Count().Should().Be(postCount);
            for (int i = 0; i < models.Count() - 1; i++)
            {
                models[i].SortOrder.Should().BeLessThan(models[i + 1].SortOrder);
            }
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
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));
     }
 }
Ejemplo n.º 15
0
        public void GetNewsForecast_ShouldReturnSuccess(int activityCount)
        {
            for (var i = 0; i < activityCount; i++)
            {
                var dbActivity = TestData.CreateDbActivity(i.ToString(), "test details", i + 1);
                context.Activity.Add(dbActivity);
            }
            context.SaveChanges();

            var result = Controller().GetActivityForecast(7) as ObjectResult;

            result.Should().BeOfType <OkObjectResult>();
            result.Should().NotBeNull();
            var models = result.Value as ICollection <Models.Activity>;

            models.Should().NotBeNull();
            models.Count().Should().Be(activityCount);
        }
        public void GetAll_ShouldReturnSuccess(int messageCount)
        {
            for (var i = 0; i < messageCount; i++)
            {
                var message = TestData.CreateMessage(i.ToString(), "test description", 0, true, false);
                message.Id = Guid.NewGuid();
                context.Message.Add(message);
            }
            context.SaveChanges();
            var controller = new MessagesController(context, logger.Object, mapper);

            var result = controller.GetAll() as ObjectResult;

            result.Should().BeOfType <OkObjectResult>();
            result.Should().NotBeNull();
            var models = result.Value as ICollection <Message>;

            models.Should().NotBeNull();
            models.Count().Should().Be(messageCount);
        }
Ejemplo n.º 17
0
        public void GetAll_ShouldReturnSuccess(int messageCount)
        {
            for (var i = 0; i < messageCount; i++)
            {
                context.Message.Add(TestData.CreateDbMessage(i.ToString(), "test description", 0, Guid.NewGuid(), true, false));
            }
            context.SaveChanges();

            var result = controller.GetAllMessages() as ObjectResult;

            result.Should().BeOfType <OkObjectResult>();
            result.Should().NotBeNull();
            var models = result.Value as ICollection <Models.Message>;

            models.Should().NotBeNull();
            models.Count().Should().Be(messageCount);
        }