예제 #1
0
 public Artist GetArtistForEdit( int idArtist )
 {
     using( var ctx = new NwdBackOfficeContext() )
     {
         return ctx.Artists.Include( "Album" ).SingleOrDefault( a => a.Id == idArtist );
     }
 }
예제 #2
0
 public List<Artist> GetAllArtists()
 {
     using( var ctx = new NwdBackOfficeContext() )
     {
         return ctx.Artists.ToList();
     }
 }
예제 #3
0
        public AlbumViewModel GetAlbum( int idAlbum )
        {
            if( idAlbum == 0 )
            {
                throw new ArgumentException( "The id of the album must not be 0", "idAlbum" );
            }

            using( var ctx = new NwdBackOfficeContext() )
            {
                Album album = ctx.Albums.SingleOrDefault( a => a.Id == idAlbum );
                if( album != null )
                {
                    return new AlbumViewModel
                    {
                        AlbumName = album.Title,
                        CoverImageUrl = album.CoverImagePath,
                        Tracks = (from t in album.Tracks
                                  select new TrackViewModel
                                  {
                                      SongName = t.Song.Name,
                                      SongUrl = t.FileRelativePath,
                                      TrackNumber = t.Number
                                  }
                                ).ToArray()
                    };
                }
                return null;
            }
        }
        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();
            }
        }
예제 #5
0
 public bool ArtistExists( Artist artist )
 {
     if( artist == null ) throw new ArgumentException("Artist can't be null");
     using( var ctx = new NwdBackOfficeContext() )
     {
         return ctx.Artists.Any( a => a.Name == artist.Name );
     }
 }
예제 #6
0
 public bool AlbumExists( Album album )
 {
     if( album == null )
     {
         throw new ArgumentNullException( "album" );
     }
     using( var ctx = new NwdBackOfficeContext() )
     {
         return ctx.Albums.Any( a => a.Title == album.Title );
     }
 }
예제 #7
0
        public Artist CreateArtist( Artist artist, HttpServerUtilityBase server )
        {
            if( artist == null ) throw new ArgumentException( "Artist can't be null" );
            if( server == null ) throw new ArgumentException( "Server can't be null" );

            using( var ctx = new NwdBackOfficeContext() )
            {
                if( !ArtistExists( artist ) ) ctx.Artists.Add( artist );
                ctx.SaveChanges();
                return ctx.Artists.Single( a => a.Name == artist.Name );
            }
        }
        public void Database_Should_Always_Be_Created()
        {
            Database.SetInitializer(new DropCreateDatabaseAlways<NwdBackOfficeContext>());
            Database.SetInitializer(new DropCreateDatabaseAlways<NwdFrontOfficeContext>());

            using (var ctx = new NwdBackOfficeContext())
            {
                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);
            }
        }
예제 #9
0
        public static Album CreateAlbum(Album album, HttpServerUtilityBase server)
        {
            int i = 0;
            if (album == null)
            {
                throw new ArgumentNullException("album");
            }
            if (server == null)
            {
                throw new ArgumentNullException("server");
            }

            using (var ctx = new NwdBackOfficeContext())
            {
                album = ctx.Albums.Add(album);

                string directory;
                string physDirectory;
                EnsureDirectory(album, server, out directory, out physDirectory);

                foreach (var track in album.Tracks)
                {
                    track.Number = i;
                    i++;
                    HttpPostedFileBase file = track.File;
                    if (file == null) throw new ApplicationException("You must add a file to a track");

                    string fileName = SaveFile(physDirectory, file);
                    track.FileRelativePath = Path.Combine(directory, fileName);
                }

                if (album.CoverFile != null)
                {
                    string coverFileName = "cover.jpg";
                    string physPath = Path.Combine(physDirectory, coverFileName);
                    album.CoverFile.SaveAs(physPath);

                    album.CoverImagePath = Path.Combine(directory, coverFileName);
                }

                ctx.SaveChanges();
                return album;
            }
        }
예제 #10
0
 public Catalog GetCatalog()
 {
     using( var ctx = new NwdBackOfficeContext() )
     {
         return new Catalog
         {
             Albums = (from a in ctx.Albums
                       select new AlbumThumbnailViewModel
                       {
                           AlbumId = a.Id.Value,
                           AlbumName = a.Title,
                           ArtistName = a.Artist.Name,
                           CoverImageUrl = a.CoverImagePath,
                           TracksCount = a.Tracks.Count
                       }).ToArray(),
             NumberOfAlbum = ctx.Albums.Count()
         };
     }
 }
