public async Task <IActionResult> Comments(int id, CommentCreateDTO commentCreateDTO) { if (id != commentCreateDTO.PostId) { return(BadRequest()); } int userid = Convert.ToInt32(User.Claims.Where(x => x.Type == "UserId").FirstOrDefault()?.Value); var comment = Mapper.CommentCreateDTOToComment(commentCreateDTO); comment.AuthorId = userid; _commentRepo.Add(comment); if (await _commentRepo.SaveAll()) { var comments = await _commentRepo.GetAll() .Where(c => c.PostId == id) .Include(x => x.Author).ToListAsync(); return(Ok(comments)); } else { return(BadRequest()); } }
public IActionResult Create(CommentCreateDTO req) { var res = new UpdateResponse <CommentDTO>(); var userId = HttpContext.User.Claims.Single(x => x.Type == "Id").Value; var ticket = _ticketRepository.FindById(req.TicketId); if (ticket == null) { res.Success = false; res.Errors.Add("Bad ticket id"); return(BadRequest(res)); } //ticket.Project.ProjectUsersReq.Where( // pur => pur.UserAssigned.Id.Equals(userId) || // pur.Sender.Id.Equals(userId)).ToList().Count <= 0 if (ticket.Project.ProjectUsersReq.Where( pur => pur.UserAssigned.Id.Equals(Guid.Parse(userId)) || pur.Sender.Id.Equals(Guid.Parse(userId))).ToList().Count <= 0) { res.Success = false; res.Errors.Add("User can make comments only on tickets that belong to him"); return(BadRequest(res)); } var projectUser = ticket.Project.ProjectUsersReq.Where( pur => pur.UserAssigned.Id.Equals(Guid.Parse(userId))).FirstOrDefault(); var comment = new Comment(); comment.Commenter = projectUser; comment.Created = DateTime.Now; comment.Message = req.Message; ticket.Comments.Add(comment); _ticketRepository.Update(ticket); try { _uow.Commit(); } catch (Exception ex) { Console.WriteLine(ex.Message); res.Success = false; res.Errors.Add("Server error"); return(StatusCode(StatusCodes.Status500InternalServerError, res)); } res.EntityDTO = _mapper.Map <Comment, CommentDTO>(comment); res.Success = true; return(Ok(res)); }
public ActionResult <CommentReadDTO> CreateComment(CommentCreateDTO commentCreateDTO) { var commentModel = _mapper.Map <Comment>(commentCreateDTO); _commentService.CreateComment(commentModel); _commentService.SaveChanges(); var commentReadDto = _mapper.Map <CommentReadDTO>(commentModel); return(CreatedAtRoute(nameof(GetCommentById), new { Id = commentReadDto.CommentId }, commentReadDto)); }
public static Comment CommentCreateDTOToComment(CommentCreateDTO cc) { return(new Comment { Created = DateTime.Now, Content = cc.Content, PostId = cc.PostId, Public = true, CommentParentId = cc.CommentParentId }); }
public ActionResult Create(CommentCreateDTO dto) { var svc = GetCommentService(); var rao = new CommentCreateRAO { Content = dto.Content, GroupId = dto.GroupId, ParentId = dto.ParentId }; if (svc.CreateComment(rao)) { return(RedirectToAction("Index", "Group", new { id = dto.GroupId })); } return(RedirectToAction("Index", "Group", new { id = dto.GroupId })); }
public IActionResult Create([FromBody] CommentCreateDTO comment) { var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; if (userId == null) { return(Unauthorized()); } var tempComment = new Comment { PostId = comment.PostId, CommenterId = userId, Description = comment.Description }; this.repository.Insert(tempComment); return(Ok()); }
public async Task CreateComment(CommentCreateDTO commentCreate) // Authorized { string serializedUser = JsonConvert.SerializeObject(commentCreate); var requestMessage = new HttpRequestMessage(HttpMethod.Post, ""); requestMessage.Content = new StringContent(serializedUser); requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); string accessToken = await localStorageService.GetItemAsync <string>("accessToken"); requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); var response = await httpClient.SendAsync(requestMessage); var responseBody = await response.Content.ReadAsStringAsync(); var errorMessage = new Regex("(?<=error_description=\").*(?=;)").Match(response.Headers.WwwAuthenticate.ToString()); }
public ActionResult Create(CommentCreateViewModel model) { bool succeeded = false; if (ModelState.IsValid) { model.ArticleId = HttpUtility.UrlDecode(model.ArticleId); CommentCreateDTO commentDTO = Mapper.Map <CommentCreateViewModel, CommentCreateDTO>(model); ServiceMessage serviceMessage = commentService.Create(commentDTO); if (!serviceMessage.Succeeded) { AddModelErrors(serviceMessage.Errors); } succeeded = serviceMessage.Succeeded; } return(JsonOnFormPost(succeeded, "~/Views/Comment/Create.cshtml", model)); }
public async Task <ActionResult> Create([FromBody] CommentCreateDTO commentCreateDTO) { if (commentCreateDTO == null) { return(BadRequest(ModelState)); } var comment = _mapper.Map <Comment>(commentCreateDTO); if (!await _commentRepository.CreateAsync(comment)) { ModelState.AddModelError("", "Error encountered when saving to database, try again. If problem persists contact your administrator"); return(StatusCode(500, ModelState)); } return(CreatedAtAction(nameof(comment), new { id = comment.CommentId }, comment)); }
public async Task <IActionResult> CreateComment(CommentCreateDTO commentCreateDTO, int commentStatus) { if (ModelState.IsValid) { if (!User.Identity.IsAuthenticated) { return(RedirectToAction("Authenticate", "Account")); } var curUser = User.Identity.Name; User user = await _userManager.FindByNameAsync(curUser); commentCreateDTO.Created = DateTime.Now; commentCreateDTO.UserId = user.Id; switch (commentStatus) { case 1: commentCreateDTO.CommentStatus = CommentStatus.Useful; break; case 2: commentCreateDTO.CommentStatus = CommentStatus.Neutral; break; case 3: commentCreateDTO.CommentStatus = CommentStatus.Dangerous; break; } Comment comment = _mapper.Map <Comment>(commentCreateDTO); await _commentService.CreateAsync(comment); return(RedirectToAction("PostDetails", "Post", new { id = commentCreateDTO.PostId })); } return(PartialView(commentCreateDTO)); }
private bool Validate(CommentCreateDTO commentDTO, ICollection <string> errors) { bool isValid = true; if (String.IsNullOrEmpty(commentDTO.Text)) { isValid = false; errors.Add("Text cannot be empty"); } if (String.IsNullOrEmpty(commentDTO.ArticleId)) { isValid = false; errors.Add("Article has to be specified"); } if (String.IsNullOrEmpty(commentDTO.AuthorLogin)) { isValid = false; errors.Add("Author has to be specified"); } return(isValid); }
public async Task AddNewComment(CommentCreateDTO commentDTO) { await Database.CommentRepository.Add(mapper.Map <CommentCreateDTO, Comment>(commentDTO)); }
public ServiceMessage Create(CommentCreateDTO commentDTO) { int articleId; string decryptedArticleId = encryptor.Decrypt(commentDTO.ArticleId); List <string> errors = new List <string>(); bool succeeded = true; if (Int32.TryParse(decryptedArticleId, out articleId)) { if (succeeded = Validate(commentDTO, errors)) { try { ArticleEntity articleEntity = unitOfWork.Articles.Get(articleId); if (articleEntity != null) { ApplicationUser applicationUser = unitOfWork.ApplicationUsers.GetByLogin(commentDTO.AuthorLogin); if (applicationUser != null) { CommentEntity commentEntity = new CommentEntity { Article = articleEntity, Author = applicationUser, DateCreated = DateTime.Now, Text = commentDTO.Text }; unitOfWork.Comments.Add(commentEntity); unitOfWork.Commit(); } else { succeeded = false; errors.Add("User was not found"); } } else { succeeded = false; errors.Add("Article was not found"); } } catch (Exception ex) { succeeded = false; ExceptionMessageBuilder.FillErrors(ex, errors); } } } else { succeeded = false; errors.Add("Article was not found"); } return(new ServiceMessage { Errors = errors, Succeeded = succeeded }); }