Exemplo n.º 1
0
        // GET: /Gallery/
        public async Task <IActionResult> Index(List <int> tags)
        {
            IEnumerable <GalleryEntry> galleryEntries = await _galleryRepository.GetGalleryEntries();

            galleryEntries = galleryEntries.Where(galleryEntry =>
                                                  tags.All(tag => galleryEntry.Tags.Select(t => t.TagID).Contains(tag)));

            ApplicationUser user = await _userManager.GetUserAsync(User);

            // loop over each gallery entry and add any computed fields
            foreach (GalleryEntry galleryEntry in galleryEntries)
            {
                // setting the thumbnail image
                galleryEntry.Thumbnail = _galleryRepository.ComputeThumbnail(galleryEntry);
                // seting the gallery entry number of likes
                galleryEntry.Likes = await _galleryRepository.ComputeNLikes(galleryEntry);

                // Boolean has the picture been favorited by this user already

                if (user != null)
                {
                    galleryEntry.Faved = _galleryRepository.isFavorited(galleryEntry.GalleryEntryID, user.Id);
                }
                if (tags.Count > 0)
                {
                    galleryEntry.Filter = tags.All(tag => galleryEntry.Tags.Select(t => t.TagID).Contains(tag));
                }
                else
                {
                    galleryEntry.Filter = false;
                }
            }
            galleryEntries = galleryEntries.OrderByDescending(galleryEntry => galleryEntry.Likes);

            // fetch tags from the database
            IEnumerable <Tag> database_tags = await _tagRepository.GetTags();

            // Transform list of tags into a list of tuples with the id, name, and bool to see if it should be checked
            IEnumerable <Tuple <int, string, bool> > tupled_database_tags = database_tags
                                                                            .Select(tag => Tuple.Create(tag.TagID, tag.Name, tags.Contains(tag.TagID)));

            // pass list of tuples to the ViewData for the View to use
            ViewData["tags"] = tupled_database_tags;
            bool logged_in = user != null;

            ViewData["logged_in"] = logged_in;

            return(View(galleryEntries));
        }