예제 #11
0
        public static Album EditAlbum(Album album, HttpServerUtilityBase server)
        {
            using (var ctx = new NwdBackOfficeContext())
            {
                album = ctx.Albums.Attach(album);
                ctx.Entry(album).Reference(e => e.Artist).Load();
                ctx.Entry(album).Collection(e => e.Tracks).Load();

                string directory;
                string physDirectory;
                EnsureDirectory(album, server, out directory, out physDirectory);

                foreach (var track in album.Tracks)
                {
                    HttpPostedFileBase file = track.File;
                    if (file != null)
                    {
                        //TODO delete previous file
                        string fileName = SaveFile(physDirectory, file);
                        track.FileRelativePath = Path.Combine(directory, fileName);
                    }

                    //else do not change the FileRelativePath since it is send by the form in an hidden input
                }

                if (album.CoverFile != null)
                {
                    string coverFileName = "cover.jpg";
                    string physPath = Path.Combine(physDirectory, coverFileName);
                    album.CoverFile.SaveAs(physPath);
                    album.CoverImagePath = Path.Combine(directory, coverFileName);
                }

                ctx.Entry(album).State = System.Data.EntityState.Modified;
                //foreach( var e in ctx.ChangeTracker.Entries() )
                //{
                //    e.State = System.Data.EntityState.Modified;
                //}
                ctx.SaveChanges();
                return album;
            }
        }
예제 #12
0
        public MiniPlayer GetMiniPlayerFor( int idAlbum, int trackNumber )
        {
            using( var ctx = new NwdBackOfficeContext() )
            {
                return
                    (
                        from a in ctx.Albums
                        let track = (from s in a.Tracks
                                     where s.AlbumId == a.Id && s.Number == trackNumber
                                     select s).FirstOrDefault()

                        where a.Id == idAlbum
                        select new MiniPlayer
                        {
                            SongFilePath = track.FileRelativePath
                        }
                    ).FirstOrDefault();

            }
        }
예제 #13
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);
            }
        }
예제 #14
0
        // POST api/track
        public async Task Post( [FromBody]TrackRegisterViewModel model )
        {
            Stream buffer = null;
            try
            {
                if( Request.Content.IsMimeMultipartContent() )
                {
                    var provider = await Request.Content.ReadAsMultipartAsync();
                    buffer = await provider.Contents[0].ReadAsStreamAsync();
                }

                if( buffer == null ) throw new ArgumentNullException();

                using( var ctx = new NwdBackOfficeContext() )
                {
                    int number = ctx.Albums.Where( a => a.Id == model.AlbumId ).FirstOrDefault().Tracks.Max( a => a.Number );
                    Track t = new Track { AlbumId = model.AlbumId, Duration = new TimeSpan( model.Duration ), Song = new Song { Composed = model.Song.Composed, Name = model.Song.Name }, Number = number };
                    ctx.Albums.SingleOrDefault( a => a.Id == model.AlbumId ).Tracks.Add( t );
                    ctx.SaveChanges();

                    string path = String.Format( "~/Private/{0}/{1}", model.AlbumId, t.AlbumId, t.Number );
                    t.FileRelativePath = path;

                    FileStream f = File.Create( HostingEnvironment.MapPath( path ) );
                    buffer.Seek( 0, SeekOrigin.Begin );
                    buffer.CopyTo( f );

                    f.Close();
                    ctx.SaveChanges();
                }
            }
            finally
            {
                buffer.Dispose();
            }
        }
예제 #15
0
 public List<Track> GetTrack( int idAlbum )
 {
     using( var ctx = new NwdBackOfficeContext() )
     {
         return ctx.Albums.Where( a => a.Id == idAlbum ).SelectMany( m => m.Tracks ).ToList();
     }
 }
예제 #16
0
 public List<Album> GetAllAlbums()
 {
     using( var ctx = new NwdBackOfficeContext() )
     {
         return ctx.Albums.ToList();
     }
 }
예제 #17
0
 public Album GetAlbumForEdit( int idAlbum )
 {
     using( var ctx = new NwdBackOfficeContext() )
     {
         return ctx.Albums.Include( "Tracks" ).Include( "Tracks.Song" ).Include( "Artist" ).SingleOrDefault( a => a.Id == idAlbum );
     }
 }