77 lines
1.5 KiB
C#
77 lines
1.5 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;
|
|
|
|
|
|
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] ExamDto examDto)
|
|
{
|
|
var result = await _examService.AddAsync(examDto);
|
|
if (result.Status)
|
|
{
|
|
return Ok(result);
|
|
}
|
|
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(string user)
|
|
{
|
|
string? userId = User.Identity.Name;
|
|
|
|
var userid = await _userManager.FindByEmailAsync(user);
|
|
if (userid == null) return BadRequest("用户验证失败, 无效用户");
|
|
|
|
|
|
|
|
var result = await _examService.GetAllExamPreview(userid.Id);
|
|
|
|
if (result.Status)
|
|
{
|
|
return Ok(result.Result);
|
|
}
|
|
return BadRequest(result);
|
|
}
|
|
|
|
}
|
|
}
|