コード例 #1
0
        public async Task AddAlbumAsync(int groupId, int albumId)
        {
            DbAlbumGroupAlbumPosition entity = await _context.AlbumGroupListPositions.SingleOrDefaultAsync(x => x.GroupId == groupId && x.AlbumId == albumId);

            if (entity == null)
            {
                var group = await _context.AlbumGroups.SingleOrDefaultAsync(x => x.Id == groupId);

                if (group == null)
                {
                    throw new EntityNotFoundRepositoryException($"Album group with ID {groupId} not found");
                }
                var album = await _context.Albums.SingleOrDefaultAsync(x => x.Id == albumId);

                if (album == null)
                {
                    throw new EntityNotFoundRepositoryException($"Album with ID {albumId} not found");
                }
                var totalItemsInGroup = _context.AlbumGroups.SelectMany(x => x.Items).Count();
                entity = new DbAlbumGroupAlbumPosition
                {
                    Album         = album,
                    CreatedUtc    = DateTime.UtcNow,
                    Group         = group,
                    PositionIndex = totalItemsInGroup + 1
                };
                await _context.AlbumGroupListPositions.AddAsync(entity);

                await _context.SaveChangesAsync();
            }
        }
コード例 #2
0
        public async Task RemoveAlbumAsync(int groupId, int albumId)
        {
            DbAlbumGroupAlbumPosition entity = await _context.AlbumGroupListPositions.SingleOrDefaultAsync(x => x.GroupId == groupId && x.AlbumId == albumId);

            if (entity != null)
            {
                _context.AlbumGroupListPositions.Remove(entity);
                await _context.SaveChangesAsync();
            }
        }