[1/14] Become a Full-stack .NET Developer: Implementing Secondary Use Cases
In this final module we completed a few secondary uses cases and in the process the first iteration of our GigHub app. From this we learned a few new concepts such as partial views, hiding certain things for anonymous users and refactoring our app using the DRY (Don't Repeat Yourself) principle.
One use case that needed to be implemented was upon clicking on the Follow button beside an artists name, that it would then seamlessly change to a Following button to indicate a user was now following that particular artist. The relationship between Follow and Following was created by adding a class to our Models folder called Following which had a composite key which was a combination of Followers and Followees (instances of the ApplicationUser class).
Within the ApplicationUser class, Followers and Followees were added as collection properties and initialised in a constructor. Fluent API was used to define this realtionship and to prevent multiple cascade deletes.
public ICollection<Following> Followers { get; set; } public ICollection<Following> Followees { get; set; } public ApplicationUser() { Followers = new Collection<Following>(); Followees = new Collection<Following>(); }
We then proceeded to hide the Follow and Going? buttons to anonymous users as they do not need to seem them unless logged in. The ViewModel for this view was appended with a show actions attribute that checks to see if a user is authenticated using ASP.NET Identity. This new WiewModel was then passed into the view and then we iterated over the list of gigs checking to see if the user was logged in and if so displaying the Follow and Going? links.
To finish off this first app iteration we needed to make a page that lists all the gigs a user is attending. Within the GigsController we created a new Attending method that would make this list.
[Authorize] public ActionResult Attending() { var userId = User.Identity.GetUSerId(); var gigs = _context.Attendances .Where(a => a.AttendeeId == userId) .Select(a => a.Gig) .ToList(); return View(gigs); }
We then manually added the view (called AttendingView) for this controller action.