Пример #1
0
        public ArticlePublishResponse Publish(User publisher, Article article)
        {
            DataWriteResult result = _articleRepository.Create(publisher, article);

            ArticlePublishResponse response = result.Success ? ResponseBase.SuccessResponse() : ResponseBase.FailureResponse();

            return(response);
        }
Пример #2
0
        public ArticlePublishResponse LikeArticle(User user, Article article)
        {
            DataWriteResult result = _articleRepository.Like(user, article);

            ArticlePublishResponse response = result.Success ? ResponseBase.SuccessResponse() : ResponseBase.FailureResponse();

            return(response);
        }
        public void Delete_Successful()
        {
            var article = new Article
            {
                Id = 3
            };

            using (new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))
            {
                DataWriteResult result = _articleRepository.Delete(publisher, article);
                Assert.AreEqual(true, result.Success);
                Assert.IsNull(result.Exception);
            }
        }
Пример #4
0
 public DataWriteResult Delete(User publisher, Article article)
 {
     try
     {
         User    dbPublisher = _context.Users.First(u => u.Id == publisher.Id && u.Role == Role.Publisher);
         Article dbArticle   = _context.Articles.FirstOrDefault(a => a.Id == article.Id);
         _context.Articles.Remove(dbArticle);
         _context.SaveChanges();
         return(DataWriteResult.SuccessResult());
     }
     catch (Exception exception)
     {
         return(DataWriteResult.FailureResult(exception));
     }
 }
Пример #5
0
 public DataWriteResult Update(User publisher, Article article)
 {
     try
     {
         User dbPublisher = _context.Users.First(u => u.Id == publisher.Id && u.Role == Role.Publisher);
         article.Publisher = dbPublisher;
         _context.Articles.Attach(article);
         _context.SaveChanges();
         return(DataWriteResult.SuccessResult());
     }
     catch (Exception exception)
     {
         return(DataWriteResult.FailureResult(exception));
     }
 }
        public DataWriteResult Like(User user, Article article)
        {
            DataWriteResult response;

            if (user.Likes > 0)
            {
                article.Likes += 1;
                user.Likes    -= 1;
                response       = DataWriteResult.SuccessResult();
            }
            else
            {
                response = DataWriteResult.FailureResult(new Exception("Insufficient likes remaining"));
            }
            return(response);
        }
        public DataWriteResult Create(User publisher, Article article)
        {
            DataWriteResult response;

            if (article.Author == null)
            {
                response = DataWriteResult.FailureResult(new Exception("Authour null"));
            }
            else
            {
                _articles.Add(article);
                response = DataWriteResult.SuccessResult();
            }
            _articles.Add(article);
            return(response);
        }
        public void Create_Exception()
        {
            var article = new Article
            {
                Title       = "First Sports Article",
                Body        = "Text for article",
                PublishDate = DateTime.Now,
                ArticleType = ArticleType.Sports
            };

            using (new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))
            {
                DataWriteResult result = _articleRepository.Create(publisher, article);
                Assert.AreEqual(false, result.Success);
                Assert.IsNotNull(result.Exception);
            }
        }
        public void Update_Successful()
        {
            var article = new Article
            {
                Id          = 2,
                Title       = "Changed to Sports Article",
                Author      = _context.Authors.First(),
                Body        = "Now a sports article",
                PublishDate = DateTime.Now,
                ArticleType = ArticleType.Sports
            };

            using (new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))
            {
                DataWriteResult result = _articleRepository.Update(publisher, article);
                Assert.AreEqual(true, result.Success);
                Assert.IsNull(result.Exception);
            }
        }
Пример #10
0
 public DataWriteResult Like(User user, Article article)
 {
     try
     {
         if (user.Likes > 0)
         {
             var articleDb = _context.Articles.Include(a => a.Author).Include(p => p.Publisher).First(a => a.Id == article.Id);
             var userDb    = _context.Users.First(u => u.Id == user.Id);
             articleDb.Likes += 1;
             userDb.Likes    -= 1;
             _context.SaveChanges();
             return(DataWriteResult.SuccessResult());
         }
         return(DataWriteResult.FailureResult(new Exception("Insufficient likes remaining")));
     }
     catch (Exception exception)
     {
         return(DataWriteResult.FailureResult(exception));
     }
 }