1- using System . Net ;
21using System . Net . Mime ;
32using CrowdParlay . Social . Api . Extensions ;
43using CrowdParlay . Social . Api . Hubs ;
5- using CrowdParlay . Social . Api . v1 . DTOs ;
64using CrowdParlay . Social . Application . Abstractions ;
75using CrowdParlay . Social . Application . DTOs ;
8- using CrowdParlay . Social . Application . Exceptions ;
96using CrowdParlay . Social . Domain . DTOs ;
10- using CrowdParlay . Social . Domain . ValueObjects ;
117using Microsoft . AspNetCore . Authorization ;
128using Microsoft . AspNetCore . Mvc ;
139using Microsoft . AspNetCore . Mvc . ModelBinding ;
1410using Microsoft . AspNetCore . SignalR ;
11+ using static Microsoft . AspNetCore . Http . StatusCodes ;
1512
1613namespace CrowdParlay . Social . Api . v1 . Controllers ;
1714
1815[ ApiController , ApiRoute ( "[controller]" ) ]
19- public class CommentsController ( ICommentsService comments , IHubContext < CommentsHub > commentHub ) : ControllerBase
16+ public class CommentsController (
17+ ICommentsService commentsService ,
18+ IReactionsService reactionsService ,
19+ IHubContext < CommentsHub > commentHub ) : ControllerBase
2020{
2121 /// <summary>
2222 /// Returns comment with the specified ID.
2323 /// </summary>
2424 [ HttpGet ( "{commentId:guid}" ) ]
2525 [ Consumes ( MediaTypeNames . Application . Json ) , Produces ( MediaTypeNames . Application . Json ) ]
26- [ ProducesResponseType ( typeof ( CommentDto ) , ( int ) HttpStatusCode . OK ) ]
27- [ ProducesResponseType ( typeof ( ProblemDetails ) , ( int ) HttpStatusCode . InternalServerError ) ]
28- [ ProducesResponseType ( typeof ( ProblemDetails ) , ( int ) HttpStatusCode . NotFound ) ]
29- public async Task < CommentDto > GetCommentById ( [ FromRoute ] Guid commentId ) =>
30- await comments . GetByIdAsync ( commentId , User . GetUserId ( ) ) ;
26+ [ ProducesResponseType < CommentResponse > ( Status200OK ) ]
27+ [ ProducesResponseType < ProblemDetails > ( Status404NotFound ) ]
28+ [ ProducesResponseType < ProblemDetails > ( Status500InternalServerError ) ]
29+ public async Task < CommentResponse > GetById ( [ FromRoute ] Guid commentId ) =>
30+ await commentsService . GetByIdAsync ( commentId , User . GetUserId ( ) ) ;
3131
3232 /// <summary>
3333 /// Get comments by filters.
3434 /// </summary>
3535 [ HttpGet ]
3636 [ Consumes ( MediaTypeNames . Application . Json ) , Produces ( MediaTypeNames . Application . Json ) ]
37- [ ProducesResponseType ( typeof ( Page < CommentDto > ) , ( int ) HttpStatusCode . OK ) ]
38- [ ProducesResponseType ( typeof ( ProblemDetails ) , ( int ) HttpStatusCode . InternalServerError ) ]
39- [ ProducesResponseType ( typeof ( ValidationProblemDetails ) , ( int ) HttpStatusCode . BadRequest ) ]
40- public async Task < Page < CommentDto > > SearchComments (
37+ [ ProducesResponseType < Page < CommentResponse > > ( Status200OK ) ]
38+ [ ProducesResponseType < ValidationProblemDetails > ( Status400BadRequest ) ]
39+ [ ProducesResponseType < ProblemDetails > ( Status500InternalServerError ) ]
40+ public async Task < Page < CommentResponse > > Search (
4141 [ FromQuery ] Guid ? discussionId ,
4242 [ FromQuery ] Guid ? authorId ,
4343 [ FromQuery , BindRequired ] int offset ,
4444 [ FromQuery , BindRequired ] int count ) =>
45- await comments . SearchAsync ( discussionId , authorId , User . GetUserId ( ) , offset , count ) ;
45+ await commentsService . SearchAsync ( discussionId , authorId , User . GetUserId ( ) , offset , count ) ;
4646
4747 /// <summary>
4848 /// Creates a top-level comment in discussion.
4949 /// </summary>
5050 [ HttpPost , Authorize ]
5151 [ Consumes ( MediaTypeNames . Application . Json ) , Produces ( MediaTypeNames . Application . Json ) ]
52- [ ProducesResponseType ( typeof ( CommentDto ) , ( int ) HttpStatusCode . Created ) ]
53- [ ProducesResponseType ( typeof ( ProblemDetails ) , ( int ) HttpStatusCode . InternalServerError ) ]
54- [ ProducesResponseType ( typeof ( ValidationProblemDetails ) , ( int ) HttpStatusCode . BadRequest ) ]
55- [ ProducesResponseType ( typeof ( ProblemDetails ) , ( int ) HttpStatusCode . Forbidden ) ]
56- public async Task < ActionResult < CommentDto > > Create ( [ FromBody ] CommentRequest request )
52+ [ ProducesResponseType < CommentResponse > ( Status201Created ) ]
53+ [ ProducesResponseType < ValidationProblemDetails > ( Status400BadRequest ) ]
54+ [ ProducesResponseType < ProblemDetails > ( Status403Forbidden ) ]
55+ [ ProducesResponseType < ProblemDetails > ( Status500InternalServerError ) ]
56+ public async Task < ActionResult < CommentResponse > > Create ( [ FromBody ] CommentRequest request )
5757 {
58- var authorId =
59- User . GetUserId ( )
60- ?? throw new ForbiddenException ( ) ;
61-
62- var response = await comments . CreateAsync ( authorId , request . DiscussionId , request . Content ) ;
58+ var response = await commentsService . CreateAsync ( User . GetRequiredUserId ( ) , request . DiscussionId , request . Content ) ;
6359
6460 _ = commentHub . Clients
6561 . Group ( CommentsHub . GroupNames . NewCommentInDiscussion ( request . DiscussionId ) )
6662 . SendCoreAsync ( CommentsHub . Events . NewComment . ToString ( ) , [ response ] ) ;
6763
68- return CreatedAtAction ( nameof ( GetCommentById ) , new { commentId = response . Id } , response ) ;
64+ return CreatedAtAction ( nameof ( GetById ) , new { commentId = response . Id } , response ) ;
6965 }
7066
7167 /// <summary>
7268 /// Get replies to the comment with the specified ID.
7369 /// </summary>
7470 [ HttpGet ( "{parentCommentId:guid}/replies" ) ]
7571 [ Consumes ( MediaTypeNames . Application . Json ) , Produces ( MediaTypeNames . Application . Json ) ]
76- [ ProducesResponseType ( typeof ( Page < CommentDto > ) , ( int ) HttpStatusCode . OK ) ]
77- [ ProducesResponseType ( typeof ( ProblemDetails ) , ( int ) HttpStatusCode . InternalServerError ) ]
78- [ ProducesResponseType ( typeof ( ValidationProblemDetails ) , ( int ) HttpStatusCode . BadRequest ) ]
79- [ ProducesResponseType ( typeof ( ProblemDetails ) , ( int ) HttpStatusCode . NotFound ) ]
80- public async Task < Page < CommentDto > > GetRepliesToComment (
72+ [ ProducesResponseType < Page < CommentResponse > > ( Status200OK ) ]
73+ [ ProducesResponseType < ValidationProblemDetails > ( Status400BadRequest ) ]
74+ [ ProducesResponseType < ProblemDetails > ( Status404NotFound ) ]
75+ [ ProducesResponseType < ProblemDetails > ( Status500InternalServerError ) ]
76+ public async Task < Page < CommentResponse > > GetReplies (
8177 [ FromRoute ] Guid parentCommentId ,
8278 [ FromQuery , BindRequired ] int offset ,
8379 [ FromQuery , BindRequired ] int count ) =>
84- await comments . GetRepliesToCommentAsync ( parentCommentId , User . GetUserId ( ) , offset , count ) ;
80+ await commentsService . GetRepliesToCommentAsync ( parentCommentId , User . GetUserId ( ) , offset , count ) ;
8581
8682 /// <summary>
8783 /// Creates a reply to the comment with the specified ID.
8884 /// </summary>
8985 [ HttpPost ( "{parentCommentId:guid}/replies" ) , Authorize ]
9086 [ Consumes ( MediaTypeNames . Application . Json ) , Produces ( MediaTypeNames . Application . Json ) ]
91- [ ProducesResponseType ( typeof ( CommentDto ) , ( int ) HttpStatusCode . Created ) ]
92- [ ProducesResponseType ( typeof ( ProblemDetails ) , ( int ) HttpStatusCode . InternalServerError ) ]
93- [ ProducesResponseType ( typeof ( ValidationProblemDetails ) , ( int ) HttpStatusCode . BadRequest ) ]
94- [ ProducesResponseType ( typeof ( ProblemDetails ) , ( int ) HttpStatusCode . Forbidden ) ]
95- [ ProducesResponseType ( typeof ( ProblemDetails ) , ( int ) HttpStatusCode . NotFound ) ]
96- public async Task < ActionResult < CommentDto > > ReplyToComment ( [ FromRoute ] Guid parentCommentId , [ FromBody ] ReplyRequest request )
87+ [ ProducesResponseType < CommentResponse > ( Status201Created ) ]
88+ [ ProducesResponseType < ValidationProblemDetails > ( Status400BadRequest ) ]
89+ [ ProducesResponseType < ProblemDetails > ( Status403Forbidden ) ]
90+ [ ProducesResponseType < ProblemDetails > ( Status404NotFound ) ]
91+ [ ProducesResponseType < ProblemDetails > ( Status500InternalServerError ) ]
92+ public async Task < ActionResult < CommentResponse > > Reply ( [ FromRoute ] Guid parentCommentId , [ FromBody ] ReplyRequest request )
9793 {
98- var response = await comments . ReplyToCommentAsync ( User . GetRequiredUserId ( ) , parentCommentId , request . Content ) ;
99- return CreatedAtAction ( nameof ( GetCommentById ) , new { commentId = response . Id } , response ) ;
94+ var response = await commentsService . ReplyToCommentAsync ( User . GetRequiredUserId ( ) , parentCommentId , request . Content ) ;
95+ return CreatedAtAction ( nameof ( GetById ) , new { commentId = response . Id } , response ) ;
10096 }
10197
10298 /// <summary>
103- /// Add a reaction to a comment
104- /// </summary>
105- [ HttpPost ( "{commentId:guid}/reactions" ) , Authorize ]
106- [ Consumes ( MediaTypeNames . Application . Json ) , Produces ( MediaTypeNames . Application . Json ) ]
107- [ ProducesResponseType ( typeof ( CommentDto ) , ( int ) HttpStatusCode . OK ) ]
108- [ ProducesResponseType ( typeof ( ProblemDetails ) , ( int ) HttpStatusCode . InternalServerError ) ]
109- [ ProducesResponseType ( typeof ( ValidationProblemDetails ) , ( int ) HttpStatusCode . BadRequest ) ]
110- [ ProducesResponseType ( typeof ( ProblemDetails ) , ( int ) HttpStatusCode . Forbidden ) ]
111- [ ProducesResponseType ( typeof ( ProblemDetails ) , ( int ) HttpStatusCode . NotFound ) ]
112- public async Task < CommentDto > AddReaction ( [ FromRoute ] Guid commentId , [ FromBody ] string reaction ) =>
113- await comments . AddReactionAsync ( User . GetRequiredUserId ( ) , commentId , reaction ) ;
114-
115- /// <summary>
116- /// Remove a reaction from a comment
99+ /// Sets reactions to a comment.
117100 /// </summary>
118- [ HttpDelete ( "{commentId :guid}/reactions" ) , Authorize ]
101+ [ HttpPost ( "{discussionId :guid}/reactions" ) , Authorize ]
119102 [ Consumes ( MediaTypeNames . Application . Json ) , Produces ( MediaTypeNames . Application . Json ) ]
120- [ ProducesResponseType ( typeof ( CommentDto ) , ( int ) HttpStatusCode . OK ) ]
121- [ ProducesResponseType ( typeof ( ProblemDetails ) , ( int ) HttpStatusCode . InternalServerError ) ]
122- [ ProducesResponseType ( typeof ( ValidationProblemDetails ) , ( int ) HttpStatusCode . BadRequest ) ]
123- [ ProducesResponseType ( typeof ( ProblemDetails ) , ( int ) HttpStatusCode . Forbidden ) ]
124- [ ProducesResponseType ( typeof ( ProblemDetails ) , ( int ) HttpStatusCode . NotFound ) ]
125- public async Task < CommentDto > RemoveReaction ( [ FromRoute ] Guid commentId , [ FromBody ] string reaction ) =>
126- await comments . AddReactionAsync ( User . GetRequiredUserId ( ) , commentId , reaction ) ;
127- }
103+ [ ProducesResponseType ( Status204NoContent ) ]
104+ [ ProducesResponseType < ValidationProblemDetails > ( Status400BadRequest ) ]
105+ [ ProducesResponseType < ProblemDetails > ( Status403Forbidden ) ]
106+ [ ProducesResponseType < ProblemDetails > ( Status404NotFound ) ]
107+ [ ProducesResponseType < ProblemDetails > ( Status500InternalServerError ) ]
108+ public async Task React ( [ FromRoute ] Guid discussionId , [ FromBody ] ISet < string > reactions ) =>
109+ await reactionsService . SetAsync ( discussionId , User . GetRequiredUserId ( ) , reactions ) ;
110+ }
0 commit comments