コード例 #1
0
        public async Task <int> Handle(CreateWorkingHoursCommand request, CancellationToken cancellationToken)
        {
            _identityService.CheckUserIsManagingHisOwnRecordsOrIsAdmin(request.UserName);
            var user = await _identityService.GetUserByNameAsync(request.UserName);

            _identityService.CheckUserIsNotNull(user);

            var date = request.Date.Date;
            var workingHoursOnThatDay = await _applicationDbContext.WorkingHoursSet
                                        .Where(i => i.Date == date && i.UserId == user.UserId).ToListAsync();

            var sumOfWorkingHoursOnThatDay = new TimeSpan(workingHoursOnThatDay.Sum(i => i.Duration.Ticks));

            if (sumOfWorkingHoursOnThatDay + request.Duration > DurationConstants.MaxDuration)
            {
                throw new ValidationException($"Sum of working hours per day can't be bigger than {DurationConstants.MaxDuration.TotalHours} hours");
            }

            var workingHours = new UserWorkingHours(user.UserId, request.Description, date, request.Duration);

            _applicationDbContext.WorkingHoursSet.Add(workingHours);
            await _applicationDbContext.SaveChangesAsync(cancellationToken);

            return(workingHours.Id);
        }
コード例 #2
0
        public async Task ShouldCreateWorkingHours()
        {
            //Arrange
            var request = new CreateWorkingHoursCommand
            {
                Date        = _date,
                Description = "Description",
                Duration    = TimeSpan.FromHours(4),
                UserName    = _userName
            };
            var expectedCreatedId   = 10;
            var expectedWorkingHour = new UserWorkingHours(_userId, "Description", _date, TimeSpan.FromHours(4))
            {
                Id = expectedCreatedId
            };
            var user = new UserDto(_userName, _userId, "Role");

            _identityService.Setup(s => s.GetUserByNameAsync(_userName)).ReturnsAsync(user);

            var mockWorkingHoursDbSet = new List <UserWorkingHours>
            {
                new UserWorkingHours(_userId, "Description", _date, TimeSpan.FromHours(10))
            }.AsQueryable().BuildMockDbSet();

            _applicationDbContext.Setup(c => c.WorkingHoursSet).Returns(mockWorkingHoursDbSet.Object);
            _applicationDbContext.Setup(c => c.WorkingHoursSet.Add(It.IsAny <UserWorkingHours>()))
            .Callback <UserWorkingHours>(i => i.Id = expectedCreatedId);

            //Act
            var id = await _handler.Handle(request, _cancellationToken);

            //Assert
            id.Should().Be(expectedCreatedId);
            _applicationDbContext.Verify(c =>
                                         c.WorkingHoursSet.Add(It.Is <UserWorkingHours>(i => MatchWorkingHours(i, expectedWorkingHour))));
            _applicationDbContext.Verify(c => c.SaveChangesAsync(_cancellationToken), Times.Once);
        }
コード例 #3
0
 private static bool MatchWorkingHours(UserWorkingHours userWorkingHours, UserWorkingHours expectedWorkingHour)
 {
     userWorkingHours.Should().BeEquivalentTo(expectedWorkingHour);
     return(true);
 }