private decimal CalculateNewLastIndex(ReadingUnitDTO currentTail)
        {
            var roundedTailIndex = Math.Round(currentTail.ActivityStatus.SortingIndex, 0);
            var newTailIndex     = roundedTailIndex + 1;

            _logger.ForContext("OldTailIndex", currentTail.ActivityStatus.SortingIndex).Debug("Calculated new tail sort index to {newIndex}", newTailIndex);

            return(newTailIndex);
        }
        private async Task SetSortIndexToLast(ReadingUnitDTO dto)
        {
            _logger.ForContext("UnitId", dto.Id).Debug("Move unit to last spot");
            var currentTail =
                await PlannedUnitsInProgram()
                .OrderByDescending(x => x.ActivityStatus.SortingIndex).FirstOrDefaultAsync();

            dto.ActivityStatus.SortingIndex = currentTail == null ? 0 : CalculateNewLastIndex(currentTail);
        }
        private async Task SetSortIndexToFirst(ReadingUnitDTO dto)
        {
            _logger.ForContext("UnitId", dto.Id).Debug("Move unit to first spot");
            var currentFrontTwo = await PlannedUnitsInProgramSorted().Take(2).ToListAsync();

            var currentFront = currentFrontTwo.First();

            currentFront.ActivityStatus.SortingIndex = CalculateNewSortIndex(currentFrontTwo);
            dto.ActivityStatus.SortingIndex          = 0;
        }
        private void UpdateActivityStatus(ActivityStatus newStatus, ReadingUnitDTO dto)
        {
            var oldStatus = dto.ActivityStatus;

            dto.ActivityStatus = newStatus.ToDTO();
            _logger
            .ForContext("ReadingUnit", dto.Id)
            .ForContext("FromStatus", oldStatus)
            .ForContext("ToStatus", dto.ActivityStatus)
            .Information("Update activity status");
        }
        private async Task UpdateSortIndex(int toSpot, ReadingUnitDTO dto)
        {
            _logger.ForContext("UnitId", dto.Id).Debug("Move unit to spot {spot}", toSpot);

            var toSkip = toSpot;

            if (toSpot < dto.ActivityStatus.SortingIndex)
            {
                toSkip -= 1;
                _logger.Debug("Moving to before current position, skipping one less");
            }

            _logger.Debug("Skipping {toSkip} to find indexes", toSkip);
            var plannedToBeBetween = await CategoriesInProgram()
                                     .Where(x => x.ActivityStatus.Type == ActivityStatusType.Planned)
                                     .OrderBy(x => x.ActivityStatus.SortingIndex)
                                     .Skip(toSkip)
                                     .Take(2)
                                     .ToListAsync();

            dto.ActivityStatus.SortingIndex = CalculateNewSortIndex(plannedToBeBetween);
        }