-
Notifications
You must be signed in to change notification settings - Fork 2
Added CreateThread endpoint #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
050c57c
d01a36a
e71d3b3
cb8a0ea
155fd0c
bb19796
7673376
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,13 +13,11 @@ namespace AskFm.API.Controllers; | |
| [Authorize(AuthenticationSchemes = "Bearer")] | ||
| public class CommentController : ControllerBase | ||
| { | ||
|
|
||
| private readonly ICommentLikeService _commentLikeService; | ||
| private readonly ICommentService _commentService; | ||
| private readonly ILogger<CommentController> _logger; | ||
| private readonly IUserService _userService; | ||
|
|
||
|
|
||
|
|
||
| public CommentController( | ||
| ICommentLikeService commentLikeService, | ||
| ICommentService commentService, | ||
|
|
@@ -31,11 +29,7 @@ public CommentController( | |
| _commentService = commentService; | ||
| _userService = userService; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| // GET api/comment/{id}/likes -> get all the likes for a Comment with id = id | ||
| [HttpGet("{id}/likes")] | ||
| public async Task<IActionResult> GetAllLikes(int id) | ||
|
|
@@ -55,7 +49,6 @@ public async Task<IActionResult> GetAllLikes(int id) | |
| return NotFound(new | ||
| { | ||
| message = ex.Message, | ||
|
|
||
| }); | ||
| } | ||
| catch (Exception ex) | ||
|
|
@@ -65,31 +58,28 @@ public async Task<IActionResult> GetAllLikes(int id) | |
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| // POST api/comment/{id}/likes -> add a like for a Comment with id = id | ||
| [HttpPost("{id}/likes")] | ||
| public async Task<IActionResult> AddLike(int id) | ||
| { | ||
| try | ||
| { | ||
| var user = await _userService.GetCurrentUserAsync(); | ||
|
|
||
| if (!user.success) | ||
| { | ||
| return BadRequest(user.Errors); | ||
| } | ||
|
|
||
| var createdLike = await _commentLikeService.AddLikeAsync(id, user.Data.Id); | ||
|
|
||
| if (!createdLike.success) | ||
| { | ||
| return BadRequest(createdLike.Errors); | ||
| } | ||
| return CreatedAtAction( | ||
| nameof(GetAllLikes), | ||
| new { id = id }, | ||
| nameof(GetAllLikes), | ||
| new { id = id }, | ||
| createdLike.Data); | ||
| } | ||
| catch (ArgumentException ex) | ||
|
|
@@ -108,37 +98,31 @@ public async Task<IActionResult> AddLike(int id) | |
| return StatusCode(500, new { message = "An error occurred while adding like" }); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| [HttpDelete("{id}/likes")] | ||
| public async Task<IActionResult> DeleteLike(int id) | ||
| { | ||
| int userId = 0; | ||
| try | ||
| { | ||
|
|
||
| var user = await _userService.GetCurrentUserAsync(); | ||
|
|
||
| if (user==null || !user.success) | ||
| if (user == null || !user.success) | ||
| return BadRequest(user.Errors); | ||
|
|
||
|
|
||
|
|
||
| userId = user.Data.Id; | ||
| var comment = await _commentService.GetCommentAsync(id); | ||
|
|
||
| if (comment == null || !user.success) | ||
| return BadRequest(user.Errors); | ||
|
|
||
|
|
||
|
|
||
| var result = await _commentLikeService.DeleteLikeAsync(id, userId); | ||
|
|
||
| if (!result.success) | ||
| { | ||
| return BadRequest(result.Errors); | ||
| } | ||
|
|
||
| return NoContent(); | ||
| } | ||
| catch (ArgumentException ex) | ||
|
|
@@ -157,5 +141,58 @@ public async Task<IActionResult> DeleteLike(int id) | |
| return StatusCode(500, new { message = "An error occurred while deleting like" }); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // POST api/threads/{id}/comments - Add a comment to the thread with id = {id} | ||
| [HttpPost] | ||
| [Route("threads/{id}/comments")] | ||
| public async Task<IActionResult> AddComment([FromRoute] int id, [FromBody] CreateCommentDto createCommentDto) | ||
| { | ||
| var user = await _userService.GetCurrentUserAsync(); | ||
| if (!user.success) | ||
| { | ||
| return BadRequest(user.Errors); | ||
| } | ||
|
|
||
| var result = await _commentService.AddComment(id, user.Data.Id, createCommentDto); | ||
| if (!result.success) | ||
| { | ||
| return BadRequest(result.Errors); | ||
| } | ||
|
|
||
| return Ok(result.Data); | ||
| } | ||
|
|
||
| // GET api/threads/{id}/comments - Get all comments for the thread with id = {id} | ||
| [HttpGet] | ||
| [Route("threads/{id}/comments")] | ||
| public async Task<IActionResult> GetComments([FromRoute] int id, [FromQuery] int page = 1, [FromQuery] int pageSize = 10) | ||
| { | ||
| var result = await _commentService.GetCommentsByThreadId(id, page, pageSize); | ||
| if (!result.success) | ||
| { | ||
| return BadRequest(result.Errors); | ||
| } | ||
|
|
||
| return Ok(result.Data); | ||
| } | ||
|
|
||
| // DELETE api/threads/{threadId}/comments/{commentId} - Delete a comment with id = {commentId} | ||
| [HttpDelete] | ||
| [Route("threads/{threadId}/comments/{commentId}")] | ||
| public async Task<IActionResult> DeleteComment([FromRoute] int threadId, [FromRoute] int commentId) | ||
| { | ||
| var user = await _userService.GetCurrentUserAsync(); | ||
| if (!user.success) | ||
| { | ||
| return BadRequest(user.Errors); | ||
| } | ||
|
|
||
| var result = await _commentService.DeleteComment(threadId, commentId, user.Data.Id); | ||
| if (!result.success) | ||
| { | ||
| return BadRequest(result.Errors); | ||
| } | ||
|
|
||
| return Ok(new { message = "Comment deleted successfully" }); | ||
| } | ||
|
Comment on lines
+145
to
+197
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expose comment endpoints under /api/threads as documented With - [HttpPost]
- [Route("threads/{id}/comments")]
+ [HttpPost("/api/threads/{id}/comments")]
public async Task<IActionResult> AddComment([FromRoute] int id, [FromBody] CreateCommentDto createCommentDto)
...
- [HttpGet]
- [Route("threads/{id}/comments")]
+ [HttpGet("/api/threads/{id}/comments")]
public async Task<IActionResult> GetComments([FromRoute] int id, [FromQuery] int page = 1, [FromQuery] int pageSize = 10)
...
- [HttpDelete]
- [Route("threads/{threadId}/comments/{commentId}")]
+ [HttpDelete("/api/threads/{threadId}/comments/{commentId}")]
public async Task<IActionResult> DeleteComment([FromRoute] int threadId, [FromRoute] int commentId)Alternatively, move these actions to a controller whose base route is 🤖 Prompt for AI Agents |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix comment existence check in DeleteLike
This block reuses the
userresult when validating the comment, so a missing comment slips through anduser.Errorsis returned even when the user lookup succeeded. It also risks a null dereference ifuseris null. Validatecomment.successand surface its errors instead.🤖 Prompt for AI Agents