Skip to content

Commit

Permalink
Sort users alphabetically throughout the app
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlihou committed Sep 7, 2023
1 parent 50b265e commit 0cbf71f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 51 deletions.
4 changes: 2 additions & 2 deletions FoodPicker.Web/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public async Task<NextWeekResult> NextWeek()
group v by v.UserId into u
where u.Count() >= numMeals
select u.Key;
var users = _userManager.Users.Where(u => u.IsActive).ToList();

var users = _userManager.Users.Where(u => u.IsActive).OrderBy(u => u.Name).ToList();

var fullyVotedNames =
users.Where(x => fullyVotedUserIds.Contains(x.Id)).Select(x => x.Name).ToList();
Expand Down
3 changes: 2 additions & 1 deletion FoodPicker.Web/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using FoodPicker.Web.Enums;
using FoodPicker.Infrastructure.Models;
Expand All @@ -23,7 +24,7 @@ public UserController(UserManager<ApplicationUser> userManager)

public async Task<IActionResult> Index()
{
var users = await _userManager.Users.ToListAsync();
var users = await _userManager.Users.OrderBy(u => u.Name).ToListAsync();
return View("List", users);
}

Expand Down
102 changes: 55 additions & 47 deletions FoodPicker.Web/Controllers/WeekController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public class WeekController : Controller
private readonly MealVoteService _mealVoteService;
private readonly WeekUserCommentRepository _commentRepository;

public WeekController(UserManager<ApplicationUser> userManager, MealService mealService, MealWeekRepository mealWeekRepo, EfRepository<Meal> mealRepo, MealVoteRepository mealVoteRepo, MealVoteService mealVoteService, MealRatingRepository mealRatingRepo, WeekUserCommentRepository commentRepository)
public WeekController(UserManager<ApplicationUser> userManager, MealService mealService,
MealWeekRepository mealWeekRepo, EfRepository<Meal> mealRepo, MealVoteRepository mealVoteRepo,
MealVoteService mealVoteService, MealRatingRepository mealRatingRepo,
WeekUserCommentRepository commentRepository)
{
_userManager = userManager;
_mealService = mealService;
Expand All @@ -44,12 +47,12 @@ public class WeekListViewModel
public MealWeek MealWeek { get; set; }
public bool CurrentUserVotingComplete { get; set; }
}

[HttpGet("")]
public async Task<IActionResult> Index()
{
var mealWeeks = await _mealWeekRepo.ListAllAsync();

var viewModel = new List<WeekListViewModel>();
foreach (var mealWeek in mealWeeks)
{
Expand All @@ -60,10 +63,10 @@ public async Task<IActionResult> Index()
await _mealVoteService.IsVotingCompleteForUserForWeek(mealWeek, _userManager.GetUserId(User))
});
}

return View("List", viewModel);
}
}

[HttpGet]
[Route("Create")]
[Route("{id:int}")]
Expand All @@ -84,14 +87,14 @@ public async Task<IActionResult> CreateOrEdit(int? id)
else
{
// Editing
model = await _mealWeekRepo.GetByIdWithMealsAsync((int) id);
model = await _mealWeekRepo.GetByIdWithMealsAsync((int)id);

if (model == null) throw new ApplicationException("Week not found");
}

return View(model);
}
}

[HttpPost]
[Route("Create", Name = "WeekCreate")]
[Route("{id:int}", Name = "WeekEdit")]
Expand All @@ -106,27 +109,27 @@ public async Task<IActionResult> CreateOrEdit(int? id, [FromForm] MealWeek model
DeliveryDate = model.DeliveryDate,
MealWeekStatus = model.MealWeekStatus,
};

await _mealWeekRepo.AddAsync(dbModel);
}
else
{
dbModel = await _mealWeekRepo.GetByIdAsync((int) id);
dbModel = await _mealWeekRepo.GetByIdAsync((int)id);
dbModel.DeliveryDate = model.DeliveryDate;
dbModel.MealWeekStatus = model.MealWeekStatus;

await _mealWeekRepo.UpdateAsync(dbModel);
}

