コード例 #1
0
        public static async Task Returns_updated_requests_after_saving()
        {
            var activeDates = new[] { 2.February(2021), 3.February(2021) };

            var returnedRequests = new[] { new Request(UserId, 2.February(2021), RequestStatus.Pending) };

            var mockRequestRepository =
                CreateRequestRepository.MockWithRequests(UserId, activeDates, returnedRequests);

            mockRequestRepository
            .Setup(r => r.SaveRequests(It.IsAny <IReadOnlyCollection <Request> >(), It.IsAny <IReadOnlyCollection <User> >()))
            .Returns(Task.CompletedTask);

            var patchRequest =
                new RequestsPatchRequest(new[] { new RequestsPatchRequestDailyData(2.February(2021), true) });

            var controller = new RequestsController(
                CreateDateCalculator.WithActiveDates(activeDates),
                mockRequestRepository.Object,
                Mock.Of <ITriggerRepository>(),
                CreateUserRepository.WithUsers(DefaultUsers))
            {
                ControllerContext = CreateControllerContext.WithUsername(UserId)
            };

            var result = await controller.PatchAsync(patchRequest);

            var resultValue = GetResultValue <RequestsResponse>(result);

            var day1data = GetDailyData(resultValue.Requests, 2.February(2021));
            var day2data = GetDailyData(resultValue.Requests, 3.February(2021));

            Assert.True(day1data.Requested);
            Assert.False(day2data.Requested);
        }
コード例 #2
0
        public static async Task Does_not_update_requests_outside_active_date_range()
        {
            var activeDates = new[] { 2.February(2021), 3.February(2021) };

            var mockRequestRepository =
                CreateRequestRepository.MockWithRequests(UserId, activeDates, new List <Request>());

            mockRequestRepository
            .Setup(r => r.SaveRequests(It.IsAny <IReadOnlyCollection <Request> >(), It.IsAny <IReadOnlyCollection <User> >()))
            .Returns(Task.CompletedTask);

            var patchRequest = new RequestsPatchRequest(new[]
            {
                new RequestsPatchRequestDailyData(1.February(2021), true),
                new RequestsPatchRequestDailyData(4.February(2021), true)
            });

            var controller = new RequestsController(
                CreateDateCalculator.WithActiveDates(activeDates),
                mockRequestRepository.Object,
                Mock.Of <ITriggerRepository>(),
                CreateUserRepository.WithUsers(DefaultUsers))
            {
                ControllerContext = CreateControllerContext.WithUsername(UserId)
            };

            await controller.PatchAsync(patchRequest);

            CheckSavedRequests(mockRequestRepository, new List <Request>(), DefaultUsers);
        }
コード例 #3
0
        public static async Task Updates_data_for_given_user_when_specified()
        {
            var activeDates = new[] { 2.February(2021) };

            var mockRequestRepository =
                CreateRequestRepository.MockWithRequests(UserId, activeDates, new List <Request>());

            mockRequestRepository
            .Setup(r => r.SaveRequests(It.IsAny <IReadOnlyCollection <Request> >(), It.IsAny <IReadOnlyCollection <User> >()))
            .Returns(Task.CompletedTask);

            var patchRequest = new RequestsPatchRequest(new[]
            {
                new RequestsPatchRequestDailyData(2.February(2021), true),
                new RequestsPatchRequestDailyData(3.February(2021), false),
            });

            var controller = new RequestsController(
                CreateDateCalculator.WithActiveDates(activeDates),
                mockRequestRepository.Object,
                Mock.Of <ITriggerRepository>(),
                CreateUserRepository.WithUserExistsAndUsers(UserId, true, DefaultUsers));

            await controller.PatchByIdAsync(UserId, patchRequest);

            var expectedSavedRequests = new[] { new Request(UserId, 2.February(2021), RequestStatus.Pending) };

            CheckSavedRequests(mockRequestRepository, expectedSavedRequests, DefaultUsers);
        }
