public async Task DeleteNonExistingArticle()
        {
            // arrange
            HAVIdatabaseContext dbContext  = CreateDbContext();
            ArticleRepository   repository = new ArticleRepository(dbContext);
            Article             article    = new Article()
            {
                Id                                  = 1,
                PurchaserId                         = 0,
                CountryId                           = 0,
                SupplierId                          = 0,
                ArticleInformationId                = 1,
                InternalArticleInformationId        = 1,
                VailedForCustomer                   = "Customer",
                DateCreated                         = DateTime.Now,
                ArticleInformationCompleted         = 0,
                InternalArticalInformationCompleted = 0,
                ArticleState                        = 0,
                ErrorReported                       = 0,
                ErrorField                          = "Field",
                ErrorMessage                        = "Message",
                ErrorOwner                          = "Owner"
            };
            // act
            Article result = await repository.DeleteArticleAsync(article.Id);

            // assert
            Assert.IsTrue(result == null);

            dbContext.Dispose();
        }
Exemple #2
0
        // DELETE: api/article/5
        public async Task <IHttpActionResult> Delete(int id)
        {
            var user = await userRepository.GetUserAsync(User.Identity.Name);

            if (user.UserType != DataAccess.Model.UserType.Publisher)
            {
                return(StatusCode(HttpStatusCode.Forbidden));
            }
            await articleRepository.DeleteArticleAsync(id);

            return(Ok());
        }
Exemple #3
0
        /// <summary>
        /// 삭제 -> 모달 폼 닫기 -> 선택했던 데이터 초기화 -> 전체 데이터 다시 읽어오기 -> Refresh
        /// </summary>
        private async void btnDelete_Click()
        {
            await ArticleRepository.DeleteArticleAsync(_article.Id);                   // 삭제

            await JSRuntime.InvokeAsync <object>("closeModal", "articleDeleteDialog"); // _Host.cshtml

            _article = new Article();                                                  // 선택 항목 초기화

            var pagingData = await ArticleRepository.GetAllAsync(pager.PageIndex, pager.PageSize);

            pager.RecordCount = pagingData.TotalRecords;
            articles          = pagingData.Records.ToList();

            StateHasChanged();
        }
        public async Task <ActionResult <Article> > DeleteArticle(int id)
        {
            try
            {
                var articleToDelete = await _articleRepository.GetArticle(id);

                if (articleToDelete == null)
                {
                    return(NotFound($"Article with id = {id} not found"));
                }

                return(await _articleRepository.DeleteArticleAsync(id));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Error retrieving data from the database."));
            }
        }
