Ejemplo n.º 1
0
        public async Task <ActionResult <UserSongsViewModel> > GetUserSongs(int usrID)
        {
            var userSongs = new UserSongsViewModel {
                User = _context.Users.Where(_ => _.UserId == usrID).FirstOrDefault()
            };

            foreach (var prch in _context.Purchases)
            {
                if (prch.UserId == userSongs.User.UserId)
                {
                    userSongs.Purchases.Add(prch);
                }
            }

            foreach (var sng in _context.Songs)
            {
                foreach (var userPrchs in userSongs.Purchases)
                {
                    if (userPrchs.SongId == sng.SongId)
                    {
                        userSongs.Songs.Add(sng);
                    }
                }
            }

            return(userSongs);
        }
Ejemplo n.º 2
0
        // GET: User/SongsEdit
        public ActionResult SongsEdit(string id, int page = 1, string search = "")
        {
            if (id == null)
            {
                this.AddNotification("Invalid user id!", NotificationType.ERROR);
                return(Redirect("/User/List"));
            }

            var songsPerPage = 5;

            using (var db = new ApplicationDbContext())
            {
                var ids = db.Users.Select(i => i.Id).ToList();
                if (!ids.Contains(id))
                {
                    this.AddNotification("User not found!", NotificationType.ERROR);
                    return(Redirect("/User/List"));
                }

                var user = db.Users.Find(id); // Find user

                var userSongs = new List <Song>();

                if (search == "")
                {
                    userSongs = db.Songs.Where(s => s.Uploader.Id == user.Id)
                                .OrderBy(s => s.UploadDate)
                                .Skip((page - 1) * songsPerPage)
                                .Take(songsPerPage)
                                .ToList();
                }
                else
                {
                    userSongs = db.Songs.Where(s => s.Uploader.Id == user.Id)
                                .OrderBy(s => s.UploadDate)
                                .Where(s => s.Artist.ToLower().Contains(search.ToLower()) ||
                                       s.Title.ToLower().Contains(search.ToLower()))
                                .Skip((page - 1) * songsPerPage)
                                .Take(songsPerPage)
                                .ToList();
                }

                ViewBag.CurrPage        = page;
                ViewBag.TotalPages      = search == "" ? Math.Ceiling((double)db.Songs.ToList().Count / songsPerPage) : Math.Ceiling((double)userSongs.Count / songsPerPage);
                ViewBag.SongsPerPage    = songsPerPage;
                ViewBag.UserId          = user.Id;
                ViewBag.TotalSongsFound = db.Songs
                                          .Where(s => s.Uploader.Id == user.Id)
                                          .ToList()
                                          .Where(s => s.Artist.ToLower().Contains(search.ToLower()) ||
                                                 s.Title.ToLower().Contains(search.ToLower()))
                                          .ToList()
                                          .Count;

                var viewModel = new UserSongsViewModel();
                viewModel.Songs    = userSongs;
                viewModel.Uploader = db.Users.Where(i => i.Id == id).Select(u => u.FullName).FirstOrDefault();

                if (userSongs.Count == 0)
                {
                    this.AddNotification($"User '{user.FullName}' has not uploaded any songs!", NotificationType.INFO);
                }

                return(View(viewModel));
            }
        }