public async Task <AlbumGroupDetail> AddAsync(AlbumGroup group)
        {
            var dbAlbumGroup = await _context.AlbumGroups.SingleOrDefaultAsync(x => x.Key == group.Key);

            if (dbAlbumGroup == null)
            {
                dbAlbumGroup = new DbAlbumGroup()
                {
                    Key        = group.Key,
                    Name       = group.Name,
                    CreatedUtc = DateTime.UtcNow,
                    UpdatedUtc = DateTime.UtcNow
                };
                _context.AlbumGroups.Add(dbAlbumGroup);
                await _context.SaveChangesAsync();

                return(new AlbumGroupDetail()
                {
                    Id = dbAlbumGroup.Id,
                    Key = dbAlbumGroup.Key,
                    Name = dbAlbumGroup.Name,
                    TotalAlbums = 0,
                    Created = new DateTime(dbAlbumGroup.CreatedUtc.Ticks, DateTimeKind.Utc),
                    Updated = new DateTime(dbAlbumGroup.UpdatedUtc.Ticks, DateTimeKind.Utc)
                });
            }
            else
            {
                throw new EntityAlreadyExistsRepositoryException($"A group with the key '{group.Key}' already exists");
            }
        }
Esempio n. 2
0
        public AlbumRepository_ListByAlbumGroupKeyTests()
        {
            // set up test data
            var options = new DbContextOptionsBuilder <MusicStoreDbContext>()
                          .UseInMemoryDatabase(databaseName: "test_db" + Guid.NewGuid().ToString())
                          .Options;

            this.db = new MusicStoreDbContext(options);
            foreach (string g in _validGenres)
            {
                this.db.Genres.Add(new DbGenre {
                    Name = g, CreatedUtc = DateTime.UtcNow
                });
            }
            var imageResource = new DbImageResource()
            {
                MimeType = "img/png",
                Data     = new byte[10]
            };
            var artist = new DbArtist
            {
                BioText       = "",
                CreatedUtc    = DateTime.UtcNow,
                Name          = "test artist",
                PublishStatus = DbPublishedStatus.PUBLISHED,
                UpdatedUtc    = DateTime.UtcNow
            };

            db.ImageResources.Add(imageResource);
            db.Artists.Add(artist);

            List <DbAlbum> testAlbums = new List <DbAlbum>();

            for (int i = 0; i < 10; i++)
            {
                testAlbums.Add(new DbAlbum
                {
                    Title         = "test_album_" + i,
                    CreatedUtc    = DateTime.UtcNow,
                    PublishStatus = DbPublishedStatus.PUBLISHED,
                    ReleaseDate   = DateTime.Now,
                    UpdatedUtc    = DateTime.UtcNow,
                    Artist        = new DbArtist
                    {
                        Name          = "test artist " + i,
                        PublishStatus = DbPublishedStatus.PUBLISHED,
                        CreatedUtc    = DateTime.UtcNow,
                        UpdatedUtc    = DateTime.UtcNow
                    }
                });
            }
            this.db.Albums.AddRange(testAlbums);

            var group = new DbAlbumGroup
            {
                CreatedUtc = DateTime.UtcNow,
                Key        = VALID_GROUP_KEY,
                Name       = "test group",
                UpdatedUtc = DateTime.UtcNow
            };

            db.AlbumGroups.Add(group);
            db.SaveChanges();
            _validImageId  = imageResource.Id;
            _validArtistId = artist.Id;
            _validGroupId  = group.Id;
            _validAlbumIds = testAlbums.Select(x => x.Id).ToArray();
            for (int i = 0; i < _validAlbumIds.Length; i++)
            {
                // insert all albums into the test group
                db.AlbumGroupListPositions.Add(new DbAlbumGroupAlbumPosition
                {
                    AlbumId       = _validAlbumIds[i],
                    GroupId       = _validGroupId,
                    CreatedUtc    = DateTime.UtcNow,
                    PositionIndex = i
                });
            }
            db.SaveChanges();

            var loggerMock = new Mock <ILogger <ArtistController> >();

            this.repo = new AlbumRepository(this.db, new AlbumMapper());
        }
 public AlbumGroupDetail Map(DbAlbumGroup group)
 {
     throw new NotImplementedException();
 }