Skip to content

Commit

Permalink
Generic sorting of data
Browse files Browse the repository at this point in the history
  • Loading branch information
spech66 committed Apr 13, 2023
1 parent 8161b1f commit e6f9b8b
Show file tree
Hide file tree
Showing 19 changed files with 102 additions and 79 deletions.
12 changes: 9 additions & 3 deletions LifelogBb/Controllers/BucketListsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ public BucketListsController(LifelogBbContext context, IConfiguration configurat
// GET: BucketLists
public async Task<IActionResult> Index(string sortOrder, string currentFilter, string searchString, int? pageNumber)
{
var defaultSortOrder = $"{nameof(BucketList.CreatedAt)}_desc";
ViewData["CurrentSort"] = sortOrder;
ViewData["DateSortParm"] = sortOrder == nameof(BucketList.CreatedAt) ? defaultSortOrder : nameof(BucketList.CreatedAt);

if (searchString != null)
{
Expand All @@ -43,7 +41,7 @@ public async Task<IActionResult> Index(string sortOrder, string currentFilter, s
ViewData["CurrentFilter"] = searchString;

var items = from s in _context.BucketLists select s;
items = items.SortByName(sortOrder, defaultSortOrder);
items = items.SortByName(sortOrder, $"{nameof(BucketList.CreatedAt)}_desc");

int pageSize = Config.GetConfig(_context).BucketListPageSize;
return View(await PaginatedList<BucketList>.CreateAsync(items.AsNoTracking(), pageNumber ?? 1, pageSize));
Expand Down Expand Up @@ -132,6 +130,8 @@ public async Task<IActionResult> Create(/*[Bind("...")]*/CreateBucketListViewMod
if (!ValidateFileExtions(bucketListViewModel.ImageData.FileName))
{
ModelState.AddModelError("ImageData", "Invalid file extension.");
this.AddCategoriesToViewData(_context);
this.AddTagsToViewData(_context);
return View(bucketListViewModel);
}

Expand All @@ -147,6 +147,8 @@ public async Task<IActionResult> Create(/*[Bind("...")]*/CreateBucketListViewMod
else
{
ModelState.AddModelError("ImageData", "The file is too large.");
this.AddCategoriesToViewData(_context);
this.AddTagsToViewData(_context);
return View(bucketListViewModel);
}
}
Expand Down Expand Up @@ -205,6 +207,8 @@ public async Task<IActionResult> Edit(long id, /*[Bind("...")]*/ EditBucketListV
if (!ValidateFileExtions(bucketListViewModel.ImageData.FileName))
{
ModelState.AddModelError("ImageData", "Invalid file extension.");
this.AddCategoriesToViewData(_context);
this.AddTagsToViewData(_context);
return View(bucketListViewModel);
}

Expand All @@ -220,6 +224,8 @@ public async Task<IActionResult> Edit(long id, /*[Bind("...")]*/ EditBucketListV
else
{
ModelState.AddModelError("ImageData", "The file is too large.");
this.AddCategoriesToViewData(_context);
this.AddTagsToViewData(_context);
return View(bucketListViewModel);
}
}
Expand Down
4 changes: 1 addition & 3 deletions LifelogBb/Controllers/EnduranceTrainingsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ public EnduranceTrainingsController(LifelogBbContext context, IMapper mapper)
// GET: EnduranceTrainings
public async Task<IActionResult> Index(string sortOrder, string currentFilter, string searchString, int? pageNumber)
{
var defaultSortOrder = $"{nameof(EnduranceTraining.CreatedAt)}_desc";
ViewData["CurrentSort"] = sortOrder;
ViewData["DateSortParm"] = sortOrder == nameof(EnduranceTraining.CreatedAt) ? defaultSortOrder : nameof(EnduranceTraining.CreatedAt);

if (searchString != null)
{
Expand All @@ -38,7 +36,7 @@ public async Task<IActionResult> Index(string sortOrder, string currentFilter, s
ViewData["CurrentFilter"] = searchString;

var trainings = from s in _context.EnduranceTrainings select s;
trainings = trainings.SortByName(sortOrder, defaultSortOrder);
trainings = trainings.SortByName(sortOrder, $"{nameof(EnduranceTraining.CreatedAt)}_desc");

int pageSize = 20;
return View(await PaginatedList<EnduranceTraining>.CreateAsync(trainings.AsNoTracking(), pageNumber ?? 1, pageSize));
Expand Down
4 changes: 1 addition & 3 deletions LifelogBb/Controllers/GoalsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ public GoalsController(LifelogBbContext context, IMapper mapper)
// GET: Goals
public async Task<IActionResult> Index(string sortOrder, string currentFilter, string searchString, int? pageNumber)
{
var defaultSortOrder = $"{nameof(Goal.CreatedAt)}_desc";
ViewData["CurrentSort"] = sortOrder;
ViewData["DateSortParm"] = sortOrder == nameof(Goal.CreatedAt) ? defaultSortOrder : nameof(Goal.CreatedAt);

if (searchString != null)
{
Expand All @@ -43,7 +41,7 @@ public async Task<IActionResult> Index(string sortOrder, string currentFilter, s
ViewData["CurrentFilter"] = searchString;

var goals = from s in _context.Goals select s;
goals = goals.SortByName(sortOrder, defaultSortOrder);
goals = goals.SortByName(sortOrder, $"{nameof(Goal.CreatedAt)}_desc");

var config = Config.GetConfig(_context);
int pageSize = 20;
Expand Down
7 changes: 4 additions & 3 deletions LifelogBb/Controllers/HabitsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public HabitsController(LifelogBbContext context, IMapper mapper)
// GET: Habits
public async Task<IActionResult> Index(string sortOrder, string currentFilter, string searchString, int? pageNumber)
{
var defaultSortOrder = $"{nameof(Habit.CreatedAt)}_desc";
ViewData["CurrentSort"] = sortOrder;
ViewData["DateSortParm"] = sortOrder == nameof(Habit.CreatedAt) ? defaultSortOrder : nameof(Habit.CreatedAt);

if (searchString != null)
{
Expand All @@ -44,7 +42,7 @@ public async Task<IActionResult> Index(string sortOrder, string currentFilter, s
ViewData["CurrentFilter"] = searchString;

var habits = from s in _context.Habits select s;
habits = habits.SortByName(sortOrder, defaultSortOrder);
habits = habits.SortByName(sortOrder, $"{nameof(Habit.CreatedAt)}_desc");

var config = Config.GetConfig(_context);
int pageSize = 20;
Expand Down Expand Up @@ -149,6 +147,9 @@ public async Task<IActionResult> Edit(long id, [Bind("Name,Description,StartDate
}
return RedirectToAction(nameof(Index));
}

this.AddCategoriesToViewData(_context);
this.AddTagsToViewData(_context);
return View(habitViewModel);
}

Expand Down
4 changes: 1 addition & 3 deletions LifelogBb/Controllers/JournalsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ public JournalsController(LifelogBbContext context, IMapper mapper)
// GET: Journals
public async Task<IActionResult> Index(string sortOrder, string currentFilter, string searchString, int? pageNumber)
{
var defaultSortOrder = $"{nameof(Journal.CreatedAt)}_desc";
ViewData["CurrentSort"] = sortOrder;
ViewData["DateSortParm"] = sortOrder == nameof(Journal.CreatedAt) ? defaultSortOrder : nameof(Journal.CreatedAt);

if (searchString != null)
{
Expand All @@ -38,7 +36,7 @@ public async Task<IActionResult> Index(string sortOrder, string currentFilter, s
ViewData["CurrentFilter"] = searchString;

var journals = from s in _context.Journals select s;
journals = journals.SortByName(sortOrder, defaultSortOrder);
journals = journals.SortByName(sortOrder, $"{nameof(Journal.CreatedAt)}_desc");

int pageSize = 20;
return View(await PaginatedList<Journal>.CreateAsync(journals.AsNoTracking(), pageNumber ?? 1, pageSize));
Expand Down
4 changes: 1 addition & 3 deletions LifelogBb/Controllers/QuotesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ public QuotesController(LifelogBbContext context, IMapper mapper)
// GET: Quotes
public async Task<IActionResult> Index(string sortOrder, string currentFilter, string searchString, int? pageNumber)
{
var defaultSortOrder = $"{nameof(Quote.CreatedAt)}_desc";
ViewData["CurrentSort"] = sortOrder;
ViewData["DateSortParm"] = sortOrder == nameof(Quote.CreatedAt) ? defaultSortOrder : nameof(Quote.CreatedAt);

if (searchString != null)
{
Expand All @@ -38,7 +36,7 @@ public async Task<IActionResult> Index(string sortOrder, string currentFilter, s
ViewData["CurrentFilter"] = searchString;

var quotes = from s in _context.Quotes select s;
quotes = quotes.SortByName(sortOrder, defaultSortOrder);
quotes = quotes.SortByName(sortOrder, $"{nameof(Quote.CreatedAt)}_desc");

int pageSize = 20;
return View(await PaginatedList<Quote>.CreateAsync(quotes.AsNoTracking(), pageNumber ?? 1, pageSize));
Expand Down
4 changes: 1 addition & 3 deletions LifelogBb/Controllers/StrengthTrainingsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ public StrengthTrainingsController(LifelogBbContext context, IMapper mapper)
// GET: StrengthTrainings
public async Task<IActionResult> Index(string sortOrder, string currentFilter, string searchString, int? pageNumber)
{
var defaultSortOrder = $"{nameof(StrengthTraining.CreatedAt)}_desc";
ViewData["CurrentSort"] = sortOrder;
ViewData["DateSortParm"] = sortOrder == nameof(StrengthTraining.CreatedAt) ? defaultSortOrder : nameof(StrengthTraining.CreatedAt);

if (searchString != null)
{
Expand All @@ -38,7 +36,7 @@ public async Task<IActionResult> Index(string sortOrder, string currentFilter, s
ViewData["CurrentFilter"] = searchString;

var trainings = from s in _context.StrengthTrainings select s;
trainings = trainings.SortByName(sortOrder, defaultSortOrder);
trainings = trainings.SortByName(sortOrder, $"{nameof(StrengthTraining.CreatedAt)}_desc");

int pageSize = 20;
return View(await PaginatedList<StrengthTraining>.CreateAsync(trainings.AsNoTracking(), pageNumber ?? 1, pageSize));
Expand Down
4 changes: 1 addition & 3 deletions LifelogBb/Controllers/TodosController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ public TodosController(LifelogBbContext context, IMapper mapper)
// GET: Todos
public async Task<IActionResult> Index(string sortOrder, string currentFilter, string searchString, int? pageNumber)
{
var defaultSortOrder = $"{nameof(Todo.CreatedAt)}_desc";
ViewData["CurrentSort"] = sortOrder;
ViewData["DateSortParm"] = sortOrder == nameof(Todo.CreatedAt) ? defaultSortOrder : nameof(Todo.CreatedAt);

if (searchString != null)
{
Expand All @@ -43,7 +41,7 @@ public async Task<IActionResult> Index(string sortOrder, string currentFilter, s
ViewData["CurrentFilter"] = searchString;

var todos = from s in _context.Todos select s;
todos = todos.SortByName(sortOrder, defaultSortOrder);
todos = todos.SortByName(sortOrder, $"{nameof(Todo.CreatedAt)}_desc");

var config = Config.GetConfig(_context);
int pageSize = 20;
Expand Down
4 changes: 1 addition & 3 deletions LifelogBb/Controllers/WeightsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ public WeightsController(LifelogBbContext context, IMapper mapper)
// GET: Weights
public async Task<IActionResult> Index(string sortOrder, string currentFilter, string searchString, int? pageNumber)
{
var defaultSortOrder = $"{nameof(Weight.CreatedAt)}_desc";
ViewData["CurrentSort"] = sortOrder;
ViewData["DateSortParm"] = sortOrder == nameof(Weight.CreatedAt) ? defaultSortOrder : nameof(Weight.CreatedAt);

if (searchString != null)
{
Expand All @@ -39,7 +37,7 @@ public async Task<IActionResult> Index(string sortOrder, string currentFilter, s

var weights = from s in _context.Weights select s;
weights = weights.FilterByDoubleProps(nameof(Weight.BodyWeight), searchString, 1.0);
weights = weights.SortByName(sortOrder, defaultSortOrder);
weights = weights.SortByName(sortOrder, $"{nameof(Weight.CreatedAt)}_desc");

var config = Config.GetConfig(_context);
int pageSize = config.WeightPageSize;
Expand Down
31 changes: 26 additions & 5 deletions LifelogBb/Utilities/RazorPageExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Mvc.ViewFeatures;

namespace LifelogBb.Utilities
{
public static class RazorPageExtensions
{
/// <summary>
/// Get desc or asc depending of the current sort order of the field
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="viewData"></param>
/// <param name="fieldName"></param>
/// <returns></returns>
public static string GetSortOrder<T>(ViewDataDictionary<T> viewData, string fieldName)
{
if (viewData["CurrentSort"] == null)
Expand All @@ -15,14 +21,29 @@ public static string GetSortOrder<T>(ViewDataDictionary<T> viewData, string fiel
if ((string)viewData["CurrentSort"] == $"{fieldName}_desc")
{
return "desc";
}

else if ((string)viewData["CurrentSort"] == fieldName)
} else if ((string)viewData["CurrentSort"] == fieldName)
{
return "asc";
}

return "";
}

/// <summary>
/// Get the inverted route for the field
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="viewData"></param>
/// <param name="fieldName"></param>
/// <returns></returns>
public static string GetSortRoute<T>(ViewDataDictionary<T> viewData, string fieldName)
{
if (viewData.ContainsKey("CurrentSort") && viewData["CurrentSort"] != null && (string)viewData["CurrentSort"] == $"{fieldName}_desc")
{
return fieldName;
}

return $"{fieldName}_desc";
}
}
}
6 changes: 5 additions & 1 deletion LifelogBb/Views/BucketLists/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
<div class="col">
<div class="card">
<div class="card-body">
Sort by <a asp-action="Index" asp-route-sortOrder="@ViewData["DateSortParm"]" asp-route-currentFilter="@ViewData["CurrentFilter"]" class="table-sort @RazorPageExtensions.GetSortOrder(ViewData, "CreatedAt")" style="display: inline">Date</a>.
Sort by
<a asp-action="Index" asp-route-sortOrder="@RazorPageExtensions.GetSortRoute(ViewData, "CreatedAt")" asp-route-currentFilter="@ViewData["CurrentFilter"]" class="table-sort @RazorPageExtensions.GetSortOrder(ViewData, "CreatedAt")" style="display: inline">Date</a>,
<a asp-action="Index" asp-route-sortOrder="@RazorPageExtensions.GetSortRoute(ViewData, "Title")" asp-route-currentFilter="@ViewData["CurrentFilter"]" class="table-sort @RazorPageExtensions.GetSortOrder(ViewData, "Title")" style="display: inline">Title</a>,
<a asp-action="Index" asp-route-sortOrder="@RazorPageExtensions.GetSortRoute(ViewData, "Status")" asp-route-currentFilter="@ViewData["CurrentFilter"]" class="table-sort @RazorPageExtensions.GetSortOrder(ViewData, "Status")" style="display: inline">Status</a>,
<a asp-action="Index" asp-route-sortOrder="@RazorPageExtensions.GetSortRoute(ViewData, "Category")" asp-route-currentFilter="@ViewData["CurrentFilter"]" class="table-sort @RazorPageExtensions.GetSortOrder(ViewData, "Category")" style="display: inline">Category</a> .
</div>
</div>
</div>
Expand Down
8 changes: 4 additions & 4 deletions LifelogBb/Views/EnduranceTrainings/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
<thead>
<tr>
<th class="w-1">
<a asp-action="Index" asp-route-sortOrder="@ViewData["DateSortParm"]" asp-route-currentFilter="@ViewData["CurrentFilter"]" class="table-sort @RazorPageExtensions.GetSortOrder(ViewData, "CreatedAt")">Date</a>
<a asp-action="Index" asp-route-sortOrder="@RazorPageExtensions.GetSortRoute(ViewData, "CreatedAt")" asp-route-currentFilter="@ViewData["CurrentFilter"]" class="table-sort @RazorPageExtensions.GetSortOrder(ViewData, "CreatedAt")">Date</a>
</th>
<th>
Exercise
<a asp-action="Index" asp-route-sortOrder="@RazorPageExtensions.GetSortRoute(ViewData, "Exercise")" asp-route-currentFilter="@ViewData["CurrentFilter"]" class="table-sort @RazorPageExtensions.GetSortOrder(ViewData, "Exercise")">Exercise</a>
</th>
<th>
Distance
<a asp-action="Index" asp-route-sortOrder="@RazorPageExtensions.GetSortRoute(ViewData, "Distance")" asp-route-currentFilter="@ViewData["CurrentFilter"]" class="table-sort @RazorPageExtensions.GetSortOrder(ViewData, "Distance")">Distance</a>
</th>
<th>
Duration
Expand All @@ -34,7 +34,7 @@
Pace (min/km)
</th>
<th>
Rating
<a asp-action="Index" asp-route-sortOrder="@RazorPageExtensions.GetSortRoute(ViewData, "Rating")" asp-route-currentFilter="@ViewData["CurrentFilter"]" class="table-sort @RazorPageExtensions.GetSortOrder(ViewData, "Rating")">Rating</a>
</th>
<th>
Notes
Expand Down
Loading

0 comments on commit e6f9b8b

Please sign in to comment.