How can I set the previous entry's end time to be the start time of entry I'm incrementing?

19 Views Asked by At

I have a program that adds entries with buttons, the start time and end time are then shown. Next to those entries are buttons that increment the value by 10 minutes. When adding a new entry the start time is set to the last entry's end time. When incrementing or decrementing the end time, the next entry's start time is updated correctly, but when trying to increment or decrement the start time the last entry's end time is not updated.

public async Task<IActionResult> OnPostAdjustStartTimeAsync(int id, bool increment)
{
    var currentEntry = await _context.WorkDay.FindAsync(id);
    if (currentEntry == null)
    {
        // Entry not found
        return NotFound();
    }

    // Adjust the start time of the current entry
    currentEntry.StartTime = increment ? currentEntry.StartTime.AddMinutes(10) : currentEntry.StartTime.AddMinutes(-10);

    // Retrieve the previous entry, which has the closest end time but is less than the current entry's adjusted start time
    var previousEntry = await _context.WorkDay
        .Where(wd => wd.EndTime <= currentEntry.StartTime && wd.Id != currentEntry.Id)
        .OrderByDescending(wd => wd.EndTime)
        .FirstOrDefaultAsync();

    if (previousEntry != null)
    {
        // Logic to ensure the previous entry's end time does not exceed the current entry's start time
        if (!increment && previousEntry.EndTime > currentEntry.StartTime)
        {
            previousEntry.EndTime = currentEntry.StartTime;
        }
        // If incrementing, you might decide to leave a gap or adjust based on specific rules
    }

    await _context.SaveChangesAsync(); // Save both adjustments
    return RedirectToPage();
}

public async Task<IActionResult> OnPostAdjustEndTimeAsync(int id, bool increment)
{
    var currentEntry = await _context.WorkDay.FindAsync(id);
    if (currentEntry == null)
    {
        return NotFound();
    }

    // Adjust the current entry's end time by 10 minutes.
    currentEntry.EndTime = increment ? currentEntry.EndTime.AddMinutes(10) : currentEntry.EndTime.AddMinutes(-10);

    // Ensure adjustments do not make the current end time earlier than the start time.
    if (currentEntry.EndTime <= currentEntry.StartTime)
    {
        // Optionally, revert to the minimum duration or handle differently.
        currentEntry.EndTime = currentEntry.StartTime.AddMinutes(10);
    }

    // Find the next sequential entry, if any.
    var nextEntry = await _context.WorkDay
                                  .Where(e => e.StartTime > currentEntry.StartTime && e.Id != currentEntry.Id)
                                  .OrderBy(e => e.StartTime)
                                  .FirstOrDefaultAsync();

    if (nextEntry != null)
    {
        // If incrementing, adjust the next entry's start time to prevent overlap.
        if (increment && currentEntry.EndTime > nextEntry.StartTime)
        {
            nextEntry.StartTime = currentEntry.EndTime;
        }
        // If decrementing, you may want to pull back the next entry's start time to maintain sequence without gaps.
        else if (!increment && nextEntry.StartTime > currentEntry.EndTime)
        {
            nextEntry.StartTime = currentEntry.EndTime;
        }
    }

    await _context.SaveChangesAsync();
    return RedirectToPage();
}
0

There are 0 best solutions below