public async Task SetAsyncAddsNewItem()
        {
            // arrange
            using var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
            var item = new CoursePath(Guid.NewGuid(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid());

            // act
            var repository = new CoursePathRepository(_database.SqlServerRepositoryOptions);
            var result     = await repository.SetAsync(item).ConfigureAwait(false);

            // assert
            using var connection = new SqlConnection(_database.ConnectionString);
            var saved = await connection.QuerySingleOrDefaultAsync <CoursePath>("SELECT [Key], [Name], [Slug], [Version] FROM [dbo].[CoursePath] WHERE [Key] = @Key", new { item.Key }).ConfigureAwait(false);

            // assert - result equals input except version
            Assert.Equal(result.Key, item.Key);
            Assert.Equal(result.Name, item.Name);
            Assert.Equal(result.Slug, item.Slug);
            Assert.NotEqual(result.Version, item.Version);

            // assert - saved equals input except version
            Assert.Equal(saved.Key, item.Key);
            Assert.Equal(saved.Name, item.Name);
            Assert.Equal(saved.Slug, item.Slug);
            Assert.NotEqual(saved.Version, item.Version);

            // assert - result version equals saved version
            Assert.Equal(saved.Version, result.Version);
        }
        public async Task ClearAsyncIgnoresMissingItem()
        {
            // arrange
            using (new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                // arrange
                var item       = new CoursePath(Guid.NewGuid(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid());
                var repository = new CoursePathRepository(_database.SqlServerRepositoryOptions);

                // act
                await repository.ClearAsync(item.Key, item.Version).ConfigureAwait(false);

                // assert
                Assert.True(true);
            }
        }
예제 #3
0
        public async Task <IActionResult> AddCoursePath(CoursePath _coursePath)
        {
            if (_coursePath == null)
            {
                return(BadRequest());
            }

            var course = await Context.Course.FindAsync(_coursePath.CourseId);

            if (course == null)
            {
                return(NotFound(new { message = "there is no course with this id" }));
            }

            Db.AddCoursePath(_coursePath);
            return(Created("CoursePath has been added", _coursePath));
        }
        public async Task SetAsyncThrowsOnExistingId()
        {
            // arrange
            using var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
            var item = new CoursePath(Guid.NewGuid(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid());

            await using (var connection = new SqlConnection(_database.ConnectionString))
            {
                await connection.ExecuteAsync("INSERT INTO [dbo].[CoursePath] ([Key], [Name], [Slug], [Version]) VALUES (@Key, @Name, @Slug, @Version)", item).ConfigureAwait(false);
            }

            // act
            var repository = new CoursePathRepository(_database.SqlServerRepositoryOptions);
            var ex         = await Assert.ThrowsAsync <KeyAlreadyExistsException>(() => repository.SetAsync(item)).ConfigureAwait(false);

            // assert
            Assert.Equal(item.Key, ex.Key);
        }
예제 #5
0
        public IActionResult UpdateCoursePath(int id, CoursePath _coursePath)
        {
            if (id != _coursePath.Id || id == null)
            {
                return(BadRequest());
            }

            try
            {
                Db.UpdateCoursePath(id, _coursePath);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw new TimeoutException("time exception out in CoursePath Controller Update");
            }

            return(Ok(_coursePath));
        }
예제 #6
0
        public Task <CoursePath> SetAsync(CoursePath entity)
        {
            // validation
            if (entity is null)
            {
                throw new ArgumentNullException(nameof(entity));
            }
            if (entity.Key != _key)
            {
                throw new InvalidOperationException();
            }

            // preempt the concurrency exception to avoid hitting the database
            if (entity.Version != Guid.Empty)
            {
                throw new ConcurrencyException(null, entity.Version);
            }

            return(InnerSetAsync(entity));
        }
        public async Task ClearAsyncThrowsOnVersionMismatch()
        {
            // arrange
            using (new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var existing = new CoursePath(Guid.NewGuid(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid());
                using (var connection = new SqlConnection(_database.ConnectionString))
                {
                    await connection.ExecuteAsync("INSERT INTO [dbo].[CoursePath] ([Key], [Name], [Slug], [Version]) VALUES (@Key, @Name, @Slug, @Version)", existing).ConfigureAwait(false);
                }

                // act
                var repository = new CoursePathRepository(_database.SqlServerRepositoryOptions);
                var item       = existing.WithVersion(Guid.NewGuid());
                var ex         = await Assert.ThrowsAsync <ConcurrencyException>(() => repository.ClearAsync(item.Key, item.Version)).ConfigureAwait(false);

                // assert
                Assert.Equal(existing.Version, ex.StoredVersion);
                Assert.Equal(item.Version, ex.CurrentVersion);
            }
        }
        public async Task GetAsyncReturnsExistingItem()
        {
            // arrange
            using (new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var item = new CoursePath(Guid.NewGuid(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid());
                using (var connection = new SqlConnection(_database.ConnectionString))
                {
                    await connection.ExecuteAsync("INSERT INTO [dbo].[CoursePath] ([Key], [Name], [Slug], [Version]) VALUES (@Key, @Name, @Slug, @Version)", item).ConfigureAwait(false);
                }

                // act
                var repository = new CoursePathRepository(_database.SqlServerRepositoryOptions);
                var result     = await repository.GetAsync(item.Key).ConfigureAwait(false);

                // assert
                Assert.Equal(item.Key, result.Key);
                Assert.Equal(item.Name, result.Name);
                Assert.Equal(item.Slug, result.Slug);
                Assert.Equal(item.Version, result.Version);
            }
        }
        public async Task SetAsyncUpdatesExistingItem()
        {
            // arrange
            using (new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var item = new CoursePath(Guid.NewGuid(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid());
                using (var connection = new SqlConnection(_database.ConnectionString))
                {
                    await connection.ExecuteAsync("INSERT INTO [dbo].[CoursePath] ([Key], [Name], [Slug], [Version]) VALUES (@Key, @Name, @Slug, @Version)", item).ConfigureAwait(false);
                }

                // act
                var repository = new CoursePathRepository(_database.SqlServerRepositoryOptions);
                var current    = await repository.GetAsync(item.Key).ConfigureAwait(false);

                var proposed = new CoursePath(current.Key, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), current.Version);
                var saved    = await repository.SetAsync(proposed).ConfigureAwait(false);

                // assert
                using (var connection = new SqlConnection(_database.ConnectionString))
                {
                    var actual = await connection.QuerySingleOrDefaultAsync <CoursePath>("SELECT [Key], [Name], [Slug], [Version] FROM [dbo].[CoursePath] WHERE [Key] = @Key", new { item.Key }).ConfigureAwait(false);

                    // assert - current equals item
                    Assert.Equal(item, current);

                    // assert - actual equals saved
                    Assert.Equal(saved, actual);

                    // assert - saved equals proposed plus new version
                    Assert.Equal(proposed.Key, saved.Key);
                    Assert.Equal(proposed.Name, saved.Name);
                    Assert.Equal(proposed.Slug, saved.Slug);
                    Assert.NotEqual(proposed.Version, saved.Version);
                }
            }
        }
        public async Task ClearAsyncRemovesExistingItem()
        {
            // arrange
            using (new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var item = new CoursePath(Guid.NewGuid(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid());
                using (var connection = new SqlConnection(_database.ConnectionString))
                {
                    await connection.ExecuteAsync("INSERT INTO [dbo].[CoursePath] ([Key], [Name], [Slug], [Version]) VALUES (@Key, @Name, @Slug, @Version)", item).ConfigureAwait(false);
                }

                // act
                var repository = new CoursePathRepository(_database.SqlServerRepositoryOptions);
                await repository.ClearAsync(item.Key, item.Version).ConfigureAwait(false);

                // assert
                using (var connection = new SqlConnection(_database.ConnectionString))
                {
                    var saved = await connection.QuerySingleOrDefaultAsync <CoursePath>("SELECT [Key], [Name], [Slug], [Version] FROM [dbo].[CoursePath] WHERE [Key] = @Key", new { item.Key }).ConfigureAwait(false);

                    Assert.Null(saved);
                }
            }
        }
예제 #11
0
 public void Inject(CoursePath coursePath)
 {
     _coursePath.OnNext(coursePath);
     _coursePath.OnCompleted();
 }
예제 #12
0
 private async Task <CoursePath> InnerSetAsync(CoursePath entity)
 {
     return(await _repository.SetAsync(entity).ConfigureAwait(true));
 }
예제 #13
0
 public async void UpdateCoursePath(int id, CoursePath _coursePath)
 {
     Db.Entry(_coursePath).State = EntityState.Modified;
     await Db.SaveChangesAsync();
 }
예제 #14
0
        public async void AddCoursePath(CoursePath _coursePath)
        {
            await Db.CoursePaths.AddAsync(_coursePath);

            Db.SaveChanges();
        }