Files
TechHelper/TechHelper.Server/Services/ExamService.cs
2025-06-20 15:37:39 +08:00

161 lines
4.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AutoMapper;
using Entities.Contracts;
using Entities.DTO;
using Microsoft.VisualBasic;
using SharedDATA.Api;
using TechHelper.Context;
using TechHelper.Server.Repositories;
using TechHelper.Services;
namespace TechHelper.Server.Services
{
public class ExamService : IExamService
{
private readonly IUnitOfWork _unitOfWork;
private readonly IExamRepository _examRepository;
private readonly IMapper _mapper;
public ExamService(IUnitOfWork unitOfWork, IExamRepository examRepository, IMapper mapper)
{
_unitOfWork = unitOfWork;
_examRepository = examRepository;
_mapper = mapper;
}
public async Task<ApiResponse> CreateExamAsync(AssignmentDto assignmentDto)
{
Assignment newAssi = _mapper.Map<Assignment>(assignmentDto);
await _examRepository.AddAsync(newAssi);
var context = _unitOfWork.GetDbContext<ApplicationContext>();
foreach (var entry in context.ChangeTracker.Entries())
{
if (entry.State == Microsoft.EntityFrameworkCore.EntityState.Added)
{
if(entry.Entity is Question newQues)
{
newQues.CreatorId = newAssi.CreatorId;
}
}
}
await _unitOfWork.SaveChangesAsync();
return ApiResponse.Success();
}
private async void ParseStruct(AssignmentStructDto assignmentStruct, Guid ParentID)
{
var newStruct = _mapper.Map<AssignmentStruct>(assignmentStruct);
newStruct.ParentStructId = Guid.Empty == ParentID ? null : ParentID;
await _examRepository.AddAsync(newStruct);
foreach (var item in assignmentStruct.AssignmentQuestions)
{
var newQuestion = _mapper.Map<Question>(item);
//newQuestion.ParentQuestionId = item.ParentQuestion == null ? null : item.ParentQuestion.Id;
await _examRepository.AddAsync(newQuestion);
//await ParseAssignmentQuestion(assignmentStruct, item, newQuestion);
}
foreach (var item in assignmentStruct.ChildrenGroups)
{
ParseStruct(item, assignmentStruct.Id);
}
}
private async Task ParseAssignmentQuestion(AssignmentStructDto assignmentStruct, QuestionDto item, Question newQuestion)
{
AssignmentQuestion newAssignQues = new AssignmentQuestion();
newAssignQues.QuestionId = newQuestion.Id;
newAssignQues.AssignmentStructId = assignmentStruct.Id;
newAssignQues.CreatedAt = DateTime.UtcNow;
newAssignQues.Score = item.Score;
await _examRepository.AddAsync(newAssignQues);
}
private void SetEntityIdsAndRelations(AssignmentStruct group, Guid? assignmentId, Guid creatorId)
{
group.Id = Guid.NewGuid();
group.AssignmentId = assignmentId;
foreach (var aq in group.AssignmentQuestions)
{
aq.Id = Guid.NewGuid();
aq.AssignmentStructId = group.Id;
aq.Question.Id = Guid.NewGuid();
aq.Question.CreatorId = creatorId;
aq.CreatedAt = DateTime.UtcNow;
// ... 其他默认值
}
foreach (var childGroup in group.ChildrenGroups)
{
// 子题组的 AssignmentId 为 null通过 ParentGroup 关联
SetEntityIdsAndRelations(childGroup, null, creatorId);
childGroup.ParentStructId = group.Id;
}
}
public async Task<AssignmentDto> GetExamByIdAsync(Guid id)
{
var assignment = await _examRepository.GetFullExamByIdAsync(id);
if (assignment == null)
{
throw new InvalidOperationException("");
}
return _mapper.Map<AssignmentDto>(assignment);
}
public async Task<IEnumerable<AssignmentDto>> GetAllExamPreviewsAsync(Guid userId)
{
var assignments = await _examRepository.GetExamPreviewsByUserAsync(userId);
return _mapper.Map<IEnumerable<AssignmentDto>>(assignments);
}
public Task<ApiResponse> GetAllAsync(QueryParameter query)
{
throw new NotImplementedException();
}
public Task<ApiResponse> GetAsync(Guid id)
{
throw new NotImplementedException();
}
public Task<ApiResponse> AddAsync(AssignmentDto model)
{
throw new NotImplementedException();
}
public Task<ApiResponse> UpdateAsync(AssignmentDto model)
{
throw new NotImplementedException();
}
public Task<ApiResponse> DeleteAsync(Guid id)
{
throw new NotImplementedException();
}
Task<ApiResponse> IExamService.GetAllExamPreviewsAsync(Guid userId)
{
throw new NotImplementedException();
}
}
}