Exemple #5
0
        public async Task ArticleRepositoryAllMethodTest()
        {
            var options = new DbContextOptionsBuilder <ArticleAppDbContext>()
                          //    .UseInMemoryDatabase(databaseName: "ArticlaApp").Options;
                          .UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=ArticleApp;Trusted_Connection=True;").Options;


            //AddAsync() Method Test
            using (var context = new ArticleAppDbContext(options))
            {
                //Repository Object Creation
                var repository = new ArticleRepository(context);
                var model      = new Article {
                    Title = "[1] 게시판 시작", Created = DateTime.Now
                };
                await repository.AddArticleAsync(model);

                await context.SaveChangesAsync();
            }
            using (var context = new ArticleAppDbContext(options))
            {
                Assert.AreEqual(1, await context.Articles.CountAsync());
                var model = await context.Articles.Where(m => m.Id == 1).SingleOrDefaultAsync();

                Assert.AreEqual("[1] 게시판 시작", model?.Title);
            }

            //GetAllAsync() Method Test
            using (var context = new ArticleAppDbContext(options))
            {
                var repository = new ArticleRepository(context);
                var model      = new Article {
                    Title = "[2] 게시판 가동", Created = DateTime.Now
                };
                await context.SaveChangesAsync();

                context.Articles.Add(model);
                context.Articles.Add(new Article {
                    Title = "[3] 게시판 중지", Created = DateTime.Now
                });
                await context.SaveChangesAsync();
            }

            using (var context = new ArticleAppDbContext(options))
            {
                var repository = new ArticleRepository(context);
                var models     = await repository.GetArticlesAsync();

                Assert.AreEqual(3, models.Count);
            }

            //GetByIdAsync() Method Test
            using (var context = new ArticleAppDbContext(options))
            {
            }

            using (var context = new ArticleAppDbContext(options))
            {
                var repository = new ArticleRepository(context);
                var models     = await repository.GetArticleByIdAsync(2);

                Assert.IsTrue(models.Title.Contains("가동"));
                Assert.AreEqual("[2] 게시판 가동", models.Title);
            }


            //GetEditAsync() Method Test
            using (var context = new ArticleAppDbContext(options))
            {
            }

            using (var context = new ArticleAppDbContext(options))
            {
                var repository = new ArticleRepository(context);
                var models     = await repository.GetArticleByIdAsync(2);

                models.Title = "[2] 게시판 바쁨";
                await repository.EditArticleAsync(models);

                await context.SaveChangesAsync();

                Assert.AreEqual("[2] 게시판 바쁨", (await context.Articles.Where(m => m.Id == 2).SingleOrDefaultAsync()).Title);
            }

            //GetDeleteAsync() Method Test
            using (var context = new ArticleAppDbContext(options))
            {
            }

            using (var context = new ArticleAppDbContext(options))
            {
                var repository = new ArticleRepository(context);

                await repository.DeleteArticleAsync(2);

                await context.SaveChangesAsync();

                Assert.AreEqual(2, await context.Articles.CountAsync());
                Assert.IsNull(await repository.GetArticleByIdAsync(2));
            }


            //pagingAsync() Method Test
            using (var context = new ArticleAppDbContext(options))
            {
            }

            using (var context = new ArticleAppDbContext(options))
            {
                int pageIndex = 0;
                int pageSize  = 1;

                var repository = new ArticleRepository(context);
                var models     = await repository.GetAllAsync(pageIndex, pageSize);

                await context.SaveChangesAsync();

                Assert.AreEqual("[3] 게시판 중지", models.Records.FirstOrDefault().Title);
                Assert.AreEqual(2, models.TotalRecords);
            }
        }
