public IHttpResponse Index() { var user = this.Db.Users.FirstOrDefault(u => u.Username == this.User.Username); if (user == null) { return(this.View()); } var yourChannels = this.Db.Channels.Where(x => x.Followers.Any(f => f.User.Username == this.User.Username)).Select(c => new ChannelViewModel() { Id = c.Id, Name = c.Name, Type = c.Type, FollowersCount = c.Followers.Count() }).ToList(); var myTags = this.Db.Channels .Where(x => x.Followers.Any(f => f.User.Username == this.User.Username)) .SelectMany(x => x.Tags.Select(t => t.TagId)).ToList(); var suggested = this.Db.Channels.Where(c => c.Followers.All(i => i.User.Username != this.User.Username) && c.Tags.Any(t => myTags.Contains(t.TagId))).Select(ch => new ChannelViewModel() { Id = ch.Id, Name = ch.Name, Type = ch.Type, FollowersCount = ch.Followers.Count() }).ToList(); var ids = yourChannels.Select(x => x.Id).ToList(); ids = ids.Concat(suggested.Select(y => y.Id).ToList()).ToList(); ids = ids.Distinct().ToList(); var other = this.Db.Channels.Where(c => !ids.Contains(c.Id)).Select(ch => new ChannelViewModel() { Id = ch.Id, Name = ch.Name, Type = ch.Type, FollowersCount = ch.Followers.Count() }).ToList(); var homeTestViewModel = new HomeTestViewModel() { Username = this.User.Username, YourChannels = yourChannels, Suggested = suggested, SeeOther = other, UserRole = this.User.Role }; return(this.View("Home/HomeTest", homeTestViewModel)); }
public ActionResult Index() { //This is the view model to include all of the details requested. //If I had more time I would recommend the details to be loaded via a Web API, shortening loading time and freeing up the users ui var vm = new HomeTestViewModel { PaidInvoicesNumber = _context.Invoices.Where(i => i.IsPaid).Count(), PaidInvoicesValue = _context.Invoices.Where(i => i.IsPaid).AsEnumerable().Aggregate(0M, (acc, inv) => acc + inv.Value), Customers = _context.Customers.Select(c => new HomeCustomer { Customer = c, PaidInvoices = _context.Invoices.OrderBy(i => i.InvoiceDate).Where(i => i.CustomerId == c.Id && c.Id == i.CustomerId).ToList(), OutstandingInvoices = _context.Invoices.Where(i => !i.IsPaid && c.Id == i.CustomerId).ToList(), MostRecentInvoice = _context.Invoices.OrderBy(i => i.InvoiceDate).FirstOrDefault(i => i.CustomerId == c.Id), OutstandingNum = _context.Invoices.Where(i => !i.IsPaid && c.Id == i.CustomerId).Count(), }).ToList() }; return(View(vm)); }