コード例 #4
0
        public static async Task Adds_recalculation_trigger_when_saving_requests()
        {
            var activeDates = new[] { 2.February(2021) };

            var mockRequestRepository =
                CreateRequestRepository.MockWithRequests(UserId, activeDates, new List <Request>());

            mockRequestRepository
            .Setup(r => r.SaveRequests(It.IsAny <IReadOnlyCollection <Request> >(), It.IsAny <IReadOnlyCollection <User> >()))
            .Returns(Task.CompletedTask);

            var mockTriggerRepository = new Mock <ITriggerRepository>();

            var patchRequest = new RequestsPatchRequest(new List <RequestsPatchRequestDailyData>());

            var controller = new RequestsController(
                CreateDateCalculator.WithActiveDates(activeDates),
                mockRequestRepository.Object,
                mockTriggerRepository.Object,
                CreateUserRepository.WithUsers(DefaultUsers))
            {
                ControllerContext = CreateControllerContext.WithUsername(UserId)
            };

            await controller.PatchAsync(patchRequest);

            mockTriggerRepository.Verify(r => r.AddTrigger(), Times.Once);
        }
コード例 #5
0
        public async Task <IActionResult> PatchAsync([FromBody] RequestsPatchRequest request)
        {
            var userId = this.GetCognitoUserId();

            await this.UpdateRequests(userId, request);

            var response = await this.GetRequests(userId);

            return(this.Ok(response));
        }
コード例 #6
0
        public async Task <IActionResult> PatchByIdAsync(string userId, [FromBody] RequestsPatchRequest request)
        {
            if (!await this.userRepository.UserExists(userId))
            {
                return(this.NotFound());
            }

            await this.UpdateRequests(userId, request);

            var response = await this.GetRequests(userId);

            return(this.Ok(response));
        }
コード例 #7
0
        private async Task UpdateRequests(string userId, RequestsPatchRequest request)
        {
            var activeDates = this.dateCalculator.GetActiveDates();

            var users = await this.userRepository.GetUsers();

            var requestsToSave = request.Requests
                                 .Where(r => activeDates.Contains(r.LocalDate))
                                 .GroupBy(r => r.LocalDate)
                                 .Where(g => !ValuesCancelOut(g))
                                 .Select(AuthoritativeValue)
                                 .Select(v => CreateRequest(userId, v))
                                 .ToArray();

            await this.requestRepository.SaveRequests(requestsToSave, users);

            await this.triggerRepository.AddTrigger();
        }
コード例 #8
0
        public static async Task Updates_requests_with_last_given_value_for_each_date()
        {
            var activeDates = new[] { 2.February(2021), 3.February(2021) };

            var mockRequestRepository =
                CreateRequestRepository.MockWithRequests(UserId, activeDates, new List <Request>());

            mockRequestRepository
            .Setup(r => r.SaveRequests(It.IsAny <IReadOnlyCollection <Request> >(), It.IsAny <IReadOnlyCollection <User> >()))
            .Returns(Task.CompletedTask);

            var patchRequest = new RequestsPatchRequest(new[]
            {
                new RequestsPatchRequestDailyData(2.February(2021), true),
                new RequestsPatchRequestDailyData(2.February(2021), false),
                new RequestsPatchRequestDailyData(2.February(2021), true),
                new RequestsPatchRequestDailyData(3.February(2021), false),
                new RequestsPatchRequestDailyData(3.February(2021), true),
                new RequestsPatchRequestDailyData(3.February(2021), false),
            });

            var controller = new RequestsController(
                CreateDateCalculator.WithActiveDates(activeDates),
                mockRequestRepository.Object,
                Mock.Of <ITriggerRepository>(),
                CreateUserRepository.WithUsers(DefaultUsers))
            {
                ControllerContext = CreateControllerContext.WithUsername(UserId)
            };

            await controller.PatchAsync(patchRequest);

            var expectedSavedRequests = new[]
            {
                new Request(UserId, 2.February(2021), RequestStatus.Pending),
                new Request(UserId, 3.February(2021), RequestStatus.Cancelled),
            };

            CheckSavedRequests(mockRequestRepository, expectedSavedRequests, DefaultUsers);
        }