117 lines
2.5 KiB
C#
117 lines
2.5 KiB
C#
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)
|
|
{
|
|
try
|
|
{
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
if (await _unitOfWork.SaveChangesAsync() > 0)
|
|
{
|
|
return ApiResponse.Success();
|
|
}
|
|
|
|
return ApiResponse.Error("保存失败");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ApiResponse.Error(ex.Message);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
}
|