public async Task<bool> LoadChildrenAsync(AlbumViewerContext ctx)
        {
            Artist = await ctx.Artists.SingleOrDefaultAsync(art => art.Id == ArtistId);
            Tracks = await ctx.Tracks.Where(tk => tk.AlbumId == Id).ToListAsync();

            return true;
        }
    public AccountController(AlbumViewerContext ctx, 
        IServiceProvider svcProvider)
        //SignInManager<User> signInManager)
    {
        context = ctx;
        serviceProvider = svcProvider;
 
    }
Exemple #3
0
        public async Task <bool> LoadChildrenAsync(AlbumViewerContext ctx)
        {
            Artist = await ctx.Artists.SingleOrDefaultAsync(art => art.Id == ArtistId);

            Tracks = await ctx.Tracks.Where(tk => tk.AlbumId == Id).ToListAsync();

            return(true);
        }
        /// <summary>
        /// Imports data from json
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static int ImportFromJson(AlbumViewerContext context, string json)
        {
            // data exists
            if (context.Albums.Any())
                return 1;

            var albums = JsonConvert.DeserializeObject<Album[]>(json);

            foreach (var album in albums)
            {
                // clear out primary/identity keys so insert works
                album.Id = 0;
                album.ArtistId = 0;
                album.Artist.Id = 0;

                var existingArtist = context.Artists
                                            .FirstOrDefault(a => a.ArtistName == album.Artist.ArtistName);
                if (existingArtist == null)
                {
                    context.Artists.Add(album.Artist);
                }
                else
                {
                    album.Artist = existingArtist;
                    album.ArtistId = existingArtist.Id;
                }

                if (album.Tracks != null)
                {
                    foreach (var track in album.Tracks)
                    {
                        track.Id = 0;
                        context.Tracks.Add(track);
                    }
                }
                context.Albums.Add(album);

                try
                {
                    context.SaveChanges();
                }
                catch
                {
                    Console.WriteLine("Error adding: " + album.ArtistId);
                }
            }

            var user = new User()
            {
                Username = "******",
                Password = "******",
                Fullname = "Test User",
            };
            context.Users.Add(user);
            context.SaveChanges();

            return 1;
        }
Exemple #5
0
        /// <summary>
        /// Imports data from json
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static int ImportFromJson(AlbumViewerContext context, string json)
        {
            var albums = JsonConvert.DeserializeObject <Album[]>(json);

            foreach (var album in albums)
            {
                // clear out primary/identity keys so insert works
                album.Id        = 0;
                album.ArtistId  = 0;
                album.Artist.Id = 0;

                var existingArtist = context.Artists.Where(a => a.ArtistName == album.Artist.ArtistName).FirstOrDefault();
                if (existingArtist == null)
                {
                    context.Artists.Add(album.Artist);
                }
                else
                {
                    album.Artist   = existingArtist;
                    album.ArtistId = existingArtist.Id;
                }

                if (album.Tracks != null)
                {
                    foreach (var track in album.Tracks)
                    {
                        track.Id = 0;
                        context.Add(track);
                    }
                }
                context.Add(album);

                try
                {
                    context.SaveChanges();
                }
                catch
                {
                    Console.WriteLine("Error adding: " + album.ArtistId);
                }
            }

            var user = new User()
            {
                Username = "******",
                Password = "******",
                Fullname = "Test User",
            };

            context.Users.Add(user);
            context.SaveChanges();

            return(1);
        }
        public static bool EnsureAlbumData(AlbumViewerContext context, string jsonDataFilePath)
        {
            bool hasData = false;
            try
            {
                hasData = context.Albums.Any();
            }
            catch (Exception)
            {
                context.Database.EnsureCreated(); // just create the schema as is no migrations
                hasData = context.Albums.Any();
            }

            if (!hasData)
            {
                string json = System.IO.File.ReadAllText(jsonDataFilePath);
                return ImportFromJson(context, json) > 0;
            }

            return true;
        }
