Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BLL/Interfaces/Discussion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface IDiscussionService
Task<GetDiscussionDTO> AddNewDiscussionAsync(CreateDiscussionDTO Discussion);
Task DeleteAsync(int id);
Task<GetDiscussionDTO> GetByIdAsync(int id);
Task<List<GetCommentDTO>> GetListCommentByIdDiscussionAsync(int id);
Task<List<GetDiscussionDTO>> GetAllDiscussionsAsync();
Task<GetDiscussionDTO> UpdateAsync(CreateDiscussionDTO Discussion);
}
Expand Down
6 changes: 6 additions & 0 deletions BLL/Services/Discussion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ public async Task<GetDiscussionDTO> GetByIdAsync(int id)
}
}

public async Task<List<GetCommentDTO>> GetListCommentByIdDiscussionAsync(int id)
{
var answer = await DiscussionRepository.GetListCommentByIdDiscussionAsync(id);
return mapper.Map<List<GetCommentDTO>>(answer);
}

public async Task<GetDiscussionDTO> UpdateAsync(CreateDiscussionDTO e)
{
try
Expand Down
1 change: 1 addition & 0 deletions DAL/Interfaces/Discussion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace DAL.Interfaces
public interface IDiscussionRepository
{
Task<List<Discussion>> GetAllAsync();
Task<List<Comment>> GetListCommentByIdDiscussionAsync(int id);
Task<Discussion> GetByIdAsync(int id);
Task<Discussion> UpdateAsync(Discussion entity);
Task<Discussion> AddAsync(Discussion entity);
Expand Down
27 changes: 27 additions & 0 deletions DAL/Repositories/Discussion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,33 @@ public async Task<Discussion> GetByIdAsync(int Id)
throw;
}
}
public async Task<List<Comment>> GetListCommentByIdDiscussionAsync(int Id)
{
try
{
var discussion = await GetByIdAsync(Id);
if (discussion == null)
{
throw new ArgumentNullException(nameof(discussion), "The Id Discussion is null");
}
else
{
List<Comment> comments = new List<Comment>();
foreach (var item in discussion.Comments)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a lambda expression:
return discussion.Comments.Where(item => item.DiscussionId == Id).ToList();

{

if (item.DiscussionId == Id)
comments.Add(item);
}
return discussion.Comments.Where(item => item.DiscussionId == Id).ToList();
}
}
catch (Exception ex)
{
logger.LogError("failed to get Comment To Discussion" + ex.Message.ToString());
throw;
}
}

public async Task<Discussion> UpdateAsync(Discussion entity)
{
Expand Down
23 changes: 23 additions & 0 deletions WebApi/Controllers/DiscussionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ public async Task<IActionResult> GetById(int id)
return StatusCode(500, "Internal Server Error"); // HTTP 500
}
}
[HttpGet("GetListComment/{id}")]
public async Task<IActionResult> GetListCommentById(int id)
{
try
{
var comment = await DiscussionService.GetListCommentByIdDiscussionAsync(id);
if (comment == null)
{
return NotFound($"Discussion with ID {id} not found"); // HTTP 404
}
return Ok(comment); // HTTP 200 OK
}
catch (ArgumentException ex)
{
logger.LogError($"Invalid argument: {ex.Message}");
return BadRequest(ex.Message); // HTTP 400
}
catch (Exception ex)
{
logger.LogError($"Failed to get discussion with ID {id}: {ex.Message}");
return StatusCode(500, "Internal Server Error"); // HTTP 500
}
}

[HttpPost]
public async Task<IActionResult> Add([FromBody] CreateDiscussionDTO newDiscussion)
Expand Down