コード例 #1
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));
     }
 }
コード例 #2
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));
     }
 }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }
コード例 #5
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));
     }
 }