Exemple #6
0
        public async Task ArticleRepositoryAllMethodTest()
        {
            //[0] DbContextOptions<T> Object Creation
            var options = new DbContextOptionsBuilder <ArticleAppDbContext>()
                          .UseInMemoryDatabase(databaseName: $"ArticleApp{Guid.NewGuid()}").Options;

            //.UseSqlServer("server=(localdb)\\mssqllocaldb;database=ArticleApp;integrated security=true;").Options;

            //[1] AddAsync() Method Test
            //[1][1] Repository 클래스를 사용하여 저장
            using (var context = new ArticleAppDbContext(options))
            {
                // Repository Object Creation
                //[!] Arrange
                var repository = new ArticleRepository(context);
                var model      = new Article {
                    Title = "[1] 게시판 시작", Created = DateTime.Now
                };

                //[!] Act: AddAsync() 메서드 테스트
                await repository.AddArticleAsync(model);

                await context.SaveChangesAsync(); // 이 코드는 생략 가능
            }
            //[1][2] DbContext 클래스를 통해서 개수 및 레코드 확인
            using (var context = new ArticleAppDbContext(options))
            {
                //[!] Assert
                Assert.AreEqual(1, await context.Articles.CountAsync());

                var model = await context.Articles.Where(m => m.Id == 1).SingleOrDefaultAsync();

                Assert.AreEqual("[1] 게시판 시작", model?.Title);
            }

            //[2] GetAllAsync() Method Test
            using (var context = new ArticleAppDbContext(options))
            {
                // 트랜잭션 관련 코드는 InMemoryDatabase 공급자에서는 지원 X
                // using (var transaction = context.Database.BeginTransaction()) { transaction.Commit(); }

                var repository = new ArticleRepository(context);
                var model      = new Article {
                    Title = "[2] 게시판 가동", Created = DateTime.Now
                };
                await context.Articles.AddAsync(model);

                await context.SaveChangesAsync(); //[1]

                await context.Articles.AddAsync(new Article { Title = "[3] 게시판 중지", Created = DateTime.Now });

                await context.SaveChangesAsync(); //[2]
            }
            using (var context = new ArticleAppDbContext(options))
            {
                var repository = new ArticleRepository(context);
                var models     = await repository.GetArticlesAsync();

                Assert.AreEqual(3, models.Count);
            }

            //[3] GetByIdAsync() Method Test
            using (var context = new ArticleAppDbContext(options))
            {
                // Empty
            }
            using (var context = new ArticleAppDbContext(options))
            {
                var repository = new ArticleRepository(context);
                var model      = await repository.GetArticleByIdAsync(2);

                Assert.IsTrue(model.Title.Contains("가동"));
                Assert.AreEqual("[2] 게시판 가동", model.Title);
            }

            //[4] GetEditAsync() Method Test
            using (var context = new ArticleAppDbContext(options))
            {
                // Empty
            }
            using (var context = new ArticleAppDbContext(options))
            {
                var repository = new ArticleRepository(context);
                var model      = await repository.GetArticleByIdAsync(2);

                model.Title = "[2] 게시판 바쁨";
                await repository.EditArticleAsync(model);

                await context.SaveChangesAsync(); // 생략가능 - 저장 시점을 코드로 표현하기 위함

                Assert.AreEqual("[2] 게시판 바쁨",
                                (await context.Articles.Where(m => m.Id == 2).SingleOrDefaultAsync()).Title);
            }

            //[5] GetDeleteAsync() Method Test
            using (var context = new ArticleAppDbContext(options))
            {
                // Empty
            }
            using (var context = new ArticleAppDbContext(options))
            {
                var repository = new ArticleRepository(context);
                await repository.DeleteArticleAsync(2);

                await context.SaveChangesAsync();

                Assert.AreEqual(2, await context.Articles.CountAsync());
                Assert.IsNull(await repository.GetArticleByIdAsync(2));
            }

            //[6] PagingAsync() Method Test
            using (var context = new ArticleAppDbContext(options))
            {
                // Empty
            }
            using (var context = new ArticleAppDbContext(options))
            {
                int pageIndex = 0;
                int pageSize  = 1;

                var repository = new ArticleRepository(context);
                var models     = await repository.GetAllAsync(pageIndex, pageSize);

                Assert.AreEqual("[3] 게시판 중지", models.Records.FirstOrDefault().Title);
                Assert.AreEqual(2, models.TotalRecords);
            }
        }
        public async Task ArticleRepositoryAllMethodTest()
        {
            // DbContextOption<T> Object Creation
            var options = new DbContextOptionsBuilder <ArticleAppDbContext>()
                          .UseInMemoryDatabase(databaseName: "ArticleApp").Options; //트렌젝션은 InMemory에서 작동되지 않음

            //.UseSqlServer("server=(localdb)\\mssqllocaldb;database=ArticleApp;integrated security=true").Options;   //실제 sql서버에 올라가는거라 재실행시 에러뜸, 값이 들어있기때문


            // AddAsync() Method Test
            using (var context = new ArticleAppDbContext(options))
            {
                // Repository Object Creation
                var repository = new ArticleRepository(context);
                var model      = new Article {
                    Title = "[1] 게시판 시작", Created = DateTime.Now
                };
                await repository.AddArticleAsync(model);

                await context.SaveChangesAsync();
            }
            using (var context = new ArticleAppDbContext(options))
            { //InMemory는 휘발성이기에 여러번 태스트해도 매번 처음실행하는것처럼 됨
                Assert.AreEqual(1, await context.Articles.CountAsync());
                var model = await context.Articles.Where(m => m.Id == 1).SingleOrDefaultAsync();

                Assert.AreEqual("[1] 게시판 시작", model?.Title);
            }

            // GetAllAsync() Method Test
            using (var context = new ArticleAppDbContext(options))
            {
                // InMemory 테스트용

                // Repository Object Creation
                var repository = new ArticleRepository(context);
                var model      = new Article {
                    Title = "[2] 게시판 가동", Created = DateTime.Now
                };
                // await repository.AddArticleAsync(model);  AddArticleAsync 테스트는 위에서 했으니 다른방법으로, 이거 써도 되긴함
                context.Articles.Add(model);
                await context.SaveChangesAsync(); //[1]

                context.Articles.Add(new Article {
                    Title = "[3] 게시판 중지", Created = DateTime.Now
                });
                await context.SaveChangesAsync(); //[2]

                // 현제까지 총 3개의 게시글


                /* 트랜젝션
                 * using (var transaction = context.Database.BeginTransaction())   //트렌젝션은 InMemory에서 작동되지 않음
                 * {
                 *  try
                 *  {
                 *      // Repository Object Creation
                 *      var repository = new ArticleRepository(context);
                 *      var model = new Article { Title = "[2] 게시판 가동", Created = DateTime.Now };
                 *      // await repository.AddArticleAsync(model);  AddArticleAsync 테스트는 위에서 했으니 다른방법으로, 이거 써도 되긴함
                 *      context.Articles.Add(model);
                 *      await context.SaveChangesAsync(); //[1]
                 *      context.Articles.Add(new Article { Title = "[3] 게시판 중지", Created = DateTime.Now });
                 *      await context.SaveChangesAsync(); //[2]
                 *      // 현제까지 총 3개의 게시글
                 *
                 *      transaction.Commit();
                 *  }
                 *  catch (Exception)
                 *  {
                 *      //empty
                 *  }
                 * }
                 */
            }
            using (var context = new ArticleAppDbContext(options))
            {
                var repository = new ArticleRepository(context);
                var models     = await repository.GetArticlesAsync(); // 전체레코드 읽어오기, 3개여야함

                Assert.AreEqual(3, models.Count);
            }

            // GetByIdAsync() Method Test
            using (var context = new ArticleAppDbContext(options))
            {
                // Empty
            }
            using (var context = new ArticleAppDbContext(options))
            {
                var repository = new ArticleRepository(context);
                var model      = await repository.GetArticleByIdAsync(2);

                Assert.IsTrue(model.Title.Contains("가동"));
                //Assert.IsTrue(model.Title.Contains("중지")); 에러뜨는거 확인
                Assert.AreEqual("[2] 게시판 가동", model.Title);
            }

            // GetEditAsync() Method Test
            using (var context = new ArticleAppDbContext(options))
            {
                // Empty
            }
            using (var context = new ArticleAppDbContext(options))
            {
                var repository = new ArticleRepository(context);
                var model      = await repository.GetArticleByIdAsync(2);

                model.Title = "[2] 게시판 바쁨";
                await repository.EditArticleAsync(model);

                await context.SaveChangesAsync();

                Assert.AreEqual("[2] 게시판 바쁨",
                                (await context.Articles.Where(m => m.Id == 2).SingleOrDefaultAsync()).Title);
            }

            // GetDeleteAsync() Method Test
            using (var context = new ArticleAppDbContext(options))
            {
                // Empty
            }
            using (var context = new ArticleAppDbContext(options))
            {
                var repository = new ArticleRepository(context);
                await repository.DeleteArticleAsync(2);

                await context.SaveChangesAsync();

                Assert.AreEqual(2, await context.Articles.CountAsync()); // 3->2, 2개 있어야함
                Assert.IsNull(await repository.GetArticleByIdAsync(2));  // 2번 게시글 삭제됬으니 널값이 와야함
            }

            // PagingAsync() Method Test
            using (var context = new ArticleAppDbContext(options))
            {
                // Empty
            }
            using (var context = new ArticleAppDbContext(options))
            {
                int pageIndex = 0;
                int pageSize  = 1;

                var repository = new ArticleRepository(context);
                var models     = await repository.GetAllAsync(pageIndex, pageSize);

                Assert.AreEqual("[3] 게시판 중지", models.Records.FirstOrDefault().Title);
                Assert.AreEqual(2, models.TotalRecords);
            }
        }