public async void Create_ReturnRedirect() { var size = Context.CommentAndEvaluation.Count(); var comment = new CommentAndEvaluation() { CommentAndEvaluationID = 5, ServiceID = 4, UserCommentingID = "2", Comment = "Serviço impecável", Evaluation = 4, CreationDate = DateTime.Now }; Controller.ControllerContext.HttpContext = new DefaultHttpContext { User = PrestadorLog }; var result = await Controller.Create(comment); Assert.IsType <RedirectToActionResult>(result); //Confirmar Comments var resultIndex = await Controller.Index(); var viewResultIndex = Assert.IsType <ViewResult>(resultIndex); var modelIndex = Assert.IsAssignableFrom <IEnumerable <CommentAndEvaluation> >(viewResultIndex.ViewData.Model); Assert.NotNull(modelIndex); Assert.Equal(size + 1, modelIndex.Count()); }
public async Task <IActionResult> Create([Bind("CommentAndEvaluationID,CreationDate,UserCommentingID,ServiceID,Evaluation,Comment")] CommentAndEvaluation commentAndEvaluation) { var service = _context.Service.Find(commentAndEvaluation.ServiceID); commentAndEvaluation.UserCommenting = await _userManager.GetUserAsync(User); commentAndEvaluation.UserCommentingID = commentAndEvaluation.UserCommenting.Id; commentAndEvaluation.CreationDate = DateTime.Now; commentAndEvaluation.LastUpdate = DateTime.Now; commentAndEvaluation.Service = service; commentAndEvaluation.IsEdited = false; if (ModelState.IsValid) { _context.CommentAndEvaluation.Add(commentAndEvaluation); // Gamification Points increment UpdatePoints(commentAndEvaluation, service, "Increment"); await _context.SaveChangesAsync(); SendNotification(commentAndEvaluation, "Create"); SendEmail(commentAndEvaluation, "Create"); return(RedirectToAction("Details", "Services", new { id = commentAndEvaluation.ServiceID })); } ViewData["ServiceID"] = new SelectList(_context.Service, "ServiceID", "Description", commentAndEvaluation.ServiceID); ViewData["UserCommentingID"] = _userManager.GetUserAsync(User).Result.Id; return(View("/Views/Shared/NotFound.cshtml")); }
/// <summary> /// This method increments or decreases the user total points, according to the action received in params /// </summary> /// <param name="commentAndEvaluation">CommentAndEvaluation object</param> /// <param name="service">Commented/Evaluated service object</param> /// <param name="action">action</param> /// <returns>Returns a Task of an IActionResult</returns> /// <seealso cref="Task"/> /// <seealso cref="IActionResult"/> private void UpdatePoints(CommentAndEvaluation commentAndEvaluation, Service service, String action) { var gamification = _context.Gamification.Where(g => g.IsActive == true).FirstOrDefault(); if (gamification != null) { var points = 0; var user = _context.Users.Where(u => u.Id == service.UserID).FirstOrDefault(); if (commentAndEvaluation.Evaluation > 0) { points += gamification.PointsPerEvaluation; } if (commentAndEvaluation.Comment != null && commentAndEvaluation.Comment != " ") { points += gamification.PointsPerComment; } if (points > 0) { if (action == "Increment") { user.TotalPoints += points; } else { user.TotalPoints -= points; } _context.Users.Update(user); } return; } return; }
public async Task <IActionResult> Edit(int id, [Bind("CommentAndEvaluationID,UserCommentingID,ServiceID,Evaluation,Comment,CreationDate")] CommentAndEvaluation commentAndEvaluation) { if (id != commentAndEvaluation.CommentAndEvaluationID) { return(View("/Views/Shared/NotFound.cshtml")); } if (_userManager.GetUserAsync(User).Result.Id != commentAndEvaluation.UserCommentingID) { return(Unauthorized()); } if (ModelState.IsValid) { try { commentAndEvaluation.LastUpdate = DateTime.Now; commentAndEvaluation.IsEdited = true; _context.Update(commentAndEvaluation); await _context.SaveChangesAsync(); SendNotification(commentAndEvaluation, "Edit"); SendEmail(commentAndEvaluation, "Edit"); } catch (DbUpdateConcurrencyException) { if (!CommentAndEvaluationExists(commentAndEvaluation.CommentAndEvaluationID)) { return(View("/Views/Shared/NotFound.cshtml")); } else { throw; } } return(RedirectToAction("Details", "Services", new { id = commentAndEvaluation.ServiceID })); } ViewData["ServiceID"] = new SelectList(_context.Service, "ServiceID", "Description", commentAndEvaluation.ServiceID); ViewData["UserCommentingID"] = new SelectList(_context.Users, "Id", "Id", commentAndEvaluation.UserCommentingID); return(RedirectToAction("Details", "Services", new { id = commentAndEvaluation.ServiceID }));; }
public async void EditPost_ReturnNotFound(int commentID) { var commentUpdate = new CommentAndEvaluation() { CommentAndEvaluationID = 5, ServiceID = 4, UserCommentingID = "2", Comment = "Serviço impecável", Evaluation = 3, CreationDate = DateTime.Now }; Assert.Equal("Serviço impecável", Context.CommentAndEvaluation.Find(5).Comment); Assert.NotEqual(Context.CommentAndEvaluation.Find(4).Comment, commentUpdate.Comment); Controller.ControllerContext.HttpContext = new DefaultHttpContext { User = PrestadorLog }; var result = await Controller.Edit(commentID, commentUpdate); var viewResult = Assert.IsType <ViewResult>(result); Assert.Equal("/Views/Shared/NotFound.cshtml", viewResult.ViewName); }
/// <summary> /// This method sends the notification to the service provider alerting that a comment has been created or edited. /// </summary> /// <param name="service">Service Requisition object</param> private void SendNotification(CommentAndEvaluation commentAndEvaluation, String action) { var service = _context.Service.Where(s => s.ServiceID == commentAndEvaluation.ServiceID).FirstOrDefault(); NotificationsController notificationController = new(_context, _userManager); Notification notification = new() { DestinaryID = commentAndEvaluation.Service.UserID, Subject = action == "Create" ? "Recebeu um novo comentário no seu serviço" : "Um comentário foi editado no seu serviço ", Content = "O utilizador " + commentAndEvaluation.UserCommenting.FirstName + " " + commentAndEvaluation.UserCommenting.LastName + " " + action == "Create" ? "comentou o seu serviço" : "editou um comentário no seu serviço" + " " + service.ServiceName + ". Aconselhamos que consulte o mesmo para verificar a integridade do comentário.", IsRead = false, Action = "/Services/Details/" + commentAndEvaluation.ServiceID, CreationDate = DateTime.Now, }; _ = notificationController.Create(notification).Result; }
/// <summary> /// Sends email to the provider when a comment is created or edited on one of his services. /// </summary> /// <param name="serviceRequisition">Service Requisition object</param> /// <param name="service">Service object</param> private void SendEmail(CommentAndEvaluation commentAndEvaluation, String action) { try { String initialPhrase = action == "Create" ? "Recebeu um novo comentário no seu serviço " : "Um comentário foi editado no seu serviço "; var emailBody = string.Format("<p>" + initialPhrase + ": <b>{0} </b></p><br><p><b>Feito por:</b> {1} </p><p><b><br>Avaliação:</b> {2} Estrelas." + "<br> <b>Comentário:</b> {3} <br>", commentAndEvaluation.Service.ServiceName, commentAndEvaluation.UserCommenting.UserName, commentAndEvaluation.Evaluation, commentAndEvaluation.Comment); var emailSubject = action == "Create" ? "Existe um novo comentário num dos seus serviços" : "Um comentário foi atualizado no seu serviço"; //Send mail to provider _emailSender.SendEmailAsync(commentAndEvaluation.Service.User.Email, emailSubject, emailBody); } catch (Exception e) { } }