Ejemplo n.º 1
0
        public ActionResult AssessArticle(EstimateUpdate model)
        {
            var articleId = model.ArticleId;
            var estimate  = model.Estimate;

            var article = dbContext.Article.FirstOrDefault(a => a.Id == articleId);

            if (article == null)
            {
                return(BadRequest("The requested article does not exist in the data base"));
            }

            // If a user can create a comment according to a current status of an article
            if (article.Status != ArticleStatus.APPROVED)
            {
                return(BadRequest("The article ought to be approved to have comments"));
            }

            var curUser   = GetUserInfo();
            var curUserId = curUser.id;

            if (article.AuthorId == curUserId)
            {
                return(BadRequest("Authors cannot assess their own articles"));
            }

            var entity = dbContext.UserEstimate.FirstOrDefault(
                e => e.ArticleId == articleId && e.AuthorId == curUserId);
            DateTime now = DateTime.Now;

            if (entity == null)
            {
                entity           = new UserEstimate();
                entity.ArticleId = article.Id;
                entity.AuthorId  = curUserId;

                dbContext.UserEstimate.Add(entity);
            }

            var history = AddHistory(articleId, curUserId, "Estimate.Estimate",
                                     estimate.ToString(), entity.Estimate.ToString(), now, entity.Id);

            entity.Estimate   = estimate;
            entity.InsertDate = DateTime.Now;

            AddNotification($"A user {curUser.name} has assessed an article '{article.Name}'", history, article.AuthorId);

            dbContext.SaveChanges();

            return(Ok(new { finalEstimate = article.GetFinalEstimate() }));
        }
 public async Task<EstimateUpdate.response> EstimateUpdate(EstimateUpdate.request request, CancellationToken? token = null)
 {
     return await SendAsync<EstimateUpdate.response>(request.ToXmlString(), token.GetValueOrDefault(CancellationToken.None));
 }