temp
This commit is contained in:
@@ -11,14 +11,12 @@ using TechHelper.Services;
|
||||
|
||||
namespace TechHelper.Server.Controllers
|
||||
{
|
||||
[Route("api/exam")]
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
|
||||
|
||||
public class ExamController : ControllerBase
|
||||
{
|
||||
private IExamService _examService;
|
||||
private readonly IExamService _examService;
|
||||
private readonly UserManager<User> _userManager;
|
||||
|
||||
public ExamController(IExamService examService, UserManager<User> userManager)
|
||||
@@ -27,117 +25,202 @@ namespace TechHelper.Server.Controllers
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个新的考试/作业。
|
||||
/// </summary>
|
||||
/// <param name="examDto">考试/作业的数据传输对象。</param>
|
||||
/// <returns>新创建的考试/作业信息或错误信息。</returns>
|
||||
[HttpPost("add")]
|
||||
public async Task<IActionResult> AddExam(
|
||||
[FromBody] AssignmentDto examDto)
|
||||
public async Task<IActionResult> AddExam([FromBody] AssignmentDto examDto)
|
||||
{
|
||||
var user = await _userManager.FindByEmailAsync(User.Identity?.Name ?? "");
|
||||
if (user == null) return BadRequest("无效的用户");
|
||||
|
||||
var user = await _userManager.FindByEmailAsync(User.Identity.Name);
|
||||
if (user == null) return NotFound("没有找到用户");
|
||||
examDto.CreatorId = user.Id;
|
||||
var result = await _examService.CreateExamAsync(examDto);
|
||||
|
||||
if (result.Status)
|
||||
{
|
||||
return Ok(result);
|
||||
return StatusCode(201, result.Result);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest();
|
||||
return BadRequest(result.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 提交作业。
|
||||
/// </summary>
|
||||
/// <param name="submissionDto">提交的数据传输对象。</param>
|
||||
/// <returns>提交结果或错误信息。</returns>
|
||||
[HttpPost("submission")]
|
||||
public async Task<IActionResult> SubmissionAssignment(
|
||||
[FromBody] SubmissionDto submissionDto)
|
||||
[Authorize(Roles = "Student")]
|
||||
public async Task<IActionResult> SubmissionAssignment([FromBody] SubmissionDto submissionDto)
|
||||
{
|
||||
if (User == null) return BadRequest("无效的用户");
|
||||
if (User.IsInRole("Teacher"))
|
||||
var result = await _examService.SubmissionAssignment(submissionDto);
|
||||
|
||||
if (result.Status)
|
||||
{
|
||||
var result = await _examService.SubmissionAssignment(submissionDto);
|
||||
if (result.Status)
|
||||
{
|
||||
return Ok(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest(result.Message);
|
||||
}
|
||||
return Ok(result.Result);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest("你没有权限修改");
|
||||
return BadRequest(result.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("get")]
|
||||
/// <summary>
|
||||
/// 根据ID获取考试/作业详情。
|
||||
/// </summary>
|
||||
/// <param name="id">考试/作业ID。</param>
|
||||
/// <returns>考试/作业详情或未找到错误。</returns>
|
||||
[HttpGet("{id:guid}")]
|
||||
public async Task<IActionResult> GetExamById(Guid id)
|
||||
{
|
||||
|
||||
var result = await _examService.GetAsync(id);
|
||||
|
||||
if (result.Status)
|
||||
{
|
||||
if (result.Result == null)
|
||||
{
|
||||
return NotFound("未找到指定的考试/作业。");
|
||||
}
|
||||
return Ok(result.Result);
|
||||
}
|
||||
else
|
||||
return BadRequest("查找失败");
|
||||
{
|
||||
if (result.Message.Contains("未找到") || result.Message.Contains("not found", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return NotFound(result.Message);
|
||||
}
|
||||
return BadRequest(result.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有考试/作业的预览信息(教师获取自己创建的,学生获取自己需要提交的)。
|
||||
/// </summary>
|
||||
/// <returns>考试/作业预览列表或错误信息。</returns>
|
||||
[HttpGet("getAllPreview")]
|
||||
public async Task<IActionResult> GetAllExamPreview()
|
||||
{
|
||||
if (User == null) return BadRequest("用户验证失败, 无效用户");
|
||||
var user = await _userManager.FindByEmailAsync(User.Identity.Name);
|
||||
if (user == null) return NotFound("没有找到用户");
|
||||
|
||||
var userid = await _userManager.FindByEmailAsync(User.Identity.Name);
|
||||
ApiResponse result;
|
||||
|
||||
|
||||
var result = new ApiResponse();
|
||||
if (User.IsInRole("Teacher"))
|
||||
{
|
||||
result = await _examService.GetAllExamPreviewsAsync(userid.Id);
|
||||
result = await _examService.GetAllExamPreviewsAsync(user.Id);
|
||||
}
|
||||
else if (User.IsInRole("Student"))
|
||||
{
|
||||
result = await _examService.GetAllSubmissionAsync(userid.Id);
|
||||
result = await _examService.GetAllSubmissionAsync(user.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest("你没有相应的权限");
|
||||
return Forbid("你没有查看考试预览的权限。");
|
||||
}
|
||||
|
||||
if (result.Status)
|
||||
{
|
||||
return Ok(result.Result);
|
||||
}
|
||||
return BadRequest(result);
|
||||
else
|
||||
{
|
||||
return BadRequest(result.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[HttpGet("getAllSubmission")]
|
||||
/// <summary>
|
||||
/// 获取学生的所有提交记录。
|
||||
/// </summary>
|
||||
/// <returns>提交记录列表或错误信息。</returns>
|
||||
[HttpGet("getAllSubmissions")]
|
||||
[Authorize(Roles = "Student")]
|
||||
public async Task<IActionResult> GetAllSubmission()
|
||||
{
|
||||
if (User == null) return BadRequest("用户验证失败, 无效用户");
|
||||
var user = await _userManager.FindByEmailAsync(User.Identity.Name);
|
||||
if (user == null) return NotFound("没有找到用户");
|
||||
|
||||
var userid = await _userManager.FindByEmailAsync(User.Identity.Name);
|
||||
|
||||
var result = await _examService.GetAllSubmissionAsync(userid.Id);
|
||||
var result = await _examService.GetAllSubmissionAsync(user.Id);
|
||||
|
||||
if (result.Status)
|
||||
{
|
||||
return Ok(result.Result);
|
||||
}
|
||||
return BadRequest(result);
|
||||
else
|
||||
{
|
||||
return BadRequest(result.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 逻辑删除指定ID的考试/作业。
|
||||
/// </summary>
|
||||
/// <param name="id">要删除的考试/作业ID。</param>
|
||||
/// <returns>操作结果。</returns>
|
||||
[HttpDelete("delete/{id:guid}")]
|
||||
[Authorize(Roles = "Teacher")]
|
||||
[HttpDelete("{guid}")]
|
||||
public async Task<IActionResult> DeleteAsync(Guid guid)
|
||||
public async Task<IActionResult> DeleteAsync(Guid id)
|
||||
{
|
||||
var deleteResult = await _examService.DeleteAsync(guid);
|
||||
var deleteResult = await _examService.DeleteAsync(id);
|
||||
|
||||
if (deleteResult.Status)
|
||||
{
|
||||
return Ok();
|
||||
return NoContent();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (deleteResult.Message.Contains("未找到") || deleteResult.Message.Contains("not found", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return NotFound(deleteResult.Message);
|
||||
}
|
||||
return BadRequest(deleteResult.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 为指定学生指派作业
|
||||
/// </summary>
|
||||
/// <param name="AETSdto"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("assignmentExamToStudent")]
|
||||
[Authorize(Roles = "Teacher")]
|
||||
public async Task<IActionResult> AssignmentExamToStudent([FromBody] AssigExamToStudentsDto AETSdto)
|
||||
{
|
||||
var result = await _examService.AssignmentToStudentsAsync(AETSdto);
|
||||
if (result.Status)
|
||||
{
|
||||
return Ok(result.Result);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest(result.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 为所有学生指派作业
|
||||
/// </summary>
|
||||
/// <param name="AETSdto"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("assignmentExamToStudent/{id:guid}")]
|
||||
[Authorize(Roles = "Teacher")]
|
||||
public async Task<IActionResult> AssignmentExamToAllStudentsAsync(Guid id)
|
||||
{
|
||||
var user = await _userManager.FindByEmailAsync(User.Identity.Name ?? "");
|
||||
var result = await _examService.AssignmentToAllStudentsAsync(id, user.Id);
|
||||
if (result.Status)
|
||||
{
|
||||
return Ok(result.Result);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest(result.Message);
|
||||
}
|
||||
return BadRequest();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
279
TechHelper.Server/Controllers/SubmissionController.cs
Normal file
279
TechHelper.Server/Controllers/SubmissionController.cs
Normal file
@@ -0,0 +1,279 @@
|
||||
using Entities.Contracts;
|
||||
using Entities.DTO;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using TechHelper.Server.Services;
|
||||
using TechHelper.Services;
|
||||
|
||||
namespace TechHelper.Server.Controllers
|
||||
{
|
||||
[Route("api/submission")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
|
||||
public class SubmissionController : ControllerBase
|
||||
{
|
||||
private readonly UserManager<User> _userManager;
|
||||
private readonly ISubmissionServices _submissionServices;
|
||||
|
||||
public SubmissionController(UserManager<User> userManager, ISubmissionServices submissionServices)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_submissionServices = submissionServices;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前用户的所有错题。
|
||||
/// </summary>
|
||||
/// <returns>错题列表或错误信息。</returns>
|
||||
[HttpGet("getAllErrorQuestions")]
|
||||
public async Task<IActionResult> GetAllErrorQuestionsAsync()
|
||||
{
|
||||
var user = await _userManager.FindByEmailAsync(User.Identity.Name);
|
||||
if (user == null)
|
||||
{
|
||||
return NotFound("未找到当前用户信息。");
|
||||
}
|
||||
|
||||
var result = await _submissionServices.GetAllErrorQuestionsAsync(user.Id);
|
||||
|
||||
if (result.Status)
|
||||
{
|
||||
return Ok(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest(result.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定作业中当前用户的错题。
|
||||
/// </summary>
|
||||
/// <param name="assignmentId">作业ID。</param>
|
||||
/// <returns>错题列表或错误信息。</returns>
|
||||
[HttpGet("getAssignmentErrorQuestions/{assignmentId:guid}")]
|
||||
public async Task<IActionResult> GetAssignmentErrorQuestionsAsync(Guid assignmentId)
|
||||
{
|
||||
var user = await _userManager.FindByEmailAsync(User.Identity.Name);
|
||||
if (user == null)
|
||||
{
|
||||
return NotFound("未找到当前用户信息。");
|
||||
}
|
||||
|
||||
var result = await _submissionServices.GetAssignmentErrorQuestionsAsync(assignmentId, user.Id);
|
||||
|
||||
if (result.Status)
|
||||
{
|
||||
return Ok(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest(result.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定作业中当前用户的错题类型分布。
|
||||
/// </summary>
|
||||
/// <param name="assignmentId">作业ID。</param>
|
||||
/// <returns>错题类型分布数据。</returns>
|
||||
[HttpGet("getAssignmentErrorQuestionTypeDistribution/{assignmentId:guid}")]
|
||||
public async Task<IActionResult> GetAssignmentErrorQuestionTypeDisAsync(Guid assignmentId)
|
||||
{
|
||||
var user = await _userManager.FindByEmailAsync(User.Identity.Name);
|
||||
if (user == null)
|
||||
{
|
||||
return NotFound("未找到当前用户信息。");
|
||||
}
|
||||
|
||||
var result = await _submissionServices.GetAssignmentErrorQuestionTypeDisAsync(assignmentId, user.Id);
|
||||
|
||||
if (result.Status)
|
||||
{
|
||||
return Ok(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest(result.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定作业中所有学生的错题情况概述。
|
||||
/// </summary>
|
||||
/// <param name="assignmentId">作业ID。</param>
|
||||
/// <returns>每个学生的错题统计信息。</returns>
|
||||
[HttpGet("getAssignmentAllStudentsError/{assignmentId:guid}")]
|
||||
[Authorize(Roles = "Teacher")]
|
||||
public async Task<IActionResult> GetAssignmentAllStudentsError(Guid assignmentId)
|
||||
{
|
||||
// 假设当前用户是教师,如果需要验证,可以使用UserManager.IsInRoleAsync
|
||||
var user = await _userManager.FindByEmailAsync(User.Identity.Name);
|
||||
if (user == null)
|
||||
{
|
||||
return NotFound("未找到当前用户信息。");
|
||||
}
|
||||
// TODO: 根据实际业务需求,可能需要验证当前用户是否为该作业的教师。
|
||||
// 例如: var isTeacherOfAssignment = await _assignmentService.IsTeacherOfAssignment(assignmentId, user.Id);
|
||||
|
||||
var result = await _submissionServices.GetAssignmentAllStudentsError(assignmentId, user.Id); // 传入当前教师ID
|
||||
|
||||
if (result.Status)
|
||||
{
|
||||
return Ok(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest(result.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定作业中哪些学生做错了哪些题目。
|
||||
/// </summary>
|
||||
/// <param name="assignmentId">作业ID。</param>
|
||||
/// <returns>按题目分组的学生错题列表。</returns>
|
||||
[HttpGet("getQuestionErrorStudents/{assignmentId:guid}")]
|
||||
[Authorize(Roles = "Teacher")]
|
||||
public async Task<IActionResult> GetQuestionErrorStudents(Guid assignmentId)
|
||||
{
|
||||
var result = await _submissionServices.GetQuestionErrorStudents(assignmentId);
|
||||
|
||||
if (result.Status)
|
||||
{
|
||||
return Ok(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest(result.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加一次提交记录。
|
||||
/// </summary>
|
||||
/// <param name="model">提交的数据模型。</param>
|
||||
/// <returns>新创建的提交记录或错误信息。</returns>
|
||||
[HttpPost("add")]
|
||||
public async Task<IActionResult> AddAsync([FromBody] Submission model)
|
||||
{
|
||||
// 可以在这里获取当前用户ID并赋值给 model.StudentId,确保提交人信息正确
|
||||
// var user = await _userManager.FindByEmailAsync(User.Identity.Name);
|
||||
// if (user == null) return NotFound("未找到当前用户信息。");
|
||||
// model.StudentId = user.Id;
|
||||
|
||||
var result = await _submissionServices.AddAsync(model);
|
||||
|
||||
if (result.Status)
|
||||
{
|
||||
// 如果成功,通常返回 201 Created,并包含新资源的URI
|
||||
// 但如果服务层只返回数据,也可以直接 Ok
|
||||
return StatusCode(201, result); // 建议返回 201 Created
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest(result.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 逻辑删除指定ID的提交记录。
|
||||
/// </summary>
|
||||
/// <param name="id">提交ID。</param>
|
||||
/// <returns>操作结果。</returns>
|
||||
[HttpDelete("delete/{id:guid}")]
|
||||
public async Task<IActionResult> DeleteAsync(Guid id)
|
||||
{
|
||||
// TODO: 在服务层或控制器层添加权限检查:确保删除者有权删除此提交(例如是提交者本人、相关教师或管理员)
|
||||
// var user = await _userManager.FindByEmailAsync(User.Identity.Name);
|
||||
// if (user == null) return Unauthorized(); // 或 Forbidden
|
||||
|
||||
var result = await _submissionServices.DeleteAsync(id);
|
||||
|
||||
if (result.Status)
|
||||
{
|
||||
return NoContent(); // 204 No Content,表示删除成功但无内容返回
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest(result.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有提交记录(支持分页和查询)。
|
||||
/// </summary>
|
||||
/// <param name="query">查询参数,包含分页信息。</param>
|
||||
/// <returns>分页的提交记录列表。</returns>
|
||||
[HttpGet("getAll")]
|
||||
[Authorize(Roles = "Admin,Teacher")]
|
||||
public async Task<IActionResult> GetAllAsync([FromQuery] QueryParameter query)
|
||||
{
|
||||
var result = await _submissionServices.GetAllAsync(query);
|
||||
|
||||
if (result.Status)
|
||||
{
|
||||
return Ok(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest(result.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据ID获取单个提交记录。
|
||||
/// </summary>
|
||||
/// <param name="id">提交ID。</param>
|
||||
/// <returns>单个提交记录或未找到错误。</returns>
|
||||
[HttpGet("{id:guid}")]
|
||||
public async Task<IActionResult> GetAsync(Guid id)
|
||||
{
|
||||
var result = await _submissionServices.GetAsync(id);
|
||||
|
||||
if (result.Status)
|
||||
{
|
||||
return Ok(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest(result.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新提交记录。
|
||||
/// </summary>
|
||||
/// <param name="id">提交ID。</param>
|
||||
/// <param name="model">要更新的提交数据。</param>
|
||||
/// <returns>更新后的提交记录或错误信息。</returns>
|
||||
[HttpPut("update/{id:guid}")]
|
||||
public async Task<IActionResult> UpdateAsync(Guid id, [FromBody] Submission model)
|
||||
{
|
||||
if (id != model.Id) // 确保路径中的ID和模型中的ID一致
|
||||
{
|
||||
return BadRequest("路由ID与请求体中的ID不匹配。");
|
||||
}
|
||||
|
||||
// TODO: 权限检查:确保更新者有权更新此提交
|
||||
// var user = await _userManager.FindByEmailAsync(User.Identity.Name);
|
||||
// if (user == null) return Unauthorized();
|
||||
|
||||
var result = await _submissionServices.UpdateAsync(model);
|
||||
|
||||
if (result.Status)
|
||||
{
|
||||
return Ok(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
return BadRequest(result.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -3,6 +3,7 @@ using Entities.DTO;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using TechHelper.Server.Services;
|
||||
using TechHelper.Services;
|
||||
|
||||
namespace TechHelper.Server.Controllers
|
||||
@@ -11,12 +12,14 @@ namespace TechHelper.Server.Controllers
|
||||
[ApiController]
|
||||
public class UserController : ControllerBase
|
||||
{
|
||||
private IUserSerivces _userSerivces;
|
||||
private IClassService _classService;
|
||||
private UserManager<User> _userManager;
|
||||
public UserController(IClassService classService, UserManager<User> userManager)
|
||||
public UserController(IClassService classService, UserManager<User> userManager, IUserSerivces userSerivces)
|
||||
{
|
||||
_classService = classService;
|
||||
_userManager = userManager;
|
||||
_userSerivces = userSerivces;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,5 +29,22 @@ namespace TechHelper.Server.Controllers
|
||||
{
|
||||
return Ok();
|
||||
}
|
||||
|
||||
|
||||
|
||||
[HttpGet("restoreUserRole")]
|
||||
public async Task<IActionResult> RestoreUserRole()
|
||||
{
|
||||
var user = await _userManager.FindByEmailAsync(User.Identity.Name);
|
||||
|
||||
if (user == null) return NotFound();
|
||||
if (User.IsInRole("Teacher") || User.IsInRole("Student"))
|
||||
return Ok();
|
||||
var result = await _userSerivces.RestoreUserRoleInformation(user);
|
||||
if (result.Status)
|
||||
return Ok();
|
||||
else
|
||||
return Unauthorized();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user