return RedirectToRoute("WeekEdit", new {id = dbModel.Id});
return RedirectToRoute("WeekEdit", new { id = dbModel.Id });
}

[HttpPost("[action]/{id:int}")]
public async Task<IActionResult> GenerateMeals(int? id)
{
if (id is 0 or null) return BadRequest();
var week = await _mealWeekRepo.GetByIdWithMealsAsync((int) id);

var week = await _mealWeekRepo.GetByIdWithMealsAsync((int)id);
if (week == null) return BadRequest();

var meals = await _mealService.GetMealsForMealWeek(week);
Expand All @@ -141,28 +144,30 @@ public async Task<IActionResult> GenerateMeals(int? id)
await _mealVoteService.ProcessAutoVotes(week);
}

return RedirectToRoute("WeekEdit", new {id});
return RedirectToRoute("WeekEdit", new { id });
}

public class MealVoteViewModel
{
public MealWeek MealWeek { get; set; }
public List<MealVote> UserMealVotes { get; set; }
public ILookup<int, MealRating> PreviousRatings { get; set; }

[MaxLength(1000)]
[Display(Name = "User Comment")]
public string WeekUserComment { get; set; }
}

[HttpGet("Vote/{id:int}")]
public async Task<IActionResult> Vote(int? id)
{
if (id is null or 0) return BadRequest();
var model = await _mealWeekRepo.GetByIdWithMealsAsync((int) id);

var model = await _mealWeekRepo.GetByIdWithMealsAsync((int)id);
var weekVotes = await _mealVoteRepo.GetUserVotesForWeekAsync(model, _userManager.GetUserId(User));

var ratingLookup = _mealRatingRepo.GetPreviousRatingsForMeals(model.Meals);

return View(new MealVoteViewModel
{
MealWeek = model,
Expand All @@ -171,16 +176,17 @@ public async Task<IActionResult> Vote(int? id)
MealId = x.Id
}).ToList(),
PreviousRatings = ratingLookup,
WeekUserComment = (await _commentRepository.GetCommentForWeekAndUser((int) id, _userManager.GetUserId(User)))?.Comment
WeekUserComment =
(await _commentRepository.GetCommentForWeekAndUser((int)id, _userManager.GetUserId(User)))?.Comment
});
}

