public void AddTrackToPlaylist( string playlistName, string songName, string songUrl )
        {
            if( String.IsNullOrWhiteSpace( playlistName ) ) throw new ArgumentNullException();
            if( String.IsNullOrWhiteSpace( songName ) ) throw new ArgumentNullException();
            if( String.IsNullOrWhiteSpace( songUrl ) ) throw new ArgumentNullException();

            var httpContext = HttpContext.Current;
            using( var ctx = new NwdFrontOfficeContext() )
            {
                var user = ctx.UserInfo.SingleOrDefault( u => u.UserName == httpContext.User.Identity.Name );
                if( user == null )
                {
                    user = ctx.UserInfo.Add( new User
                    {
                        UserName = httpContext.User.Identity.Name
                    } );
                }

                var playList = user.Playlists.SingleOrDefault( p => p.Title == playlistName );
                if( playList == null )
                {
                    throw new ApplicationException( "The playlist does not exists. Please valid this before calling this method." );
                }
                playList.Tracks.Add( new PlaylistTrack
                {
                    SongName = songName,
                    SongUrl = songUrl
                } );

                ctx.SaveChanges();
            }
        }
        public void Database_Should_Always_Be_Created()
        {
            Database.SetInitializer( new DropCreateDatabaseAlways<NwdBackOfficeContext>() );
            Database.SetInitializer( new DropCreateDatabaseAlways<NwdFrontOfficeContext>() );
            Database.SetInitializer( new DropCreateDatabaseAlways<NwdAuthContext>() );

            using( var b = new NwdBackOfficeContext() )
            {
                b.Database.Initialize( true );
                Debug.Assert( b.Database.Exists() );
                Console.WriteLine( b.Database.Connection.ConnectionString );
            }
            using( var f = new NwdFrontOfficeContext() )
            {
                f.Database.Initialize( true );
                Debug.Assert( f.Database.Exists() );
                Console.WriteLine( f.Database.Connection.ConnectionString );
            }
            using( var a = new NwdAuthContext() )
            {
                a.Database.Initialize( true );
                Debug.Assert( a.Database.Exists() );
                Console.WriteLine( a.Database.Connection.ConnectionString );
                a.Roles.Add( new Role { RoleName = "User" } );
                Role r = a.Roles.Add( new Role { RoleName = "Administrator" } );
                Nwd.Authentication.Model.User u = a.Users.Add( new Nwd.Authentication.Model.User { Username = "******", Name = "NwdProvider", Comment = "user admin", IsApproved = true, IsLockedOut = false, Password = AuthenticationUtils.HashPassword( "test" ), CreationDate = DateTime.UtcNow } );
                u.Roles.Add( r );

                a.SaveChanges();
            }
        }
        public Playlist AddNewPlaylistToCurrentUser( string playlistName )
        {
            if( String.IsNullOrWhiteSpace( playlistName ) ) throw new ArgumentNullException();

            var httpContext = HttpContext.Current;
            if( !httpContext.User.Identity.IsAuthenticated )
            {
                throw new UnauthorizedAccessException( "You must be authenticated in order to create a new playlist" );
            }
            using( var ctx = new NwdFrontOfficeContext() )
            {
                var user = ctx.UserInfo.SingleOrDefault( u => u.UserName == httpContext.User.Identity.Name );
                if( user == null )
                {
                    user = ctx.UserInfo.Add( new User
                    {
                        UserName = httpContext.User.Identity.Name
                    } );
                }
                Playlist p = new Playlist
                {
                    Title = playlistName
                };
                user.Playlists.Add( p );

                ctx.SaveChanges();
                return ctx.Playlists.Where( m => m.Title == playlistName ).FirstOrDefault();
            }
        }
 public void DeleteAPlaylist( string playlistName )
 {
     using( var ctx = new NwdFrontOfficeContext() )
     {
         Playlist p = ctx.Playlists.Where( m => m.Title == playlistName ).FirstOrDefault();
         ctx.Playlists.Remove( p );
         ctx.SaveChanges();
     }
 }
 public IEnumerable<PlaylistTrack> Get( int id )
 {
     using (var ctx = new NwdFrontOfficeContext())
     {
         var u = ctx.Playlists.Where( a => a.Id == id ).SelectMany( m => m.Tracks ).ToList();
         if( u != null ) return u;
         return new List<PlaylistTrack>();
     }
 }
 public bool Delete( [FromBody]PlaylistRequest model )
 {
     using (var ctx = new NwdFrontOfficeContext())
     {
         Playlist p = ctx.Playlists.FirstOrDefault( a => a.Title == model.PlaylistName );
         ctx.Playlists.Remove( p );
         ctx.SaveChanges();
         return true;
     }
 }
        public void Database_Should_Always_Be_Created()
        {
            Database.SetInitializer(new DropCreateDatabaseAlways<NwdMusikEntities>());
            Database.SetInitializer( new DropCreateDatabaseAlways<NwdFrontOfficeContext>() );

            using (var ctx = new NwdMusikEntities())
            {
                ctx.Database.Initialize(true);
                Assert.That(ctx.Database.Exists());
                Console.WriteLine(ctx.Database.Connection.ConnectionString);
            }
            using( var ctx = new NwdFrontOfficeContext() )
            {
                ctx.Database.Initialize( true );
                Assert.That( ctx.Database.Exists() );
                Console.WriteLine( ctx.Database.Connection.ConnectionString );
            }
        }
Example #8
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<NwdBackOfficeContext>());
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<NwdFrontOfficeContext>());

            using (var ctx = new NwdBackOfficeContext())
            {
                ctx.Database.Initialize(true);
            }
            using (var ctx = new NwdFrontOfficeContext())
            {
                ctx.Database.Initialize(true);
            }
        }
 public IEnumerable<Playlist> GetAllPlaylist()
 {
     using( var ctx = new NwdFrontOfficeContext() )
     {
         return ctx.UserInfo.Where(u => u.UserName == HttpContext.Current.User.Identity.Name).SelectMany(m => m.Playlists).ToList();
     }
 }