static void Main() { var ctx = new MusicDbContext(); MusicData dbContext = new MusicData(ctx); for (int i = 0; i < 3; i++) { var artist = new Artist() { Name = "Artist" + i, }; dbContext.Artists.Add(artist); var album = new Album() { Title = "Album" + i}; album.Artists.Add(artist); dbContext.Albums.Add(album); var song = new Song("Song" + i, artist); song.Albums.Add(album); dbContext.Songs.Add(song); } dbContext.SaveChanges(); }
public ReviewController(MusicDbContext context) { this.context = context; Mapper.Initialize(cfg => { cfg.CreateMap <Review, ReviewViewModel>() .ForMember(t => t.ArtistName, opt => opt.MapFrom(a => a.Artist.ArtistName)) .ForMember(t => t.TrackName, opt => opt.MapFrom(g => g.Track.TrackName)); cfg.CreateMap <ReviewViewModel, Review>(); }); }
public TrackController(MusicDbContext context) { this.context = context; Mapper.Initialize(cfg => { cfg.CreateMap <Track, TrackViewModel>() .ForMember(t => t.ArtistName, opt => opt.MapFrom(a => a.Artist.ArtistName)) .ForMember(t => t.GenreName, opt => opt.MapFrom(g => g.Genre.Name)); cfg.CreateMap <TrackViewModel, Track>(); }); }
public AccountController( UserManager <ApplicationUser> userManager, SignInManager <ApplicationUser> signInManager, IOptions <IdentityCookieOptions> identityCookieOptions, MusicDbContext context ) { _userManager = userManager; _signInManager = signInManager; _externalCookieScheme = identityCookieOptions.Value.ExternalCookieAuthenticationScheme; _context = context; }
public void CanViewIndex() { DbContextOptions <MusicDbContext> options = new DbContextOptionsBuilder <MusicDbContext>() .UseInMemoryDatabase("HomeViewIndex").Options; using (MusicDbContext context = new MusicDbContext(options)) { HomeController testHC = new HomeController(context); var result1 = testHC.Index(); Assert.IsType <ViewResult>(result1); } }
private static void SeedSongIfNotExists(MusicDbContext dbContext, String name, Genre genre, Artist artist) { var item = dbContext.Songs.FirstOrDefault(x => x.Title == name); if (item == null) { dbContext.Songs.Add(new SongBuilder() .WithTitle(name) .AddGenresItem(genre) .AddArtistsItem(artist) .Build()); } }
private static async Task <SongEntity> FindSong(MusicDbContext context, ItemId id, CancellationToken cancellationToken, bool includePlaybacks = false) { IQueryable <SongEntity> queryable = context.Songs; if (includePlaybacks) { queryable = queryable.Include(s => s.Playbacks); } var entityId = id.ToInt32(); return(await queryable .SingleAsync(s => s.Id == entityId, cancellationToken)); }
public async Task <MaintainMusicInfoResult> AddSong(SongRequest songRequest) { try { var song = new Song { Title = songRequest.Title, AKATitles = songRequest.AKATitles, Picture = songRequest.Picture, Seconds = songRequest.Seconds, SingerSongs = songRequest.Singers.Select(u => new SingerSong { Singer = new Singer { Name = u } }).ToList(), GenreSongs = songRequest.Genres.Select(u => new GenreSong { Genre = new Genre { Title = u } }).ToList(), Album = new Album { Title = songRequest.Album }, }; await MusicDbContext.Songs.AddAsync(song); await IndexMaintainer.AddIndex(song, CancellationToken.None); Logger.LogInformation("{AddSong} index successful", nameof(AddSong)); await MusicDbContext.SaveChangesAsync(); Logger.LogInformation("{AddSong} data successful", nameof(AddSong)); return(new MaintainMusicInfoResult { ResultCode = ResultCode.Successful }); } catch (Exception ex) { return(new MaintainMusicInfoResult { ResultCode = ResultCode.Exception, ExtraMessage = ex.Message }); } }
public bool DeleteAlbum(int id) { using (var db = new MusicDbContext()) { var album = db.Albums.Find(id); if (album == null) { return(false); } db.Albums.Remove(album); return(db.SaveChanges() > 0); } }
public void CanCreatePlaylist() { DbContextOptions <MusicDbContext> options = new DbContextOptionsBuilder <MusicDbContext>() .UseInMemoryDatabase("testDB11").Options; using (MusicDbContext context = new MusicDbContext(options)) { PlaylistController pc = new PlaylistController(context); Playlist p1 = new Playlist(); context.Playlists.Add(p1); context.SaveChanges(); Assert.NotEmpty(context.Playlists); } }
public static void Main() { Database.SetInitializer( new MigrateDatabaseToLatestVersion<MusicDbContext, Configuration>()); var db = new MusicDbContext(); var artist = new Artist() { Name = "Someone", Country = "UK", DateOfBirth = new DateTime(1977, 05, 06) }; db.Artists.Add(artist); db.SaveChanges(); }
public void CanGetPlaylist() { DbContextOptions <MusicDbContext> options = new DbContextOptionsBuilder <MusicDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options; using (MusicDbContext context = new MusicDbContext(options)) { PlaylistController pc = new PlaylistController(context); Playlist playlist = new Playlist(); playlist.ID = 1; playlist.Name = "Test"; context.Playlists.AddAsync(playlist); context.SaveChangesAsync(); var response = pc.GetPlaylistByID(1).Result; var result = (ObjectResult)response; Assert.Equal(HttpStatusCode.OK, (HttpStatusCode)result.StatusCode.Value); } }
public void GetRetunsString() { var options = new DbContextOptionsBuilder <MusicDbContext>() .UseInMemoryDatabase(databaseName: "ReturnStringTest") .Options; //Arrange using (var context = new MusicDbContext(options)) { var controller = new SongsController(context); //Act var result = controller.Get(5); //Assert Assert.IsType <string>(result); } }
public DatabaseService(MusicDbContext context) { _context = context; var model = _context.Model.GetRelationalModel(); var functions = model.Functions; foreach (var function in functions) { Functions.Add(function.Name); } var tables = _context.Model.GetRelationalModel().Tables; //.Model.; foreach (var table in tables) { Tables.Add(table.Name); } }
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); } }
public int SaveAlbum(Album album) { using (var db = new MusicDbContext()) { if (album.Id > 0) { db.Albums.Attach(album); db.Entry(album).State = EntityState.Modified; } else { db.Albums.Add(album); } db.SaveChanges(); return(album.Id); } }
public async Task <MaintainMusicInfoResult> DeleteSong(Guid songPk) { var existSong = await MusicDbContext.Songs.FindAsync(songPk); if (existSong != null) { MusicDbContext.Songs.Remove(existSong); await IndexMaintainer.RemoveIndex(songPk, CancellationToken.None); Logger.LogInformation("{DeleteSong} index successful", nameof(DeleteSong)); await MusicDbContext.SaveChangesAsync(); Logger.LogInformation("{DeleteSong} data successful", nameof(DeleteSong)); } return(new MaintainMusicInfoResult { ResultCode = ResultCode.Successful }); }
public void CanGetAllPlaylists() { DbContextOptions <MusicDbContext> options = new DbContextOptionsBuilder <MusicDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options; using (MusicDbContext context = new MusicDbContext(options)) { Playlist playlist = new Playlist(); playlist.ID = 1; playlist.Name = "Test"; Playlist playlist1 = new Playlist(); playlist1.ID = 2; playlist1.Name = "Test1"; context.Playlists.AddAsync(playlist); context.Playlists.AddAsync(playlist1); context.SaveChangesAsync(); PlaylistController pc = new PlaylistController(context); var FakeNews = context.Playlists; Assert.Equal(pc.Get(), FakeNews); } }
public async void CheckingForRedirectWhenNewUserGetToPlaylistDisplayPage() { DbContextOptions <MusicDbContext> options = new DbContextOptionsBuilder <MusicDbContext>() .UseInMemoryDatabase("DisplayPlaylistOne").Options; using (MusicDbContext context = new MusicDbContext(options)) { User user1 = new User(); user1.Name = "username"; await context.Users.AddAsync(user1); await context.SaveChangesAsync(); PlaylistController plc = new PlaylistController(context); var result = await plc.Get(user1.Id); var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result); Assert.Null(redirectToActionResult.ControllerName); Assert.Equal("Create", redirectToActionResult.ActionName); } }
public async void CheckToSeeCustomPlaylistIsBeingCreated() { DbContextOptions <MusicDbContext> options = new DbContextOptionsBuilder <MusicDbContext>() .UseInMemoryDatabase("CreateCustomPlaylist").Options; using (MusicDbContext context = new MusicDbContext(options)) { User user2 = new User(); user2.Name = "username"; await context.Users.AddAsync(user2); await context.SaveChangesAsync(); PlaylistController plc = new PlaylistController(context); var result = await plc.CreateCustomPlaylist(user2.Id); var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result); //Assert.Null(redirectToActionResult.ControllerName); Assert.Equal("Get", redirectToActionResult.ActionName); } }
internal static void Main() { MusicDbContext context = new MusicDbContext(); context.Albums.Any(); // Add an Accept header for JSON format. Client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); Console.WriteLine("Your server must be started! If there is an error check the Uri!"); Console.WriteLine(); //AddNewAlbum("Testing the client"); //UpdateAlbum(1, new Album //{ // Id = 1, // Title = "Test updating through client" //}); //DeleteAlbum(3); PrintAllAlbums(); }
public async void CanEditUser() { DbContextOptions <MusicDbContext> options = new DbContextOptionsBuilder <MusicDbContext>() .UseInMemoryDatabase("CanEditDB").Options; using (MusicDbContext context = new MusicDbContext(options)) { // arrange User testUser1 = new User(); testUser1.Id = 20; testUser1.Name = "Bob"; testUser1.GenreID = 2; testUser1.PlaylistID = 3; await context.Users.AddAsync(testUser1); await context.SaveChangesAsync(); UserController testUC = new UserController(context); // act var getResult1 = context.Users.Where(u => u.Name == "Bob"); // Assert Assert.Equal(1, getResult1.Count()); var result2 = await testUC.Edit(20, "Bobby"); var getResult2 = context.Users.Where(u => u.Name == "Bobby"); var getResult3 = context.Users.Where(u => u.GenreID == 2); var getResult4 = context.Users.Where(u => u.PlaylistID == 3); Assert.Equal(0, getResult1.Count()); Assert.Equal(1, getResult2.Count()); Assert.Equal(getResult2, getResult3); Assert.Equal(getResult2, getResult4); } }
public async void GetSingleSongTest() { DbContextOptions <MusicDbContext> options = new DbContextOptionsBuilder <MusicDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options; using (MusicDbContext context = new MusicDbContext(options)) { // Arrange Playlist playlist = new Playlist() { Name = "test" }; context.Playlists.Add(playlist); await context.SaveChangesAsync(); Song song = new Song() { Name = "Shake It Off", Artist = "Taylor Swift", Album = "1989", Genre = "Pop", ReleaseDate = DateTime.Today, PlaylistID = 1 }; SongController sc = new SongController(context); context.Songs.Add(song); context.SaveChanges(); var findSong = await context.Songs.FirstAsync(s => s.Name == song.Name); // Act var result = sc.GetSongByID(findSong.ID); var answer = (OkObjectResult)result; // Assert Assert.Equal((HttpStatusCode.OK), (HttpStatusCode)answer.StatusCode); } }
public void CanPostSong() { DbContextOptions <MusicDbContext> options = new DbContextOptionsBuilder <MusicDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options; using (MusicDbContext context = new MusicDbContext(options)) { Song song = new Song() { Name = "Shake It Off", Artist = "Taylor Swift", Album = "1989", Genre = "Pop", ReleaseDate = DateTime.Today, PlaylistID = 1 }; SongController sc = new SongController(context); var test = sc.Post(song); CreatedAtRouteResult carr = (CreatedAtRouteResult)test.Result; Song mysoun = (Song)carr.Value; Assert.Equal(1, mysoun.ID); } }
public async void CheckingForViewWhenComingBackDefaultPlaylistUser() { DbContextOptions <MusicDbContext> options = new DbContextOptionsBuilder <MusicDbContext>() .UseInMemoryDatabase("DisplayPlaylistFour").Options; using (MusicDbContext context = new MusicDbContext(options)) { User user = new User(); user.Name = "username"; Playlist playlist = new Playlist(); playlist.Id = 27; await context.Users.AddAsync(user); await context.Playlists.AddAsync(playlist); await context.SaveChangesAsync(); user.PlaylistID = playlist.Id.Value; user.GenreID = 3; playlist.UserID = user.Id; context.Users.Update(user); context.Playlists.Update(playlist); await context.SaveChangesAsync(); PlaylistViewModel plvm = new PlaylistViewModel(); plvm.User = user; PlaylistController plc = new PlaylistController(context); var result = await plc.Get(user.Id) as ViewResult; var plVMTest = (PlaylistViewModel)result.ViewData.Model; var testingResult = plVMTest.User.Id; Assert.Equal(user.Id, testingResult); } }
public async void GetAllSongsTest() { DbContextOptions <MusicDbContext> options = new DbContextOptionsBuilder <MusicDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options; using (MusicDbContext context = new MusicDbContext(options)) { // Arrange Playlist playlist = new Playlist() { Name = "test" }; await context.Playlists.AddAsync(playlist); await context.SaveChangesAsync(); Song song = new Song() { Name = "Shake It Off", Artist = "Taylor Swift", Album = "1989", Genre = "Pop", ReleaseDate = DateTime.Today, PlaylistID = 1 }; SongController sc = new SongController(context); await context.Songs.AddAsync(song); await context.SaveChangesAsync(); // Act var result = sc.Get(); // Assert Assert.Equal(context.Songs, result); } }
public async void CheckToSeeIfUsersPlaylistGetsSelected() { DbContextOptions <MusicDbContext> options = new DbContextOptionsBuilder <MusicDbContext>() .UseInMemoryDatabase("FindUserPlaylist").Options; using (MusicDbContext context = new MusicDbContext(options)) { User user3 = new User(); user3.Name = "username"; Playlist playlist = new Playlist(); playlist.Id = 27; user3.GenreID = 3; await context.Users.AddAsync(user3); await context.Playlists.AddAsync(playlist); await context.SaveChangesAsync(); user3.PlaylistID = playlist.Id.Value; playlist.UserID = user3.Id; context.Users.Update(user3); context.Playlists.Update(playlist); await context.SaveChangesAsync(); PlaylistViewModel plvm = new PlaylistViewModel(); plvm.User = user3; PlaylistController plc = new PlaylistController(context); var result = await plc.Edit(user3); var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result); Assert.Equal("Get", redirectToActionResult.ActionName); } }
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); } }
public async void CanCreateWithGet() { DbContextOptions <MusicDbContext> options = new DbContextOptionsBuilder <MusicDbContext>() .UseInMemoryDatabase("CreateAndDeleteUserDB").Options; using (MusicDbContext context = new MusicDbContext(options)) { // arrange User testUser1 = new User(); testUser1.Name = "Bob"; User testUser2 = new User(); testUser2.Name = "Todd"; UserController testUC = new UserController(context); // act var result = testUC.Get(); // Assert Assert.IsType <ViewResult>(result); await testUC.Get(testUser1.Name); var results1 = context.Users.Where(u => u.Name == "Bob"); var results2 = context.Users.Where(u => u.Name == "Todd"); // assert there is one instance of the results Assert.Equal(1, results1.Count()); Assert.Equal(0, results2.Count()); await testUC.Get("Bob"); // assert that there is still one instance of results Assert.Equal(1, results1.Count()); } }
public AlbumsController(MusicDbContext dbContext) { this.dbContext = dbContext; }
public Genres1Controller(MusicDbContext context) { _context = context; }
public SongsController(MusicDbContext context) { _context = context; }