Пример #1
0
        //Comments

        public string AddComment(CommentWCF comment)
        {
            logger.Info("Registeres POST request - attempting to add new comment.");
            var response = _userController.Exist(JObject.Parse("{\"username\": \"" + comment.User + "\"}"));

            if ((Boolean)response["exist"])
            {
                var post = _blogContext.Posts.ToList().Find(p => p.PostID == comment.PostID);
                if (post == null)
                {
                    logger.Warn("Cannot add comment - There is no such PostID.");
                    return("Error there is no such PostId");
                }
                else
                {
                    var calories  = getCalories(comment.Content);
                    var content1  = comment.Content + " Counted calories:" + calories;
                    var commentDB = new Comment()
                    {
                        User = comment.User, Content = content1, Date = comment.Date, PostID = comment.PostID
                    };
                    _blogContext.Comments.Add(commentDB);
                    _blogContext.SaveChanges();
                    logger.Info("New comment added.");
                    return("Comment added");
                }
            }
            else
            {
                logger.Info("Cannot add Comment - there is no such user.");
                return("There is no such user");
            }
        }
        private CommentWCF commentWCFChanger(Comment comment)
        {
            var t = new CommentWCF();

            t.Content = comment.Content;
            t.Date    = comment.Date;
            t.User    = comment.User;
            t.PostID  = comment.PostID;
            return(t);
        }
Пример #3
0
        public async Task <string> AddComment(CommentWCF comment)
        {
            var jsonObject = JsonConvert.SerializeObject(comment);
            var content    = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
            HttpResponseMessage response = await _client.PostAsync("Blog/AddComment", content);

            if (response.IsSuccessStatusCode)
            {
                var wcfResponse = await response.Content.ReadAsAsync <string>();

                return(wcfResponse);
            }
            return("Error");
        }