Пример #1
0
        public async Task <IActionResult> Edit(int id, [Bind("AlbumID,Title,ArtistID,GenreID,Likes")] Album album)
        {
            if (id != album.AlbumID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(album);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AlbumExists(album.AlbumID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            ViewData["ArtistID"] = new SelectList(_context.Artists, "ArtistID", "Name", album.ArtistID);
            ViewData["GenreID"]  = new SelectList(_context.Genres, "GenreID", "Name", album.GenreID);
            return(View(album));
        }
Пример #2
0
        public async Task <IActionResult> Put(int id, [FromBody] Song value)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            //check id exists in the database
            var result = _context.Songs.FirstOrDefault(x => x.ID == id);

            if (result != null)
            {
                //result.ID = id;
                result.Name   = value.Name;
                result.Album  = value.Album;
                result.Artist = value.Artist;
                _context.Update(result);
                await _context.SaveChangesAsync();
            }
            else
            {
                await Post(value);
            }

            return(Ok());
        }
        public async Task <IActionResult> Edit(int id, [Bind("Id,FirstName,LastName,Age,MainStyle")] Performer performer)
        {
            if (id != performer.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(performer);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PerformerExists(performer.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(performer));
        }
Пример #4
0
        public async Task <IActionResult> Edit(int id, [Bind("ArtistID,Name,Bio")] Artist artist)
        {
            if (id != artist.ArtistID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(artist);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ArtistExists(artist.ArtistID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(artist));
        }
Пример #5
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Url,Length,AlbumId")] Song song)
        {
            if (id != song.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(song);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SongExists(song.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(song));
        }
        public async Task <IActionResult> Edit(int id, [Bind("GenreID,Name")] Genre genre)
        {
            if (id != genre.GenreID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(genre);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!GenreExists(genre.GenreID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(genre));
        }
Пример #7
0
        public async Task <IActionResult> Edit(int id, [Bind("RatingID,AlbumID,Username,Stars")] Rating rating)
        {
            if (id != rating.RatingID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(rating);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RatingExists(rating.RatingID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(rating));
        }
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,CreationDate,Location,Country")] Publisher publisher)
        {
            if (id != publisher.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(publisher);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PublisherExists(publisher.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(publisher));
        }
Пример #9
0
 public IActionResult Edit(Artist artist)
 {
     if (ModelState.IsValid)
     {
         db.Update(artist);
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(artist));
 }
Пример #10
0
        public async Task <IActionResult> Update(RegisterViewModel registerVM, string returnUrl = null)
        {
            var currentUser = await _userManager.GetUserAsync(User);

            currentUser.GenreID = registerVM.GenreID;

            _context.Update(currentUser);
            await _context.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
Пример #11
0
 public ActionResult Edit(Album album)
 {
     if (ModelState.IsValid)
     {
         db.Update(album);
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.Artists = new SelectList(db.Artists.ToList(), "ArtistID", "Name");
     ViewBag.Genres  = new SelectList(db.Genres.ToList(), "GenreID", "Name");
     return(View(album));
 }
Пример #12
0
        public async Task <IActionResult> Edit(int id, [Bind("GenreID, Name, Albums")] Genre genre)
        {
            if (id != genre.GenreID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                _context.Update(genre);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(genre));
        }
Пример #13
0
        public async Task <IActionResult> Edit(int id, [Bind("ArtistID, Name, Bio")] Artist artist)
        {
            if (id != artist.ArtistID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                _context.Update(artist);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(artist));
        }
Пример #14
0
        public async void SongGetterandSetters()
        {
            DbContextOptions <MusicDbContext> options = new DbContextOptionsBuilder <MusicDbContext>()
                                                        .UseInMemoryDatabase("GetterSetterSongDB").Options;

            using (MusicDbContext context = new MusicDbContext(options))
            {
                //Arrange
                Song testSong1 = new Song();
                testSong1.Name      = "Gonna Give You Up";
                testSong1.Artist    = "Slick Rick";
                testSong1.Genre     = "oldies";
                testSong1.ApiListId = 33;
                testSong1.OurListId = 1;

                await context.Songs.AddAsync(testSong1);

                await context.SaveChangesAsync();

                var dbTestSong = await context.Songs.FirstOrDefaultAsync(u => u.Name == "Gonna Give You Up");

                dbTestSong.Name       = "Never Gonna Give You Up";
                testSong1.Artist      = "Rick Astley";
                testSong1.Genre       = "memerific oldies";
                testSong1.ApiListId   = 34;
                testSong1.OurListId   = 2;
                testSong1.ReleaseDate = new DateTime(2009, 09, 3, 14, 8, 5, 123);
                context.Update(dbTestSong);
                await context.SaveChangesAsync();

                var result1 = await context.Songs.FirstOrDefaultAsync(s => s.Artist == "Rick Astley");

                var result2 = context.Songs.Where(s => s.Artist == "Slick Rick");

                Assert.NotNull(result1);
                Assert.Equal(0, result2.Count());
                Assert.Equal("Never Gonna Give You Up", result1.Name);
                Assert.Equal("memerific oldies", result1.Genre);
                Assert.Equal(34, result1.ApiListId);
                Assert.Equal(2, result1.OurListId);
                Assert.Equal(new DateTime(2009, 09, 3, 14, 8, 5, 123), result1.ReleaseDate);
            }
        }
Пример #15
0
        public IActionResult Edit(int id, [Bind("ArtistID, Name, Bio")] Artist artist)
        {
            if (_context.Artists.SingleOrDefault(m => (m.Name == artist.Name && m.ArtistID != artist.ArtistID)) != null)
            {
                ViewData["Error"] = "Artist Name must be unique.";
            }
            else
            {
                if (id != artist.ArtistID)
                {
                    return(NotFound());
                }

                if (ModelState.IsValid)
                {
                    _context.Update(artist);
                    _context.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }
            return(View(artist));
        }
Пример #16
0
        public IActionResult Edit(int id, [Bind("GenreID, Name")] Genre genre)
        {
            if (_context.Genres.SingleOrDefault(m => (m.Name == genre.Name && m.GenreID != genre.GenreID)) != null)
            {
                ViewData["Error"] = "Genre Name must be unique.";
            }
            else
            {
                if (id != genre.GenreID)
                {
                    return(NotFound());
                }

                if (ModelState.IsValid)
                {
                    _context.Update(genre);
                    _context.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }
            return(View(genre));
        }
        public async void UserGetterandSetters()
        {
            DbContextOptions <MusicDbContext> options = new DbContextOptionsBuilder <MusicDbContext>()
                                                        .UseInMemoryDatabase("GetterSetterUserDB").Options;

            using (MusicDbContext context = new MusicDbContext(options))
            {
                //Arrange
                User testUser1 = new User();
                testUser1.Name       = "Bob";
                testUser1.GenreID    = 2;
                testUser1.PlaylistID = 3;

                await context.Users.AddAsync(testUser1);

                await context.SaveChangesAsync();

                var dbTestUser = await context.Users.FirstOrDefaultAsync(u => u.Name == "Bob");

                dbTestUser.Name       = "Bobby Brown";
                dbTestUser.GenreID    = 4;
                dbTestUser.PlaylistID = 5;

                context.Update(dbTestUser);
                await context.SaveChangesAsync();

                var result1 = context.Users.Where(u => u.Name == "Bobby Brown");
                var result2 = context.Users.Where(u => u.Name == "Bob");

                Assert.NotNull(result1);
                Assert.Equal(0, result2.Count());
                Assert.Equal("Bobby Brown", dbTestUser.Name);
                Assert.Equal(4, dbTestUser.GenreID);
                Assert.Equal(5, dbTestUser.PlaylistID);
            }
        }
Пример #18
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,ReleaseDate,Genre,PublisherId,Picture")] Album album)
        {
            if (id != album.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (Request.Form.Files.Count > 0)
                    {
                        IFormFile file = Request.Form.Files.FirstOrDefault();
                        using var dataStream = new MemoryStream();
                        await file.CopyToAsync(dataStream);

                        album.Picture = dataStream.ToArray();
                    }

                    album = await _context.Albums.Include(album => album.PerformerAlbums).AsNoTracking().FirstOrDefaultAsync(album => album.Id == id);

                    foreach (var performerAlbum in album.PerformerAlbums)
                    {
                        _context.PerformerAlbums.Remove(performerAlbum);
                    }


                    if (Request.Form.Files.Count > 0)
                    {
                        IFormFile file = Request.Form.Files.FirstOrDefault();
                        using var dataStream = new MemoryStream();
                        await file.CopyToAsync(dataStream);

                        album.Picture = dataStream.ToArray();
                    }

                    _context.Update(album);
                    await _context.SaveChangesAsync();

                    foreach (string performerId in Request.Form["Performers"])
                    {
                        PerformerAlbum performerAlbum = new PerformerAlbum()
                        {
                            AlbumId     = album.Id,
                            PerformerId = int.Parse(performerId)
                        };

                        _context.Add(performerAlbum);
                    }
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AlbumExists(album.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(album));
        }