/// <summary> /// get the album resource from the database if it exists. If it does not exist, /// get the album and the resource and put them into a new AlbumResource /// </summary> /// <param name="albumID"></param> /// <param name="resourceGUID"></param> /// <param name="userName"></param> /// <returns></returns> public AlbumResource GetOrCreateAlbumResource(int albumID, string resourceGUID, string userName) { AlbumResource albumResource = null; var sessionFactory = SessionFactoryCreator.CreateSessionFactory(); using (var session = sessionFactory.OpenSession()) { using (session.BeginTransaction()) { albumResource = session.QueryOver <AlbumResource>() .Where(x => x.Album.ID == albumID && x.Resource.Md5 == resourceGUID) .List().FirstOrDefault(); if (albumResource == null) { Album album = session.QueryOver <Album>() .Where(x => x.ID == albumID) .JoinQueryOver(x => x.Owner) .Where(x => x.UserName == userName) .List().FirstOrDefault(); DigitalResource resource = Get(resourceGUID, userName); albumResource = new AlbumResource { Album = album, Resource = resource }; } } } return(albumResource); }
private static Album MapAlbum(AlbumResource resource, Dictionary <string, ArtistMetadata> artistDict) { Album album = new Album(); album.ForeignAlbumId = resource.Id; album.OldForeignAlbumIds = resource.OldIds; album.Title = resource.Title; album.Overview = resource.Overview; album.Disambiguation = resource.Disambiguation; album.ReleaseDate = resource.ReleaseDate; if (resource.Images != null) { album.Images = resource.Images.Select(MapImage).ToList(); } album.AlbumType = resource.Type; album.SecondaryTypes = resource.SecondaryTypes.Select(MapSecondaryTypes).ToList(); album.Ratings = MapRatings(resource.Rating); album.Links = resource.Links?.Select(MapLink).ToList(); album.Genres = resource.Genres; album.CleanTitle = Parser.Parser.CleanArtistName(album.Title); if (resource.Releases != null) { album.AlbumReleases = resource.Releases.Select(x => MapRelease(x, artistDict)).Where(x => x.TrackCount > 0).ToList(); } album.AnyReleaseOk = true; return(album); }
public async Task <IActionResult> UpdateAlbum([FromRoute] int id, [FromForm] AlbumResource albumResource) { Album album = await albumRepository.GetAsync(id); if (album != null) { var userName = User.FindFirstValue(ClaimTypes.Name); if (album.Author.UserName != userName) { return(Forbid()); } if (string.IsNullOrEmpty(albumResource.Name)) { return(BadRequest()); } album.Name = albumResource.Name; album.ModifiedDate = DateTime.UtcNow; await this.unitOfWork.CompleteAsync(); var outputAlbumResource = mapper.Map <Album, AlbumResource>(album); return(Ok(outputAlbumResource)); } else { return(NotFound()); } }
public bool AddResource(int albumID, string resourceID) { bool success = false; //throw new Exception("forced error"); Repository.ResourceRepository rr = new Repository.ResourceRepository(); AlbumResource albumResource = rr.GetOrCreateAlbumResource(albumID, resourceID, User.Identity.Name); //DigitalResource resource = null; if (albumResource.Album != null && albumResource.Resource != null) { if (albumResource.ID == 0)//needs to be saved to the database { if (albumResource.IsValid) { rr.SaveAlbumResource(albumResource); } } } else { success = false; throw new System.Exception("Unable to add resource to Album. Check that you own both resource and album and that they exist"); } return(true); }
private Album MapSearchResult(AlbumResource resource) { var album = _albumService.FindById(resource.Id) ?? MapAlbum(resource, null); var artist = _artistService.FindById(resource.ArtistId); if (artist == null) { artist = new Artist(); artist.Metadata = MapArtistMetadata(resource.Artists.Single(x => x.Id == resource.ArtistId)); } album.Artist = artist; album.ArtistMetadata = artist.Metadata; return(album); }
public async Task UpdateAlbum() { // Arrange int albumId = new Random().Next(1, 100); string seed = Guid.NewGuid().ToString(); string expectedUserName = string.Format("test_{0}@gmail.com", Guid.NewGuid()); ControllerContext controllerContext = Utilities.SetupCurrentUserForController(expectedUserName); var mockAlbumRepository = new Mock <IAlbumRepository>(); var mockUserRepository = new Mock <IUserRepository>(); User expectedUser = new User() { Id = seed, UserName = expectedUserName }; mockUserRepository.Setup(m => m.GetOrAdd(It.IsAny <User>())).Returns(expectedUser); var mockUnitOfWork = new Mock <IUnitOfWork>(); AlbumsController controller = new AlbumsController(this._mapper, mockAlbumRepository.Object, mockUserRepository.Object, mockUnitOfWork.Object); controller.ControllerContext = controllerContext; var seedAlbum = new Album() { Id = albumId, Name = "Album " + albumId, Author = new User() { UserName = expectedUserName }, }; mockAlbumRepository.Setup(m => m.GetAsync(albumId, true)).ReturnsAsync(seedAlbum); AlbumResource updateResource = new AlbumResource() { Id = albumId, Name = seed }; // Act var result = await controller.UpdateAlbum(albumId, updateResource); // Assert Assert.IsType <OkObjectResult>(result); Assert.IsType <AlbumResource>(((OkObjectResult)result).Value); AlbumResource returnedResource = (AlbumResource)((OkObjectResult)result).Value; Assert.True(returnedResource.Equals(updateResource)); }
private static Album MapAlbum(AlbumResource resource, Dictionary <string, ArtistMetadata> artistDict) { Album album = new Album(); album.ForeignAlbumId = resource.Id; album.OldForeignAlbumIds = resource.OldIds; album.Title = resource.Title; album.Overview = resource.Overview; album.Disambiguation = resource.Disambiguation; album.ReleaseDate = resource.ReleaseDate; if (resource.Images != null) { album.Images = resource.Images.Select(MapImage).ToList(); } album.AlbumType = resource.Type; album.SecondaryTypes = resource.SecondaryTypes.Select(MapSecondaryTypes).ToList(); album.Ratings = MapRatings(resource.Rating); album.Links = resource.Links?.Select(MapLink).ToList(); album.Genres = resource.Genres; album.CleanTitle = Parser.Parser.CleanArtistName(album.Title); if (resource.Releases != null) { album.AlbumReleases = resource.Releases.Select(x => MapRelease(x, artistDict)).Where(x => x.TrackCount > 0).ToList(); // Monitor the release with most tracks var mostTracks = album.AlbumReleases.Value.OrderByDescending(x => x.TrackCount).FirstOrDefault(); if (mostTracks != null) { mostTracks.Monitored = true; } } else { album.AlbumReleases = new List <AlbumRelease>(); } album.AnyReleaseOk = true; return(album); }
public async Task <IActionResult> CreateAlbum([FromForm] AlbumResource albumResource) { if (string.IsNullOrEmpty(albumResource.Name)) { return(NoContent()); } else { var album = this.mapper.Map <AlbumResource, Album>(albumResource); var currentUser = new User() { UserName = User.FindFirstValue(ClaimTypes.Name) }; album.Author = this.userRepository.GetOrAdd(currentUser); this.albumRepository.Add(album); await this.unitOfWork.CompleteAsync(); return(Ok(mapper.Map <Album, AlbumResource>(album))); } }
public async Task GetPhoto() { // Arrange var seedIds = new List <int> { new Random().Next(1, 50), new Random().Next(51, 100), new Random().Next(101, 150), new Random().Next(151, 200) }; var seedAlbums = SeedAlbums(seedIds); string seed = Guid.NewGuid().ToString(); string expectedUserName = string.Format("test_{0}@gmail.com", seed); ControllerContext controllerContext = Utilities.SetupCurrentUserForController(expectedUserName); var mockAlbumRepository = new Mock <IAlbumRepository>(); var mockUserRepository = new Mock <IUserRepository>(); User expectedUser = new User() { Id = seed, UserName = expectedUserName }; mockUserRepository.Setup(m => m.GetOrAdd(It.IsAny <User>())).Returns(expectedUser); var mockUnitOfWork = new Mock <IUnitOfWork>(); AlbumsController controller = new AlbumsController(this._mapper, mockAlbumRepository.Object, mockUserRepository.Object, mockUnitOfWork.Object); controller.ControllerContext = controllerContext; foreach (var seedAlbum in seedAlbums) { var id = seedAlbum.Id; var seedAlbumResource = this._mapper.Map <Album, AlbumResource>(seedAlbum); mockAlbumRepository.Setup(m => m.GetAsync(id, true)).ReturnsAsync(seedAlbum); // Act var result = await controller.GetAlbum(id); // Assert Assert.IsType <OkObjectResult>(result); Assert.IsType <AlbumResource>(((OkObjectResult)result).Value); AlbumResource returnedAlbumResource = (AlbumResource)((OkObjectResult)result).Value; Assert.True(returnedAlbumResource.Equals(seedAlbumResource)); } }
private Album MapSearchResult(AlbumResource resource) { var artists = resource.Artists.Select(MapArtistMetadata).ToDictionary(x => x.ForeignArtistId, x => x); var artist = _artistService.FindById(resource.ArtistId); if (artist == null) { artist = new Artist(); artist.Metadata = artists[resource.ArtistId]; } var album = _albumService.FindById(resource.Id) ?? MapAlbum(resource, artists); album.Artist = artist; album.ArtistMetadata = artist.Metadata.Value; if (!album.AlbumReleases.Value.Any()) { return(null); } return(album); }