Skip to content

Commit

Permalink
#39 | Update SubAnswer Code
Browse files Browse the repository at this point in the history
  • Loading branch information
LuongXuanNhat committed Dec 3, 2023
1 parent 6582942 commit b77cb28
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
24 changes: 21 additions & 3 deletions VNH.Infrastructure/Implement/Catalog/Forum/AnswerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ public async Task<ApiResult<List<AnswerQuestionDto>>> GetAnswer(string questionI
}

var users = await _dataContext.User.ToListAsync();
var answerQuestion = await _dataContext.Answers.Where(x => x.QuestionId.Equals(question.Id)).ToListAsync();
var answerQuestion = await _dataContext.Answers.Where(x => x.QuestionId.Equals(question.Id)).OrderByDescending(x => x.CreatedAt).ToListAsync();
var subAnswerQuestion = await _dataContext.SubAnswers.ToListAsync();
var result = new List<AnswerQuestionDto>();
foreach(var item in answerQuestion)
{
var subAnswer = subAnswerQuestion.Where(x => x.PreAnswerId.Equals(item.Id)).ToList();
var subAnswer = subAnswerQuestion.Where(x => x.PreAnswerId.Equals(item.Id)).OrderByDescending(x => x.CreatedAt).ToList();
var answ = _mapper.Map<AnswerQuestionDto>(item);
answ.UserShort = GetUserShort(users, item.AuthorId);
if(subAnswer.Count > 0)
Expand Down Expand Up @@ -175,12 +175,26 @@ public async Task<ApiResult<string>> UpdateSubAnswer(SubAnswerQuestionDto subAns
_dataContext.SubAnswers.Update(subAns);
await _dataContext.SaveChangesAsync();

var answers = await GetAnswer(subAns.Id.ToString());
var answers = await GetSubAnswer(subAns.PreAnswerId.ToString());
await _answerHubContext.Clients.All.SendAsync("ReceiveAnswer", answers);

return new ApiSuccessResult<string>("Đã cập nhập bình luận");
}

private async Task<List<SubAnswerQuestionDto>> GetSubAnswer(string? answerId)
{
List<SubAnswerQuestionDto> result = new();
var users = await _dataContext.User.ToListAsync();
var listSubAnswer = await _dataContext.SubAnswers.Where(x=>x.PreAnswerId.ToString() == answerId).ToListAsync();
foreach (var item in listSubAnswer)
{
var answer = _mapper.Map<SubAnswerQuestionDto>(item);
answer.UserShort = GetUserShort(users, item.AuthorId);
result.Add(answer);
}
return result;
}

public async Task<ApiResult<string>> DeteleSubAnswer(string id)
{
var subAnswer = await _dataContext.SubAnswers.FirstOrDefaultAsync(x => x.Id.Equals(Guid.Parse(id)));
Expand All @@ -190,6 +204,10 @@ public async Task<ApiResult<string>> DeteleSubAnswer(string id)
}
_dataContext.SubAnswers.Remove(subAnswer);
await _dataContext.SaveChangesAsync();

var answers = await GetSubAnswer(subAnswer.PreAnswerId.ToString());
await _answerHubContext.Clients.All.SendAsync("ReceiveAnswer", answers);

return new ApiSuccessResult<string>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public async Task<ApiResult<QuestionResponseDto>> Detail(string Id)

public async Task<ApiResult<List<QuestionResponseDto>>> GetAll()
{
var questions = await _dataContext.Questions.ToListAsync();
var questions = await _dataContext.Questions.OrderByDescending(x => x.CreatedAt).ToListAsync();
var users = await _dataContext.User.ToListAsync();

var result = new List<QuestionResponseDto>();
Expand Down
4 changes: 2 additions & 2 deletions VNH.Infrastructure/Implement/Catalog/Posts/PostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public async Task<ApiResult<PostResponseDto>> Detail(string Id)

public async Task<ApiResult<List<PostResponseDto>>> GetAll()
{
var posts = await _dataContext.Posts.ToListAsync();
var posts = await _dataContext.Posts.OrderByDescending(x => x.CreatedAt).ToListAsync();
var users = await _dataContext.User.ToListAsync();
var topics = await _dataContext.Topics.ToListAsync();

Expand All @@ -206,7 +206,7 @@ public async Task<ApiResult<List<PostResponseDto>>> GetAll()
}
public async Task<ApiResult<List<PostResponseDto>>> GetAllMobile()
{
var posts = await _dataContext.Posts.ToListAsync();
var posts = await _dataContext.Posts.OrderByDescending(x => x.CreatedAt).ToListAsync();
var users = await _dataContext.User.ToListAsync();
var topics = await _dataContext.Topics.ToListAsync();

Expand Down
2 changes: 1 addition & 1 deletion VNH.WebAPi/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task<IActionResult> Login([FromBody] LoginRequest request)
var result = await _account.Authenticate(request);
if (!result.IsSuccessed)
{
return BadRequest(result);
return Ok(result);
}
var userPrincipal = _account.ValidateToken(result.ResultObj);
var authProperties = new AuthenticationProperties
Expand Down

0 comments on commit b77cb28

Please sign in to comment.