Skip to content

Commit

Permalink
Upvote (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym authored Jan 27, 2020
1 parent 3521b32 commit 50578b6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/dotnet/APIView/APIViewWeb/Client/css/site.scss
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ code-inner {
margin: -8px -16px;
}

.btn-upvote {
filter: grayscale(100%) opacity(30%);
&.active {
filter: unset;
}
}

.new-comment-content {
position: relative;
margin: 0 4px 8px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ public async Task<ActionResult> Delete(string reviewId, string commentId, string
return await CommentPartialAsync(reviewId, elementId);
}

[HttpPost]
public async Task<ActionResult> ToggleUpvote(string reviewId, string commentId, string elementId)
{
await _commentsManager.ToggleUpvoteAsync(User, reviewId, commentId);

return await CommentPartialAsync(reviewId, elementId);
}

private async Task<ActionResult> CommentPartialAsync(string reviewId, string elementId)
{
var comments = await _commentsManager.GetReviewCommentsAsync(reviewId);
Expand Down
2 changes: 2 additions & 0 deletions src/dotnet/APIView/APIViewWeb/Models/CommentModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace APIViewWeb.Models
Expand All @@ -15,5 +16,6 @@ public class CommentModel
public string Username { get; set; }
public bool IsResolve { get; set; }
public DateTime? EditedTimeStamp { get; set; }
public List<string> Upvotes { get; set; } = new List<string>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
{
<div id="@comment.CommentId" class="review-comment" data-comment-id="@comment.CommentId">
<div class="comment-actions">
<form data-post-update="comments" asp-controller="Comments" asp-action="ToggleUpvote" method="post" asp-route-reviewId="@Model.ReviewId">
<input type="hidden" name="commentId" value="@comment.CommentId" />
<button type="submit" class="btn btn-light btn-sm btn-upvote" title="@string.Join(", ", comment.Upvotes) " active-if="@comment.Upvotes.Contains(User.GetGitHubLogin())">
@if (comment.Upvotes.Any())
{
@comment.Upvotes.Count
}
👍
</button>
</form>

@if (comment.Username == User.GetGitHubLogin())
{
<div class="dropdown show">
Expand Down
11 changes: 11 additions & 0 deletions src/dotnet/APIView/APIViewWeb/Repositories/CommentsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ public async Task UnresolveConversation(ClaimsPrincipal user, string reviewId, s
}
}

public async Task ToggleUpvoteAsync(ClaimsPrincipal user, string reviewId, string commentId)
{
var comment = await _commentsRepository.GetCommentAsync(reviewId, commentId);

if (comment.Upvotes.RemoveAll(u => u == user.GetGitHubLogin()) == 0)
{
comment.Upvotes.Add(user.GetGitHubLogin());
}

await _commentsRepository.UpsertCommentAsync(comment);
}
private async Task AssertOwnerAsync(ClaimsPrincipal user, CommentModel commentModel)
{
var result = await _authorizationService.AuthorizeAsync(user, commentModel, new[] { CommentOwnerRequirement.Instance });
Expand Down

0 comments on commit 50578b6

Please sign in to comment.