forked from Azure/azure-sdk-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentsController.cs
More file actions
116 lines (97 loc) · 4.72 KB
/
Copy pathCommentsController.cs
File metadata and controls
116 lines (97 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Threading.Tasks;
using APIViewWeb.Hubs;
using APIViewWeb.LeanModels;
using APIViewWeb.Managers;
using APIViewWeb.Managers.Interfaces;
using APIViewWeb.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
namespace APIViewWeb.Controllers
{
[Authorize("RequireOrganization")]
public class CommentsController: Controller
{
private readonly ICommentsManager _commentsManager;
private readonly IReviewManager _reviewManager;
private readonly IHubContext<SignalRHub> _signalRHubContext;
private readonly INotificationManager _notificationManager;
public CommentsController(ICommentsManager commentsManager, IReviewManager reviewManager, INotificationManager notificationManager, IHubContext<SignalRHub> signalRHub)
{
_signalRHubContext = signalRHub;
_commentsManager = commentsManager;
_reviewManager = reviewManager;
_notificationManager = notificationManager;
}
[HttpPost]
public async Task<ActionResult> Add(string reviewId, string revisionId, string elementId, string commentText, string sectionClass, string[] taggedUsers, string resolutionLock = "off", bool usageSampleComment = false, string crossLangId = null, CommentSeverity? severity = null)
{
if (string.IsNullOrEmpty(commentText))
{
var notifcation = new NotificationModel() { Message = "Comment Text cannot be empty. Please type your comment entry and try again.", Level = NotificatonLevel.Error };
await _signalRHubContext.Clients.Group(User.GetGitHubLogin()).SendAsync("RecieveNotification", notifcation);
return new BadRequestResult();
}
var comment = new CommentItemModel();
comment.CreatedOn = DateTime.UtcNow;
comment.ReviewId = reviewId;
comment.APIRevisionId = revisionId;
comment.ElementId = elementId;
comment.SectionClass = sectionClass;
comment.CommentText = commentText;
comment.CommentType = (usageSampleComment) ? CommentType.SampleRevision : CommentType.APIRevision;
comment.ResolutionLocked = !resolutionLock.Equals("on");
comment.CreatedBy = User.GetGitHubLogin();
comment.CrossLanguageId = crossLangId;
comment.Severity = severity;
foreach(string user in taggedUsers)
{
comment.TaggedUsers.Add(user);
}
await _commentsManager.AddCommentAsync(User, comment);
var review = await _reviewManager.GetReviewAsync(User, reviewId);
if (review != null)
{
await _notificationManager.SubscribeAsync(review,User);
}
return await CommentPartialAsync(reviewId, comment.ElementId);
}
[HttpPost]
public async Task<ActionResult> Update(string reviewId, string commentId, string commentText, string[] taggedUsers)
{
var comment = await _commentsManager.UpdateCommentAsync(User, reviewId, commentId, commentText, taggedUsers);
return await CommentPartialAsync(reviewId, comment.ElementId);
}
[HttpPost]
public async Task<ActionResult> Resolve(string reviewId, string elementId)
{
await _commentsManager.ResolveConversation(User, reviewId, elementId);
return await CommentPartialAsync(reviewId, elementId);
}
[HttpPost]
public async Task<ActionResult> Unresolve(string reviewId, string elementId)
{
await _commentsManager.UnresolveConversation(User, reviewId, elementId);
return await CommentPartialAsync(reviewId, elementId);
}
[HttpPost]
public async Task<ActionResult> Delete(string reviewId, string commentId, string elementId)
{
await _commentsManager.SoftDeleteCommentAsync(User, reviewId, commentId);
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);
comments.TryGetThreadForLine(elementId, out var partialModel);
return PartialView("_CommentThreadPartial", partialModel);
}
}
}