public ActionResult PostComment(CommentBindingModel cbm) { this.service.PostComment(cbm, User.Identity.Name); var commentsVM = this.service.GetCommentsVms(cbm.YoutubeId); return(this.PartialView("~/Views/Shared/Partials/CommentsPartial.cshtml", commentsVM)); }
public async Task <ServiceResult> Create(User user, Movie movie, CommentBindingModel model) { var result = new ServiceResult(); var comment = new Comment { Movie = movie, MovieId = movie.Id, User = user, UserId = user.Id, DateCreated = DateTime.Now }; Mapper.Map(model, comment); try { await this.db.Comments.AddAsync(comment); await this.db.SaveChangesAsync(); result.Succeeded = true; } catch (DbUpdateException ex) { result.Succeeded = false; result.Error = ex.Message; } return(result); }
public void PostPutInShoppingCartTest() { Database.SetInitializer(new BikePortalDbTestInitializer()); var container = UnityConfig.Container; var bikeBll = container.Resolve <IBikeBll>(); var userBll = new Mock <IUserBll>(); var mapper = BikePortalMapper.Create(); var articleController = new BikeController(bikeBll, mapper, userBll.Object); articleController.Request = new HttpRequestMessage(); var bikes = articleController.Get(); var firstBikeId = bikes.First().Id; var comments = articleController.GetComments(firstBikeId).ToList(); var user = User.Create("name", "name"); userBll.Setup(u => u.GetUser(It.IsAny <string>())).Returns(user); var commentText = "hello wordl"; var commentBindingModel = new CommentBindingModel { CommentText = commentText }; var responseTask = articleController.PostComment(firstBikeId, commentBindingModel); var message = responseTask.ExecuteAsync(new CancellationToken()).Result; Assert.AreEqual(message.StatusCode, HttpStatusCode.OK); var updatedBikeComments = articleController.GetComments(firstBikeId).ToList(); Assert.AreEqual(comments.Count() + 1, updatedBikeComments.Count()); }
public ActionResult AddComment(CommentBindingModel model) { if (model != null && this.ModelState.IsValid) { model.UserId = this.User.Identity.GetUserId(); model.PostedOn = DateTime.Now; var comment = Mapper.Map<Comment>(model); var author = this.Data.Users.All().FirstOrDefault(u => u.Id == model.UserId); comment.Author = author; this.Data.Comments.Add(comment); this.Data.SaveChanges(); var commentDb = this.Data.Comments .All() .Where(x => x.Id == comment.Id) .Project() .To<CommentViewModel>() .FirstOrDefault(); return this.PartialView("DisplayTemplates/CommentViewModel", commentDb); } return this.RedirectToAction("Error404", "Home"); }
public IHttpActionResult PostComment(Guid challengeId, CommentBindingModel model) { if (ModelState.IsValid) { try { var challenge = RepositoryProvider.Get <ChallengeRepository>().GetById(challengeId); if (challenge == null) { return(NotFound()); } var comment = new Comment(); comment.Caption = model.Caption; comment.CommentId = Guid.NewGuid(); comment.CreateDate = DateTime.Now; comment.ReferenceId = challengeId.ToString(); comment.Type = Comment.CommentType.ChallengeComment; comment.UserId = CurrentAccess.UserId; RepositoryProvider.Get <CommentRepository>().Insert(comment); RepositoryProvider.Save(); return(Ok()); } catch (Exception exception) { return(InternalServerError(exception)); } } return(BadRequest(ModelState)); }
public void CreateOrUpdate(CommentBindingModel model) { using (var context = new DatabaseContext()) { Comment element; if (model.Id.HasValue) { element = context.Comments.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new Comment(); context.Comments.Add(element); } element.Author = model.Author; element.BlogId = model.BlogId; element.CreationDate = model.CreationDate; element.Header = model.Header; element.Text = model.Text; context.SaveChanges(); } }
public ActionResult SendComment(CommentBindingModel model) { if (model == null || !this.ModelState.IsValid) { return new HttpStatusCodeResult(400, "Invalid user input"); } var comment = new Comment { AuthorId = this.User.Identity.GetUserId(), CommentedAt = DateTime.Now, Content = model.Content, AdId = model.AdId, RecipientId = model.RecipientId }; this.Data.Comments.Add(comment); this.Data.SaveChanges(); this.HubContext.Clients.User(this.User.Identity.GetUserName()).getNotificationsCount(); return this.Json( "Successfully sended your comment!", JsonRequestBehavior.AllowGet); }
public ActionResult AddComment(CommentBindingModel model) { if (model != null && this.ModelState.IsValid) { model.UserId = this.User.Identity.GetUserId(); model.PostedOn = DateTime.Now; var comment = Mapper.Map <Comment>(model); var author = this.Data.Users.All().FirstOrDefault(u => u.Id == model.UserId); comment.Author = author; this.Data.Comments.Add(comment); this.Data.SaveChanges(); var commentDb = this.Data.Comments .All() .Where(x => x.Id == comment.Id) .Project() .To <CommentViewModel>() .FirstOrDefault(); return(this.PartialView("DisplayTemplates/CommentViewModel", commentDb)); } return(this.RedirectToAction("Error404", "Home")); }
public async Task <IActionResult> Update(string id, [FromBody] CommentBindingModel model) { if (ModelState.IsValid) { var movie = this._movieService.GetMovieById(model.movieId); var comment = this._commentsService.GetCommentById(id); if (movie == null || comment == null) { return(NotFound()); } var user = await this._userManager.FindByNameAsync(User.Identity.Name); if (comment.UserId != user.Id) { return(BadRequest()); } var result = await this._commentsService.Update(comment, model); if (result.Succeeded) { return(Ok()); } } return(BadRequest()); }
public async Task UpdateShouldUpdateComment() { // Arrange var db = this.GetDatabase(); var comment = new Comment() { Title = "asdfgw56y345h", Text = "dfgsdfgsdfgf" }; var model = new CommentBindingModel() { Title = "I Like this movie", Text = "Its a great movie" }; db.AddRange(comment); await db.SaveChangesAsync(); var commentsService = new CommentsService(db); // Act var result = await commentsService.Update(comment, model); // Assert result.Succeeded .Should() .Be(true); db.Comments .Should() .Match(r => r.Any(c => c.Title == model.Title && c.Text == model.Text)); }
public void PostComment_ShouldReturnPartialWithValidCommentViewModels() { //setup var bindingModel = new CommentBindingModel() { Content = "something said" }; var commentsEnumeration = new List <CommentViewModel>() { new CommentViewModel() { Author = "user", Content = "something said" }, new CommentViewModel() { Author = "secondUser", Content = "something else" } }; this.homeServiceMock.Setup(x => x.GetCommentsVms(It.IsAny <string>())).Returns(commentsEnumeration); //act and assert this.userController .WithCallTo(x => x.PostComment(bindingModel)) .ShouldRenderPartialView("~/Views/Shared/Partials/CommentsPartial.cshtml") .WithModel <IEnumerable <CommentViewModel> >(comments => { Assert.IsNotNull(comments); }); }
public async Task <IActionResult> AddComment([FromBody] CommentBindingModel model) { if (ModelState.IsValid) { var userId = HttpContext.User.GetUserId(); var user = _context.User.FirstOrDefault(x => x.Id == userId); _context.Comment.Add( new Comment() { Content = model.Content, UserId = userId, User = user, PostId = model.PostId }); await _context.SaveChangesAsync(); //var comment = _context.Comment.LastOrDefault(); //var post = _context.Post.FirstOrDefault(x => x.Id == model.PostId); //post.Comments.Add(comment); await _context.SaveChangesAsync(); return(Ok()); } return(HttpBadRequest()); }
public IHttpActionResult PostAComment( [FromUri] int id, [FromBody] CommentBindingModel commentModel) { if (commentModel == null) { return(this.BadRequest("Comment data is required")); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var bug = this.Data.Bugs.Find(id); if (bug == null) { return(this.NotFound()); } var userId = this.User.Identity.GetUserId(); User user = null; if (userId != null) { user = this.Data.Users.Find(id); } var comment = new Comment() { Text = commentModel.Text, PublishDate = DateTime.Now, Author = user, BugId = bug.Id, Bug = bug }; bug.Comments.Add(comment); this.Data.Comments.Add(comment); this.Data.Bugs.Update(bug); this.Data.SaveChanges(); if (user == null) { return(this.Ok(new { Id = comment.Id, Message = "Added anonimous comment for bug #" + bug.Id })); } return(this.Ok(new { Id = comment.Id, Author = user.UserName, Message = "User comment added for bug #" + bug.Id })); }
public ActionResult Add(CommentBindingModel model) { string urlAbsolute = this.Request.UrlReferrer.AbsolutePath; var snippetId = int.Parse(Regex.Replace(urlAbsolute, @"[^\d+]", "")); if (model != null && this.ModelState.IsValid) { model.UserId = this.UserProfile.Id; model.CreationTime = DateTime.Now; model.SnippetId = snippetId; var comment = Mapper.Map <Comment>(model); this.Data.Comments.Add(comment); this.Data.SaveChanges(); var commentFromDb = this.Data.Comments .All() .Where(c => c.Id == comment.Id) .ProjectTo <CommentDetailsViewModel>() .FirstOrDefault(); return(this.PartialView("DisplayTemplates/CommentDetailsViewModel", commentFromDb)); } return(new EmptyResult()); }
public ActionResult Add(CommentBindingModel model) { if (model != null && this.ModelState.IsValid) { var snippet = this.Data.Snippets.Find(model.SnippetId); if (snippet == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Invalid comment!")); } string currentUserId = this.User.Identity.GetUserId(); var comment = new Comment { AuthorId = currentUserId, SnippetId = snippet.Id, CreatedOn = DateTime.Now, Content = model.Content }; this.Data.Comments.Add(comment); this.Data.SaveChanges(); var commentViewModel = this.Data.Comments .All() .Where(x => x.Id == comment.Id) .Project() .To <CommentMinifiedViewModel>() .FirstOrDefault(); return(this.PartialView("DisplayTemplates/CommentMinifiedViewModel", commentViewModel)); } return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Invalid comment!")); }
public IHttpActionResult AddComment(int id, CommentBindingModel comment) { if (ModelState.IsValid) { return(Ok(_eventService.AddComment(id, User.Identity.Name, comment))); } return(BadRequest()); }
public async Task AddComment(CommentBindingModel model) { var comment = Mapper.Map <CommentBindingModel, Comment>(model); comment.CommentedOn = DateTime.Now; await this.service.AddComment(comment); }
public void Insert(CommentBindingModel model) { using (var context = new NewsBlogDatabase()) { context.Comments.Add(CreateModel(model, new Comments())); context.SaveChanges(); } }
private Comments CreateModel(CommentBindingModel model, Comments comment) { comment.Comment = model.Comment; comment.Date = model.DateCreate; comment.Iduser = model.UserId; comment.Idarticle = model.ArticleId; return(comment); }
public IActionResult AddToPost(string postId) { var model = new CommentBindingModel() { SourceId = postId, PostId = postId }; return(View(model)); }
public void AddComment(CommentBindingModel bm) { Opinion opinion = Mapper.Map <Opinion>(bm); opinion.Book = this.Context.Books.Find(bm.BookId); opinion.User = this.Context.Users.Find(bm.UserId); opinion.CreatedOn = DateTime.Now; this.Context.Opinions.Add(opinion); this.Context.SaveChanges(); }
public ActionResult Comment([Bind(Include = "Comment,BookId,UserId")] CommentBindingModel bm) { if (ModelState.IsValid) { this.service.AddComment(bm); return(RedirectToAction("about", routeValues: new { id = bm.BookId })); } TempData["AddedComment"] = $"The comment is not created!"; return(RedirectToAction("about", routeValues: new { id = bm.BookId })); }
private CommentBindingModel CreateCommentModel(string commentId) { var model = new CommentBindingModel() { SourceId = commentId, Description = Guid.NewGuid().ToString() }; return(model); }
private CommentBindingModel CreateCommentModel(string postId, string description) { var model = new CommentBindingModel() { SourceId = postId, Description = description }; return(model); }
public IHttpActionResult AddComment(int id, CommentBindingModel model) { if (!ModelState.IsValid) { return(this.BadRequest(ModelState)); } var userId = RequestContext.Principal.Identity.GetUserId(); var allIssueComments = issuesService.AddComment(id, userId, model); return(CreatedAtRoute("GetIssueComments", new { id = id }, allIssueComments)); }
//Удалить комментарий public void Delete(CommentBindingModel model) { var element = _commentStorage.GetElement(new CommentBindingModel { Id = model.Id }); if (element == null) { throw new Exception("Элемент не найден"); } _commentStorage.Delete(model); }
public void Update(CommentBindingModel model) { using (var context = new NewsBlogDatabase()) { var comment = context.Comments.FirstOrDefault(rec => rec.Idcomment == model.Id); if (comment == null) { throw new Exception("Комментарий не найден"); } CreateModel(model, comment); context.SaveChanges(); } }
public async Task WithModelWithIncorrectCommentId_ShouldReturnFalse() { var unitOfWork = this.GetRedditCloneUnitOfWork(); var randomCommentId = Guid.NewGuid().ToString(); var model = new CommentBindingModel() { SourceId = randomCommentId }; var result = await this.CallAddResponseToCommentAsync(unitOfWork, model); Assert.False(result); }
public List <CommentViewModel> Read(CommentBindingModel model) { if (model == null) { return(_commentStorage.GetFullList()); } if (model.Id.HasValue) { return(new List <CommentViewModel> { _commentStorage.GetElement(model) }); } return(_commentStorage.GetFilteredList(model)); }
public ActionResult Reply(CommentBindingModel model) { if (model != null && this.ModelState.IsValid) { var comment = Mapper.Map<Comment>(model); comment.CommentedAt = DateTime.Now; comment.AuthorId = this.UserProfile.Id; this.Data.Comments.Add(comment); this.Data.SaveChanges(); return this.Json(new { msg = "Successfully replied"}, JsonRequestBehavior.AllowGet); } return this.Json(new {msg ="Error during sending a reply."}, JsonRequestBehavior.AllowGet); }
public IHttpActionResult AddCommentToBug(int id, CommentBindingModel comment) { if (comment == null) { return this.BadRequest(); } if (string.IsNullOrEmpty(comment.Text)) { return this.BadRequest(); } var bug = this.data.Bugs.Find(id); if (bug == null) { return this.NotFound(); } var newComment = new Comment { BugId = bug.Id, Text = comment.Text, DateCreated = DateTime.Now, }; var author = this.User.Identity.GetUserId(); if (author != null) { newComment.AuthorId = this.User.Identity.GetUserId(); } data.Comments.Add(newComment); data.SaveChanges(); if (author != null) { return this.Ok(new { Id = newComment.Id, Author = this.User.Identity.GetUserName(), Message = string.Format("User comment added for bug #{0}", bug.Id) }); } return this.Ok(new { Id = newComment.Id, Message = string.Format("Added anonymous comment for bug #{0}", bug.Id) }); }
public void PostComment(CommentBindingModel cbm, string activeUser) { var video = this.data.Videos.All().FirstOrDefault(x => x.YoutubeId == cbm.YoutubeId); var comment = new Comment() { Author = this.data.Users.All().First(x => x.UserName == activeUser), Content = cbm.Content, Video = video, PostedOn = DateTime.Now }; this.data.Comments.Add(comment); this.data.SaveChanges(); }
public async Task <IActionResult> Create(int postId, CommentBindingModel model) { if (!ModelState.IsValid) { return(BadRequest()); } var userId = this.userManager.GetUserId(this.User); await this.comments.CreateAsync(userId, postId, model.Content); this.TempData.AddSuccessMessage("Comment succesfully posted!"); return(RedirectToAction("Details", "Posts", new { id = postId })); }
public async Task <IHttpActionResult> PostComment(long id, CommentBindingModel comment) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Comment entity = await this._service.CreateCommentAsync(id, User.Identity.GetUserId(), comment.Content); if (entity == null) { return(NotFound()); } return(Ok(entity)); }
public IHttpActionResult AddCommentToPigeon(int pigeonId, CommentBindingModel inputComment) { if (inputComment == null) { return this.BadRequest(CommentPostModelInvalidMessage); } var loggedUserId = this.User.Identity.GetUserId(); var loggedUser = this.Data.Users.GetById(loggedUserId); var pigeon = this.Data.Pigeons.GetById(pigeonId); if (pigeon == null) { return this.NotFound(); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var commentToAdd = new Comment { Content = inputComment.Content, AuthorId = loggedUserId, Author = loggedUser, PigeonId = pigeon.Id, Pigeon = pigeon, CreatedOn = DateTime.Now }; this.Data.Comments.Add(commentToAdd); pigeon.Comments.Add(commentToAdd); pigeon.CommentsCount++; this.Data.Pigeons.Update(pigeon); this.Data.SaveChanges(); var commentViewModel = CommentViewModel.CreateSingle(commentToAdd); return this.Ok(commentViewModel); }
public ActionResult AddComment(CommentBindingModel model) { if (model != null && this.ModelState.IsValid) { model.AuthorId = this.User.Identity.GetUserId(); model.CreationTime = DateTime.Now; var comment = Mapper.Map<Comment>(model); this.Data.Comments.Add(comment); this.Data.SaveChanges(); var commentDb = Data.Comments .All() .Where(x => x.Id == comment.Id) .Project() .To<DetailsCommentViewModel>() .FirstOrDefault(); return this.PartialView("DisplayTemplates/DetailsCommentViewModel", commentDb); } return this.Json("Error"); }
public IHttpActionResult EditPigeonComment(int pigeonId, int commentId, CommentBindingModel inputComment) { if (inputComment == null) { return this.BadRequest(CommentEditModelInvalidMessage); } var loggedUserId = this.User.Identity.GetUserId(); var pigeon = this.Data.Pigeons.GetById(pigeonId); if (pigeon == null) { return this.NotFound(); } if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var commentToUpdate = pigeon.Comments .FirstOrDefault(c => c.Id == commentId); if (commentToUpdate == null) { return this.NotFound(); } if (commentToUpdate.AuthorId != loggedUserId) { return this.Unauthorized(); } commentToUpdate.Content = inputComment.Content; this.Data.SaveChanges(); return this.Ok(new { commentToUpdate.Id, commentToUpdate.Content }); }
// GET api/comments // public IHttpActionResult Get() // { // var currentUserName = this.User.Identity.Name; // var result = this.comments // .GetAll(currentUserName) // .ProjectTo<CommentViewModel>() // .ToList(); // return this.Ok(result); // } // GET api/comments/all // public IHttpActionResult Get(int page = 1, int pageSize = GlobalConstants.DefaultPageSize) // { // var currentUserName = this.User.Identity.Name; // var result = this.comments // .GetAll(currentUserName) // .Skip((page - 1) * pageSize) // .Take(pageSize) // .ProjectTo<CommentViewModel>() // .ToList(); // return this.Ok(result); // } // POST api/comments //[Authorize] public IHttpActionResult Post(CommentBindingModel commentsRequestModel) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var newComment = Mapper.Map<Comment>(commentsRequestModel); int commentId; try { commentId = this.comments.Add(newComment); } catch (ArgumentNullException) { return this.NotFound(); } return this.Ok(commentId); }