Exemple #7
0
        public static bool EnsureAlbumData(AlbumViewerContext context, string jsonDataFilePath)
        {
            bool hasData = false;

            try
            {
                hasData = context.Albums.Any();
            }
            catch
            {
                context.Database.EnsureCreated(); // just create the schema - no migrations
                hasData = context.Albums.Any();
            }


            if (!hasData)
            {
                string json = System.IO.File.ReadAllText(jsonDataFilePath);
                return(ImportFromJson(context, json) > 0);
            }


            return(true);
        }
 public AlbumViewerApiController(AlbumViewerContext ctx, IServiceProvider svcProvider)
 {
     context = ctx;
     serviceProvider = svcProvider;
 }
 public async Task<bool> LoadTracksAsync(AlbumViewerContext ctx)
 {
     Tracks = await ctx.Tracks.Where(tk => tk.AlbumId == Id).ToListAsync();
     return true;
 }
 public async Task<bool> LoadArtistAsync(AlbumViewerContext ctx)
 {
     Artist = await ctx.Artists.FirstOrDefaultAsync(art => art.Id == ArtistId);
     return true;
 }
Exemple #11
0
        public async Task <bool> LoadTracksAsync(AlbumViewerContext ctx)
        {
            Tracks = await ctx.Tracks.Where(tk => tk.AlbumId == Id).ToListAsync();

            return(true);
        }
Exemple #12
0
        public async Task <bool> LoadArtistAsync(AlbumViewerContext ctx)
        {
            Artist = await ctx.Artists.FirstOrDefaultAsync(art => art.Id == ArtistId);

            return(true);
        }
 public AlbumViewerMvcController(AlbumViewerContext ctx, IServiceProvider svcProvider)
 {
     context = ctx;
     serviceProvider = svcProvider;
     //this.environment = environment;
 }
        public async Task<IEnumerable> Artists()
        {
            // For demonstration use a manually instantiated instance of
            // the dbContext.
            //
            // Note the need to pass in serviceProvider or *something*
            // that gives access to the Configuration so the connection
            // string from config can be found.
            // 
            // This overload is what EF uses internally to create a
            // to create a configured instance of a context.
            //
            // Scoped instances of Context can be problematic in some
            // controllers - you're incurring overhead for each hit even
            // if context is isn't used in an action. If you need multiple instances
            // of contexts you may also need to manually instantiate. 
            using (var ctxt = new AlbumViewerContext(serviceProvider))
            {
                var result = await ctxt.Artists
                    .OrderBy(art => art.ArtistName)
                    .Select(art => new
                    {
                        art.ArtistName,
                        art.Description,
                        art.ImageUrl,
                        art.Id,
                        AlbumCount = context.Albums.Count(alb => alb.ArtistId == art.Id)
                    })
                    .ToListAsync();

                return result;
            }
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, AlbumViewerContext albumContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                loggerFactory.AddConsole(Configuration.GetSection("Logging"));
                loggerFactory.AddDebug();
            }
            else
            {
                app.UseExceptionHandler(errorApp =>

                    // Application level exception handler here - this is just a place holder
                    errorApp.Run(async (context) =>
                    {
                        context.Response.StatusCode = 500;
                        context.Response.ContentType = "text/html";
                        await context.Response.WriteAsync("<html><body>\r\n");
                        await
                            context.Response.WriteAsync(
                                "We're sorry, we encountered an un-expected issue with your application.<br>\r\n");

                        // Capture the exception
                        var error = context.Features.Get<IExceptionHandlerFeature>();
                        if (error != null)
                        {
                            // This error would not normally be exposed to the client
                            await
                                context.Response.WriteAsync("<br>Error: " +
                                                            HtmlEncoder.Default.Encode(error.Error.Message) + "<br>\r\n");
                        }
                        await context.Response.WriteAsync("<br><a href=\"/\">Home</a><br>\r\n");
                        await context.Response.WriteAsync("</body></html>\r\n");
                        await context.Response.WriteAsync(new string(' ', 512)); // Padding for IE
                    }));

                //app.UseExceptionHandler("/");
                //app.UseExceptionHandler("/Home/Error");
            }

            // Enable Cookie Auth with automatic user policy
            app.UseCookieAuthentication(new CookieAuthenticationOptions()
            {
                AutomaticAuthenticate = true,
                AutomaticChallenge = true,
                LoginPath = "/api/login"
            });

            app.UseDatabaseErrorPage();
            app.UseStatusCodePages();

            app.UseDefaultFiles(); // so index.html is not required
            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            bool result = AlbumViewerDataImporter.EnsureAlbumData(albumContext,
                           Path.Combine(env.WebRootPath, "data//albums.js"));
        }