Files
TechHelper/TechHelper.Server/Services/StudentSubmissionService.cs
SpecialX 439c8a2421
Some checks failed
TechAct / explore-gitea-actions (push) Failing after 30s
feat: 添加学生提交系统功能
- 添加学生提交管理服务 (StudentSubmissionService, StudentSubmissionDetailService)
- 新增学生提交相关控制器 (StudentSubmissionController, StudentSubmissionDetailController)
- 添加学生提交数据传输对象 (StudentSubmissionDetailDto, StudentSubmissionSummaryDto)
- 新增学生提交相关页面组件 (StudentExamView, ExamDetailView, StudentCard等)
- 添加学生提交信息卡片组件 (SubmissionInfoCard, TeacherSubmissionInfoCard)
- 更新数据库迁移文件以支持提交系统
2025-09-09 15:42:31 +08:00

143 lines
5.7 KiB
C#
Raw Permalink 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.EntityFrameworkCore;
using SharedDATA.Api;
using TechHelper.Context;
using TechHelper.Repository;
using TechHelper.Server.Repositories;
using TechHelper.Services;
namespace TechHelper.Server.Services
{
public class StudentSubmissionService : IStudentSubmissionService
{
private readonly IUnitOfWork _unitOfWork;
private readonly IMapper _mapper;
private readonly IRepository<Submission> _submissionRepository;
private readonly IRepository<Assignment> _assignmentRepository;
private readonly IRepository<User> _userRepository;
public StudentSubmissionService(
IUnitOfWork unitOfWork,
IMapper mapper,
IRepository<Submission> submissionRepository,
IRepository<Assignment> assignmentRepository,
IRepository<User> userRepository)
{
_unitOfWork = unitOfWork;
_mapper = mapper;
_submissionRepository = submissionRepository;
_assignmentRepository = assignmentRepository;
_userRepository = userRepository;
}
public async Task<ApiResponse> GetStudentSubmissionsAsync(Guid studentId)
{
try
{
var submissions = await _submissionRepository
.GetAll(s => s.StudentId == studentId)
.Include(s => s.Assignment)
.ThenInclude(a => a.Creator)
.OrderByDescending(s => s.SubmissionTime)
.ToListAsync();
var result = new List<StudentSubmissionSummaryDto>();
foreach (var submission in submissions)
{
var summary = new StudentSubmissionSummaryDto
{
Id = submission.Id,
AssignmentName = submission.Assignment?.Title ?? "未知作业",
ErrorCount = await CalculateErrorCountAsync(submission.Id),
CreatedDate = submission.SubmissionTime,
Score = (int)submission.OverallGrade,
TotalQuestions = submission.Assignment?.TotalQuestions ?? 0,
StudentName = submission.Assignment?.Creator?.UserName ?? "未知老师",
Status = submission.Status.ToString()
};
result.Add(summary);
}
return ApiResponse.Success(result: result);
}
catch (Exception ex)
{
return ApiResponse.Error($"获取学生提交信息失败: {ex.Message}");
}
}
public async Task<ApiResponse> GetStudentSubmissionsPagedAsync(Guid studentId, int pageNumber = 1, int pageSize = 10)
{
try
{
var totalCount = await _submissionRepository
.GetAll(s => s.StudentId == studentId)
.CountAsync();
var submissions = await _submissionRepository
.GetAll(s => s.StudentId == studentId)
.Include(s => s.Assignment)
.ThenInclude(a => a.Creator)
.OrderByDescending(s => s.SubmissionTime)
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToListAsync();
var result = new List<StudentSubmissionSummaryDto>();
foreach (var submission in submissions)
{
var summary = new StudentSubmissionSummaryDto
{
Id = submission.Id,
AssignmentName = submission.Assignment?.Title ?? "未知作业",
ErrorCount = await CalculateErrorCountAsync(submission.Id),
CreatedDate = submission.SubmissionTime,
Score = submission.OverallGrade,
TotalQuestions = submission.Assignment?.TotalQuestions ?? 0,
StudentName = submission.Assignment?.Creator?.UserName ?? "未知老师",
Status = submission.Status.ToString()
};
result.Add(summary);
}
var response = new StudentSubmissionSummaryResponseDto
{
Submissions = result,
TotalCount = totalCount
};
return ApiResponse.Success(result: response);
}
catch (Exception ex)
{
return ApiResponse.Error($"获取学生提交信息失败: {ex.Message}");
}
}
private async Task<int> CalculateErrorCountAsync(Guid submissionId)
{
var submissionDetails = await _unitOfWork.GetRepository<SubmissionDetail>()
.GetAll(sd => sd.SubmissionId == submissionId)
.ToListAsync();
return submissionDetails.Select(x => !x.IsCorrect).Count();
}
// 以下方法是IBaseService接口的实现可以根据需要实现
public Task<ApiResponse> GetAllAsync() => throw new NotImplementedException();
public Task<ApiResponse> GetAsync(Guid id) => throw new NotImplementedException();
public Task<ApiResponse> AddAsync(StudentSubmissionSummaryDto model) => throw new NotImplementedException();
public Task<ApiResponse> UpdateAsync(StudentSubmissionSummaryDto model) => throw new NotImplementedException();
public Task<ApiResponse> DeleteAsync(Guid id) => throw new NotImplementedException();
public Task<ApiResponse> GetAllAsync(QueryParameter query)
{
throw new NotImplementedException();
}
}
}