144 lines
3.1 KiB
C#
144 lines
3.1 KiB
C#
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 System.Security.Claims;
|
|
using TechHelper.Services;
|
|
|
|
|
|
namespace TechHelper.Server.Controllers
|
|
{
|
|
[Route("api/exam")]
|
|
[ApiController]
|
|
[Authorize]
|
|
|
|
|
|
public class ExamController : ControllerBase
|
|
{
|
|
private IExamService _examService;
|
|
private readonly UserManager<User> _userManager;
|
|
|
|
public ExamController(IExamService examService, UserManager<User> userManager)
|
|
{
|
|
_examService = examService;
|
|
_userManager = userManager;
|
|
}
|
|
|
|
[HttpPost("add")]
|
|
public async Task<IActionResult> AddExam(
|
|
[FromBody] AssignmentDto examDto)
|
|
{
|
|
var user = await _userManager.FindByEmailAsync(User.Identity?.Name ?? "");
|
|
if (user == null) return BadRequest("无效的用户");
|
|
|
|
examDto.CreatorId = user.Id;
|
|
var result = await _examService.CreateExamAsync(examDto);
|
|
if (result.Status)
|
|
{
|
|
return Ok(result);
|
|
}
|
|
else
|
|
{
|
|
return BadRequest();
|
|
}
|
|
}
|
|
|
|
[HttpPost("submission")]
|
|
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)
|
|
{
|
|
return Ok(result);
|
|
}
|
|
else
|
|
{
|
|
return BadRequest(result.Message);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return BadRequest("你没有权限修改");
|
|
}
|
|
}
|
|
|
|
[HttpGet("get")]
|
|
public async Task<IActionResult> GetExamById(Guid id)
|
|
{
|
|
|
|
var result = await _examService.GetAsync(id);
|
|
if (result.Status)
|
|
return Ok(result.Result);
|
|
else
|
|
return BadRequest("查找失败");
|
|
}
|
|
|
|
|
|
[HttpGet("getAllPreview")]
|
|
public async Task<IActionResult> GetAllExamPreview()
|
|
{
|
|
if (User == null) return BadRequest("用户验证失败, 无效用户");
|
|
|
|
var userid = await _userManager.FindByEmailAsync(User.Identity.Name);
|
|
|
|
|
|
var result = new ApiResponse();
|
|
if (User.IsInRole("Teacher"))
|
|
{
|
|
result = await _examService.GetAllExamPreviewsAsync(userid.Id);
|
|
}
|
|
else if (User.IsInRole("Student"))
|
|
{
|
|
result = await _examService.GetAllSubmissionAsync(userid.Id);
|
|
}
|
|
else
|
|
{
|
|
return BadRequest("你没有相应的权限");
|
|
}
|
|
|
|
if (result.Status)
|
|
{
|
|
return Ok(result.Result);
|
|
}
|
|
return BadRequest(result);
|
|
}
|
|
|
|
|
|
[HttpGet("getAllSubmission")]
|
|
public async Task<IActionResult> GetAllSubmission()
|
|
{
|
|
if (User == null) return BadRequest("用户验证失败, 无效用户");
|
|
|
|
var userid = await _userManager.FindByEmailAsync(User.Identity.Name);
|
|
|
|
var result = await _examService.GetAllSubmissionAsync(userid.Id);
|
|
|
|
if (result.Status)
|
|
{
|
|
return Ok(result.Result);
|
|
}
|
|
return BadRequest(result);
|
|
}
|
|
|
|
|
|
[Authorize(Roles = "Teacher")]
|
|
[HttpDelete("{guid}")]
|
|
public async Task<IActionResult> DeleteAsync(Guid guid)
|
|
{
|
|
var deleteResult = await _examService.DeleteAsync(guid);
|
|
if (deleteResult.Status)
|
|
{
|
|
return Ok();
|
|
}
|
|
return BadRequest();
|
|
}
|
|
}
|
|
}
|