Beispiel #1
0
        protected override void AddMoreLogic(Playlist item)
        {
            UsersPlaylistsRepository usersPlaylistsRepo = new UsersPlaylistsRepository(Constants.UsersPlaylistsDirectory);
            UsersPlaylists           usersPlaylist      = new UsersPlaylists(AuthenticationService.LoggedUser.Id, item.Id);

            usersPlaylistsRepo.Save(usersPlaylist);
        }
Beispiel #2
0
        private void Share()
        {
            Console.Clear();
            Console.WriteLine("Share a playlist");
            PlaylistsRepository playlistsRepo = new PlaylistsRepository(Constants.PlaylistsDirectory);
            List <Playlist>     playlistsDb   = playlistsRepo.GetAll(playlist => playlist.ParentUserId == AuthenticationService.LoggedUser.Id);

            foreach (Playlist playlist in playlistsDb)
            {
                Console.WriteLine("Playlist ID: " + playlist.Id);
                Console.WriteLine("Playlist Name: " + playlist.Name);
                Console.WriteLine("Playlist Description: " + playlist.Description);
                Console.WriteLine("Playlist Is Public: " + playlist.IsPublic);
                Console.WriteLine("===============================================");
            }
            Console.Write("Playlist ID: ");
            int      playlistIdInput = Int32.Parse(Console.ReadLine());
            Playlist playlistDb      = playlistsDb.FirstOrDefault(playlist => playlist.Id == playlistIdInput);

            if (playlistDb == null)
            {
                Console.WriteLine("Cannot find playlist");
                Console.ReadKey(true);
                return;
            }
            Console.Clear();
            UsersRepository usersRepo = new UsersRepository(Constants.UsersDirectory);
            List <User>     usersDb   = usersRepo.GetAll(user => user.Id != AuthenticationService.LoggedUser.Id);

            foreach (User user in usersDb)
            {
                Console.WriteLine("User ID: " + user.Id);
                Console.WriteLine("User Username: "******"User Email: " + user.Email);
                Console.WriteLine("============================================");
            }

            Console.Write("User ID: ");
            int userIdInput = Int32.Parse(Console.ReadLine());

            Console.Clear();
            User userDb = usersDb.FirstOrDefault(user => user.Id == userIdInput);

            if (userDb == null)
            {
                Console.WriteLine("Cannot find user");
                Console.ReadKey(true);
                return;
            }
            UsersPlaylistsRepository usersPlaylistsRepository = new UsersPlaylistsRepository(Constants.UsersPlaylistsDirectory);
            UsersPlaylists           usersPlaylists           = new UsersPlaylists(userDb.Id, playlistDb.Id);

            usersPlaylistsRepository.Save(usersPlaylists);
            Console.WriteLine("Playlist shared successfully.");
            Console.ReadKey(true);
        }
        public void SharePlaylist() //TODO: Ensure there are checks if playlist is already shared.
        {
            UsersRepository usersRepo     = new UsersRepository(Constants.UsersPath);
            int             currentUserId = AuthenticationService.LoggedUser.Id;
            List <User>     users         = usersRepo.GetAll(u => u.IsAdministrator != true && u.Id != currentUserId);

            Console.Clear();
            if (users.Count == 0)
            {
                Console.WriteLine("There are currently no users with whom you can share a playlist.");
                Console.ReadKey(true);
                return;
            }
            foreach (User user in users)
            {
                Console.WriteLine("**************************");
                Console.WriteLine("Id: {0}", user.Id);
                Console.WriteLine("Name: {0}", user.DisplayName);
                Console.WriteLine("**************************");
            }

            Console.WriteLine();
            Console.Write("Enter Id of user with whom you will share playlist: ");
            int  userId  = 0;
            bool isIntId = int.TryParse(Console.ReadLine(), out userId);

            while (isIntId == false)
            {
                Console.WriteLine("User Id can only be an integer number. Try again!!");
                Console.ReadKey(true);
                Console.Write("Enter Id of user with whom you will share playlist: ");
                isIntId = int.TryParse(Console.ReadLine(), out userId);
            }

            bool isValidUser = false;

            foreach (User user in users)
            {
                if (user.Id == userId)
                {
                    isValidUser = true;
                    break;
                }
            }

            if (isValidUser == false)
            {
                Console.WriteLine("You cannot share a playlist with that user!");
                Console.ReadKey(true);
                return;
            }

            PlaylistsRepository      playlistsRepo          = new PlaylistsRepository(Constants.PlaylistsPath);
            UsersPlaylistsRepository usersPlaylistsRepo     = new UsersPlaylistsRepository(Constants.UsersPlaylistsPath);
            List <UsersPlaylists>    usersPlaylistsEntities = usersPlaylistsRepo.GetAll(upe => upe.UserId == currentUserId);
            List <Playlist>          playlists = new List <Playlist>();

            foreach (UsersPlaylists usersPlaylistsEntity in usersPlaylistsEntities)
            {
                Playlist playlist = playlistsRepo.GetAll(p => p.Id == usersPlaylistsEntity.PlaylistId).FirstOrDefault();
                playlists.Add(playlist);
            }

            Console.Clear();
            foreach (Playlist playlist in playlists)
            {
                Console.WriteLine("****************************");
                Console.WriteLine("Id: {0}", playlist.Id);
                Console.WriteLine("Name: {0}", playlist.Name);
                Console.WriteLine("Description: {0}", playlist.Description);
                Console.WriteLine("****************************");
            }

            Console.WriteLine();
            Console.Write("Enter id of playlist to share: ");
            isIntId = false;
            int playlistShareId = 0;

            isIntId = int.TryParse(Console.ReadLine(), out playlistShareId);
            while (isIntId == false)
            {
                Console.WriteLine("Id can only be an integer number. Try again!!");
                Console.ReadKey(true);
                Console.Write("Enter id of playlist to share: ");
                isIntId = int.TryParse(Console.ReadLine(), out playlistShareId);
            }

            bool isExistingPlaylist = usersPlaylistsRepo.EntityExists(upe => upe.PlaylistId == playlistShareId && upe.UserId == currentUserId);

            if (isExistingPlaylist == false)
            {
                Console.WriteLine("No playlist with id {0} exists or you have no rights to share it!");
                Console.ReadKey(true);
                return;
            }

            UsersPlaylists newUsersPlaylistsEntity = new UsersPlaylists(); //TODO: Enforce sharing restrictions with checks.

            newUsersPlaylistsEntity.PlaylistId = playlistShareId;
            newUsersPlaylistsEntity.UserId     = userId;
            usersPlaylistsRepo.Save(newUsersPlaylistsEntity);
            Console.WriteLine("Playlist shared successfully!");
            Console.ReadKey(true);
        }
        public void AddPlaylist()
        {
            string inputPlaylistName;
            bool   isEmptyName = false;

            do //TODO: Refactore all do while loops with a simple while loop where possible (see bookmarked while loop for example)
            {
                Console.Clear();
                Console.Write("Enter playlist name: ");
                inputPlaylistName = Console.ReadLine();
                isEmptyName       = string.IsNullOrWhiteSpace(inputPlaylistName);
                if (isEmptyName)
                {
                    Console.WriteLine("Playlist name cannot be empty. Try again!");
                    Console.ReadKey(true);
                }
            } while (isEmptyName == true);

            Playlist newPlaylist = new Playlist();

            newPlaylist.Name = inputPlaylistName;

            Console.Write("Enter playlist description: ");
            newPlaylist.Description = Console.ReadLine();

            bool   isIncorrectInput;
            string isPublic;

            do
            {
                isIncorrectInput = false;
                Console.Write("New playlist will be public(yes/no): ");
                isPublic = Console.ReadLine();
                if (!(isPublic.ToLower() == "yes" ^ isPublic.ToLower() == "no"))
                {
                    isIncorrectInput = true;
                }

                if (isIncorrectInput == true)
                {
                    Console.WriteLine("You can only enter \"yes\" and \"no\". Try again!");
                    Console.ReadKey(true);
                }
            } while (isIncorrectInput == true);

            if (isPublic == "yes")
            {
                newPlaylist.IsPublic = true;
            }
            else
            {
                newPlaylist.IsPublic = false;
            }

            PlaylistsRepository playlistsRepo   = new PlaylistsRepository(Constants.PlaylistsPath);
            int            playlistId           = playlistsRepo.Save(newPlaylist);
            int            currentUserId        = AuthenticationService.LoggedUser.Id;
            UsersPlaylists usersPlaylistsEntity = new UsersPlaylists();

            usersPlaylistsEntity.PlaylistId = playlistId;
            usersPlaylistsEntity.UserId     = currentUserId;
            UsersPlaylistsRepository usersPlaylistsRepo = new UsersPlaylistsRepository(Constants.UsersPlaylistsPath);

            usersPlaylistsRepo.Save(usersPlaylistsEntity);
            Console.WriteLine("Playlist added successfuly!");
            Console.ReadKey(true);
        }