[HttpPost("Vote/{id:int}")]
public async Task<IActionResult> Vote(int? id, [FromForm] MealVoteViewModel model)
{
if (id == null) return NotFound();
var userId = _userManager.GetUserId(User);
var dbComment = await _commentRepository.GetCommentForWeekAndUser((int) id, userId);
var dbComment = await _commentRepository.GetCommentForWeekAndUser((int)id, userId);
if (dbComment == null)
{
if (!string.IsNullOrEmpty(model.WeekUserComment))
Expand All @@ -199,15 +205,15 @@ public async Task<IActionResult> Vote(int? id, [FromForm] MealVoteViewModel mode
dbComment.Comment = model.WeekUserComment ?? "";
await _commentRepository.UpdateAsync(dbComment);
}

foreach (var dbVote in model.UserMealVotes.Select(vote => new MealVote
{
Id = vote.Id,
MealId = vote.MealId,
VoteOptionId = vote.VoteOptionId,
Comment = vote.Comment,
UserId = _userManager.GetUserId(User)
}))
{
Id = vote.Id,
MealId = vote.MealId,
VoteOptionId = vote.VoteOptionId,
Comment = vote.Comment,
UserId = _userManager.GetUserId(User)
}))
{
if (dbVote.Id == 0)
{
Expand Down Expand Up @@ -241,43 +247,45 @@ public class MealResult
public List<MealVote> Votes { get; set; }
public double Score { get; set; }
}

[HttpGet("Results/{weekId:int}")]
public async Task<IActionResult> ViewResults(int? weekId)
{
if (weekId is 0 or null) return NotFound();
var week = await _mealWeekRepo.GetByIdWithMealsAsync((int) weekId);
var week = await _mealWeekRepo.GetByIdWithMealsAsync((int)weekId);
if (week == null) return NotFound();

var model = new ViewResultsViewModel
{
Week = week,
ParticipatingUsers = await _userManager.Users.Where(u => u.IsActive).ToListAsync(),
ParticipatingUsers = await _userManager.Users.Where(u => u.IsActive).OrderBy(u => u.Name).ToListAsync(),
IsEditable = week.CanVote,
UserComments = await _commentRepository.GetCommentsForWeek((int) weekId)
UserComments = await _commentRepository.GetCommentsForWeek((int)weekId)
};
var meals = week.Meals;

var voteResults = await _mealVoteService.GetVoteResultsForWeekAsync(week);
foreach (var meal in meals)
{
model.MealsSelected[meal.Id] = meal.SelectedForOrder ?? false;
var voteResult = voteResults[meal.Id];

model.MealResults.Add(new MealResult
{
Meal = meal,
Votes = voteResult.Votes,
Score = voteResult.Score
Score = voteResult.Score
});
}

return View(model);
}

[HttpPost("Results/{weekId:int}")]
public async Task<IActionResult> ViewResults(int? weekId, [FromForm] ViewResultsViewModel model, string action)
{
if (weekId is 0 or null) return BadRequest();

foreach (var (key, value) in model.MealsSelected)
{
var dbModel = await _mealRepo.GetByIdAsync(key);
Expand All @@ -288,12 +296,12 @@ public async Task<IActionResult> ViewResults(int? weekId, [FromForm] ViewResults

if (action == "saveLock")
{
var week = await _mealWeekRepo.GetByIdAsync((int) weekId);
var week = await _mealWeekRepo.GetByIdAsync((int)weekId);
week.MealWeekStatus = MealWeekStatus.Past;
await _mealWeekRepo.UpdateAsync(week);
}
return RedirectToAction("ViewResults", new {weekId});

return RedirectToAction("ViewResults", new { weekId });
}

public class MealDetailsViewModel
Expand All @@ -306,15 +314,15 @@ public class MealDetailsViewModel
public async Task<ActionResult> MealDetailsModal(int? mealId)
{
if (mealId is 0 or null) return BadRequest();
var meal = await _mealRepo.GetByIdAsync((int) mealId);
var meal = await _mealRepo.GetByIdAsync((int)mealId);
if (meal == null) return BadRequest();

var previousRatings = _mealRatingRepo.GetPreviousRatingsForMeal(meal);

return PartialView("_MealDetailsModal", new MealDetailsViewModel
{
Meal = meal,
PreviousRatings = previousRatings
PreviousRatings = previousRatings
});
}

Expand All @@ -324,9 +332,9 @@ public async Task<ActionResult> Delete(int? weekId)
{
if (weekId is 0 or null) return BadRequest();

var week = await _mealWeekRepo.GetByIdWithMealsAsync((int) weekId);
var week = await _mealWeekRepo.GetByIdWithMealsAsync((int)weekId);
if (week == null) return BadRequest();

var meals = week.Meals;
var mealIds = meals.Select(x => x.Id).ToList();

Expand All @@ -335,7 +343,7 @@ public async Task<ActionResult> Delete(int? weekId)

var ratings = await _mealRatingRepo.ListAsync(x => mealIds.Contains(x.MealId));
await _mealRatingRepo.DeleteRangeAsync(ratings);

await _mealRepo.DeleteRangeAsync(meals);
await _mealWeekRepo.DeleteAsync(week);

Expand Down
2 changes: 1 addition & 1 deletion FoodPicker.Web/Views/Auth/Login.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<form method="post">
@{
var users = await UserManager.Users.Where(u => u.IsActive).ToListAsync();
var users = await UserManager.Users.Where(u => u.IsActive).OrderBy(u => u.Name).ToListAsync();
}
<div class="mb-3">
<label asp-for="UserId"></label>
Expand Down

0 comments on commit 0cbf71f

Please sign in to comment.