示例#1
0
        protected override void DeleteMoreLogic(int id)
        {
            PlaylistsSongsRepository playlistsSongsRepo = new PlaylistsSongsRepository(Constants.PlaylistsSongsDirectory);

            playlistsSongsRepo.Delete(playlistSong => playlistSong.PlaylistId == id);
            UsersPlaylistsRepository usersPlaylistsRepo = new UsersPlaylistsRepository(Constants.UsersPlaylistsDirectory);

            usersPlaylistsRepo.Delete(usersPlaylist => usersPlaylist.PlaylistId == id);
        }
        public void DeletePlaylist()
        {
            ViewUserPlaylists();
            Console.WriteLine();
            Console.Write("Enter id to delete: ");
            int  deleteId      = 0;
            int  currentUserId = AuthenticationService.LoggedUser.Id;
            bool isIntId       = int.TryParse(Console.ReadLine(), out deleteId);

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

            UsersPlaylistsRepository usersPlaylistsRepo   = new UsersPlaylistsRepository(Constants.UsersPlaylistsPath);
            PlaylistsRepository      playlistsRepo        = new PlaylistsRepository(Constants.PlaylistsPath);
            UsersPlaylists           usersPlaylistsEntity = usersPlaylistsRepo.GetAll(upe => upe.PlaylistId == deleteId && upe.UserId == currentUserId)
                                                            .FirstOrDefault();

            if (usersPlaylistsEntity == null)
            {
                Console.WriteLine("Playlist with id {0} does not exist or you have no rights to delete!", deleteId);
                Console.ReadKey();
                return;
            }

            PlaylistsSongsRepository playlistsSongsRepo     = new PlaylistsSongsRepository(Constants.PlaylistsSongsPath);
            List <PlaylistsSongs>    playlistsSongsEntities = playlistsSongsRepo.GetAll(pse => pse.PlaylistId == deleteId);

            foreach (PlaylistsSongs playlistsSongsEntity in playlistsSongsEntities)
            {
                playlistsSongsRepo.Delete(playlistsSongsEntity);
            }

            Playlist playlistToDelete = playlistsRepo.GetAll(p => p.Id == deleteId).FirstOrDefault();

            usersPlaylistsRepo.Delete(usersPlaylistsEntity);
            playlistsRepo.Delete(playlistToDelete);
            Console.WriteLine("Playlist successfully deleted!");
            Console.ReadKey(true);
        }