Skip to content

Commit

Permalink
update ExamHistory and create MyMultipleChoiCe
Browse files Browse the repository at this point in the history
  • Loading branch information
vuanh25 committed Dec 26, 2023
1 parent 20b8374 commit 1373e93
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 56 deletions.
2 changes: 1 addition & 1 deletion VNH.Application/Common/Contants/SystemConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public static class SystemConstants
//public const string ConnectString = "Data Source=SQL5112.site4now.net;Initial Catalog=db_aa121e_vuanhpham25;User Id=db_aa121e_vuanhpham25_admin;Password=30102002Mai";

public const string UrlWeb = "https://localhost:7138/";
public const string ConnectString = "Data Source=.;Initial Catalog=VietNamHistory;Integrated Security=True;Encrypt=true;TrustServerCertificate=true;";
public const string ConnectString = "Data Source=.;Initial Catalog=tyls;Integrated Security=True;Encrypt=true;TrustServerCertificate=true;";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public interface IMultipleChoiceService
Task<ApiResult<string>> Delete(string id, string userId);

Task<ApiResult<string>> DeleteQuizById(string id);
Task<ApiResult<List<MultipleChoiceResponseDto>>> GetMyMultipleChoice(string id);

Task<ApiResult<List<MultipleChoiceResponseDto>>> Search(string keyWord);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ExamHistoryService(UserManager<User> userManager, VietNamHistoryContext d
public async Task<ApiResult<ExamHistoryResponseDto>> Create(CreateExamHistoryDto requestDto,string name)
{
var user = await _userManager.FindByEmailAsync(name);
var check = await _dataContext.ExamHistories.Where(x => x.UserId.Equals(user.Id)).FirstOrDefaultAsync();
var check = await _dataContext.ExamHistories.Where(x => x.UserId.Equals(user.Id) && x.MultipleChoiceId == requestDto.MultipleChoiceId ).FirstOrDefaultAsync();
if (check != null)
{
return await Update(requestDto, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public MultipleChoiceService(UserManager<User> userManager, VietNamHistoryContex
_mapper = mapper;
}

public async Task<ApiResult<string>> Create(CreateQuizDto requestDto,string name)
public async Task<ApiResult<string>> Create(CreateQuizDto requestDto, string name)
{
var user = await _userManager.FindByEmailAsync(name);
var multipleChoice = new MultipleChoice();
Expand All @@ -51,8 +51,8 @@ public async Task<ApiResult<string>> Create(CreateQuizDto requestDto,string name
{
_dataContext.MultipleChoices.Add(multipleChoice);
await _dataContext.SaveChangesAsync();
foreach(var quiestion in quiz)
{
foreach (var quiestion in quiz)
{
var quizz = new Quiz()
{
Id = Guid.NewGuid(),
Expand All @@ -70,19 +70,19 @@ public async Task<ApiResult<string>> Create(CreateQuizDto requestDto,string name
QuizId = quizz.Id,
isCorrect = quizanswer.isCorrect
};

_dataContext.QuizAnswers.Add(answer);
}
}
await _dataContext.SaveChangesAsync();
return new ApiSuccessResult<string>("Tạo thành công");
}
catch(Exception ex)
catch (Exception ex)
{
return new ApiErrorResult<string>("Lỗi lưu : " + ex.Message);
}



}

Expand Down Expand Up @@ -268,75 +268,75 @@ public async Task<ApiResult<MultipleChoiceResponseDto>> Detail(string id)
}
var user = await _userManager.FindByIdAsync(multipleChoice.UserId.ToString());
var response = _mapper.Map<MultipleChoiceResponseDto>(multipleChoice);
var quizs = await _dataContext.Quizzes.Where(x=>x.MultipleChoiceId.Equals(Guid.Parse(id))).ToListAsync();
var quizs = await _dataContext.Quizzes.Where(x => x.MultipleChoiceId.Equals(Guid.Parse(id))).ToListAsync();
response.WorkTime = multipleChoice.WorkTime;

foreach (var quiz in quizs)
{

var quizanswers = await _dataContext.QuizAnswers.Where(x => x.QuizId.Equals(quiz.Id)).ToListAsync();

response.Quizs.Add(new()
{
Id = quiz.Id,
Content = quiz.Content,
QuizAnswers = _mapper.Map<List<QuizAnswerDto>>(quizanswers),
});
{
Id = quiz.Id,
Content = quiz.Content,
QuizAnswers = _mapper.Map<List<QuizAnswerDto>>(quizanswers),

});




}
foreach(var item in response.Quizs)
foreach (var item in response.Quizs)
{
var quizanswers = await _dataContext.QuizAnswers.Where(x => x.QuizId.Equals(item.Id)).ToListAsync();

}

response.UserShort = new()
{
FullName = user.Fullname,
Id = user.Id,
Image = user.Image,
};
return new ApiSuccessResult<MultipleChoiceResponseDto>(response);
}
}


public async Task<ApiResult<List<MultipleChoiceResponseDto>>> GetAll()
{
var list = await _dataContext.MultipleChoices.OrderByDescending(x=> x.CreatedAt).ToListAsync();
var list = await _dataContext.MultipleChoices.OrderByDescending(x => x.CreatedAt).ToListAsync();
var users = await _dataContext.User.ToListAsync();
var quizlist = await _dataContext.Quizzes.ToListAsync();
var quizanswerList = await _dataContext.QuizAnswers.ToListAsync();

var result = new List<MultipleChoiceResponseDto>();

foreach (var item in list)
{
var multi = _mapper.Map<MultipleChoiceResponseDto>(item);
multi.NumberQuiz = quizlist.Where(x=>x.MultipleChoiceId.Equals(item.Id)).Count();
var userShort = users.First(x=> x.Id == item.UserId);
if(userShort is not null)
multi.NumberQuiz = quizlist.Where(x => x.MultipleChoiceId.Equals(item.Id)).Count();
var userShort = users.First(x => x.Id == item.UserId);
if (userShort is not null)
{
multi.UserShort.FullName = userShort.Fullname;
multi.UserShort.Id = userShort.Id;
multi.UserShort.Image = userShort.Image;
}

result.Add(multi);

}
return new ApiSuccessResult<List<MultipleChoiceResponseDto>>(result);
}

public async Task<ApiResult<string>> Update(CreateQuizDto requestDto,string name)
public async Task<ApiResult<string>> Update(CreateQuizDto requestDto, string name)
{
var user = await _userManager.FindByEmailAsync(name);

var updateMultipleChoice = _dataContext.MultipleChoices.First(x => x.Id.Equals(Guid.Parse(requestDto.Id)));
if(updateMultipleChoice is null)
if (updateMultipleChoice is null)
{
return new ApiErrorResult<string>("Lỗi :Không được cập nhập (không tìm thấy bài thi nào)");
}
Expand All @@ -346,11 +346,11 @@ public async Task<ApiResult<string>> Update(CreateQuizDto requestDto,string name
updateMultipleChoice.UpdatedAt = DateTime.Now;
try
{
_dataContext.MultipleChoices.Update(updateMultipleChoice);
_dataContext.MultipleChoices.Update(updateMultipleChoice);
await _dataContext.SaveChangesAsync();
return new ApiSuccessResult<string>("Cập nhập bài thi thành công");
}
catch(Exception ex)
catch (Exception ex)
{
return new ApiErrorResult<string>("Lỗi lưu bài thi : " + ex.Message);

Expand All @@ -360,16 +360,16 @@ public async Task<ApiResult<string>> Update(CreateQuizDto requestDto,string name

public async Task<ApiResult<QuizDto>> UpdateQuizById(QuizDto requestDto)
{
var updateQuiz = _dataContext.Quizzes.First(x => x.Id.Equals(requestDto.Id));
if(updateQuiz is null)
var updateQuiz = _dataContext.Quizzes.First(x => x.Id.Equals(requestDto.Id));
if (updateQuiz is null)
{
return new ApiErrorResult<QuizDto>("Lỗi :Không được cập nhập (không tìm thấy câu hỏi nào)");
}
updateQuiz.Content = requestDto.Content;
foreach(var item in requestDto.QuizAnswers)
foreach (var item in requestDto.QuizAnswers)
{
var updateQuizAnswer = _dataContext.QuizAnswers.First(x=>x.Id.Equals(item.Id));
if(updateQuizAnswer != null)
var updateQuizAnswer = _dataContext.QuizAnswers.First(x => x.Id.Equals(item.Id));
if (updateQuizAnswer != null)
{
updateQuizAnswer.Content = item.Content;
updateQuizAnswer.isCorrect = item.isCorrect;
Expand All @@ -385,7 +385,7 @@ public async Task<ApiResult<QuizDto>> UpdateQuizById(QuizDto requestDto)
return new ApiSuccessResult<QuizDto>(response);

}
catch(Exception ex) {
catch (Exception ex) {
return new ApiErrorResult<QuizDto>("Lỗi lưu câu hỏi : " + ex.Message);

}
Expand All @@ -395,22 +395,22 @@ public async Task<ApiResult<QuizDto>> UpdateQuizById(QuizDto requestDto)
public async Task<ApiResult<string>> Delete(string id, string userId)
{
var multiple = await _dataContext.MultipleChoices.FirstOrDefaultAsync(x => x.Id.Equals(Guid.Parse(id)) && x.UserId.Equals(Guid.Parse(userId)));
if(multiple is null)
if (multiple is null)
{
return new ApiErrorResult<string>("Không tìm thấy bài thi");
}

var quizs = await _dataContext.Quizzes.ToListAsync();
var quizsAnswer = await _dataContext.QuizAnswers.ToListAsync();
foreach(var item in quizs)
foreach (var item in quizs)
{
if(item.MultipleChoiceId.Equals(id))
if (item.MultipleChoiceId.Equals(id))
{
foreach(var item1 in quizsAnswer)
foreach (var item1 in quizsAnswer)
{
if(item1.QuizId.Equals(item.Id))
if (item1.QuizId.Equals(item.Id))
{
_dataContext.QuizAnswers.Remove(item1);
_dataContext.QuizAnswers.Remove(item1);
await _dataContext.SaveChangesAsync();
}
}
Expand All @@ -422,30 +422,47 @@ public async Task<ApiResult<string>> Delete(string id, string userId)
_dataContext.MultipleChoices.Remove(multiple);
await _dataContext.SaveChangesAsync();
return new ApiSuccessResult<string>("Xoá bài thi thành công");
}
}

public async Task<ApiResult<string>> DeleteQuizById(string id)
{
var quiz = await _dataContext.Quizzes.FirstOrDefaultAsync(x=>x.Id.Equals(Guid.Parse(id)));
if(quiz == null)
var quiz = await _dataContext.Quizzes.FirstOrDefaultAsync(x => x.Id.Equals(Guid.Parse(id)));
if (quiz == null)
{
return new ApiErrorResult<string>("Không tìm thấy câu hỏi");
}
var quizanswers = await _dataContext.QuizAnswers.ToListAsync();
foreach(var item in quizanswers)
foreach (var item in quizanswers)
{
if(item.QuizId.Equals(id))
if (item.QuizId.Equals(id))
{
_dataContext.QuizAnswers.Remove(item);
await _dataContext.SaveChangesAsync();
}
}
_dataContext.Quizzes.Remove(quiz);
await _dataContext.SaveChangesAsync();
await _dataContext.SaveChangesAsync();
return new ApiSuccessResult<string>("Xoá câu hỏi thành công");

}

public async Task<ApiResult<List<MultipleChoiceResponseDto>>> GetMyMultipleChoice(string id)
{
var list = await _dataContext.MultipleChoices.Where(x => x.UserId.Equals(Guid.Parse(id)) ).ToListAsync();

var result = new List<MultipleChoiceResponseDto>();
var user = await _dataContext.User.Where(x => !x.IsDeleted && x.Id.ToString().Equals(id)).FirstOrDefaultAsync();
foreach( var item in list)
{
var multi = _mapper.Map<MultipleChoiceResponseDto>(item);

result.Add(multi);
}
return new ApiSuccessResult<List<MultipleChoiceResponseDto>>(result);

}



public async Task<ApiResult<List<MultipleChoiceResponseDto>>> Search(string keyWord)
{
Expand Down
10 changes: 10 additions & 0 deletions VNH.WebAPi/Controllers/MultiChoiceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ public async Task<IActionResult> Search([FromQuery] string keyWord)
}


[HttpGet("MyMultipleChoice")]
[Authorize(Roles ="admin")]
public async Task<IActionResult> GetMyPost()
{
var id = User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "";
var result = await _multipleChoiceService.GetMyMultipleChoice(id);
return result is null ? BadRequest(result) : Ok(result);
}



}

Expand Down
4 changes: 2 additions & 2 deletions VNH.WebAPi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"Issuer": "https://tyls.fun"
},
"ConnectionStrings": {
"LocalDataConnect": "Data Source=.;Initial Catalog=VietNamHistory;Integrated Security=True;Encrypt=true;TrustServerCertificate=true;",
"LocalDataConnect": "Data Source=.;Initial Catalog=tyls;Integrated Security=True;Encrypt=true;TrustServerCertificate=true;",
//"DataConnect": "Data Source=SQL5112.site4now.net;Initial Catalog=db_aa121e_vuanhpham25;User Id=db_aa121e_vuanhpham25_admin;Password=30102002Mai"
"DataConnect": "Data Source=.\\MSSQLSERVER2022;Initial Catalog=tyls;User Id=toiyeulichsu;Password=MheZDu9$oGbrYlL#S%ApJ^qf;TrustServerCertificate=True;Encrypt=False"
// "DataConnect": "Data Source=.\\MSSQLSERVER2022;Initial Catalog=tyls;User Id=toiyeulichsu;Password=MheZDu9$oGbrYlL#S%ApJ^qf;TrustServerCertificate=True;Encrypt=False"
},

"MailSettings": {
Expand Down

0 comments on commit 1373e93

Please sign in to comment.