143 lines
4.4 KiB
C#
143 lines
4.4 KiB
C#
|
using CandidateReviewContracts.BindingModels;
|
|||
|
using CandidateReviewContracts.SearchModels;
|
|||
|
using CandidateReviewContracts.StoragesContracts;
|
|||
|
using CandidateReviewContracts.ViewModels;
|
|||
|
using CandidateReviewDatabaseImplement.Models;
|
|||
|
using Microsoft.EntityFrameworkCore;
|
|||
|
|
|||
|
namespace CandidateReviewDatabaseImplement.Implements
|
|||
|
{
|
|||
|
public class AssessmentStorage : IAssessmentStorage
|
|||
|
{
|
|||
|
public AssessmentViewModel? Delete(AssessmentBindingModel model)
|
|||
|
{
|
|||
|
using var context = new CandidateReviewDatabase();
|
|||
|
|
|||
|
var element = context.Assessments
|
|||
|
.Include(x => x.User)
|
|||
|
.Include(x => x.Resume)
|
|||
|
.Include(x => x.Criterions)
|
|||
|
.ThenInclude(x => x.Criterion)
|
|||
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|||
|
|
|||
|
if (element != null)
|
|||
|
{
|
|||
|
context.Assessments.Remove(element);
|
|||
|
context.SaveChanges();
|
|||
|
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
public AssessmentViewModel? GetElement(AssessmentSearchModel model)
|
|||
|
{
|
|||
|
if (!model.Id.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
using var context = new CandidateReviewDatabase();
|
|||
|
if (model.ResumeId.HasValue)
|
|||
|
{
|
|||
|
return context.Assessments
|
|||
|
.FirstOrDefault(x => x.ResumeId == model.ResumeId)
|
|||
|
?.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
if (model.UserId.HasValue)
|
|||
|
{
|
|||
|
return context.Assessments
|
|||
|
.FirstOrDefault(x => x.UserId == model.UserId)
|
|||
|
?.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
return context.Assessments
|
|||
|
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
|
|||
|
?.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public List<AssessmentViewModel> GetFilteredList(AssessmentSearchModel model)
|
|||
|
{
|
|||
|
if (model is null)
|
|||
|
{
|
|||
|
return new();
|
|||
|
}
|
|||
|
using var context = new CandidateReviewDatabase();
|
|||
|
if (model.ResumeId.HasValue)
|
|||
|
{
|
|||
|
return context.Assessments
|
|||
|
.Where(x => x.ResumeId == model.ResumeId)
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
if (model.UserId.HasValue)
|
|||
|
{
|
|||
|
return context.Assessments
|
|||
|
.Where(x => x.UserId == model.UserId)
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
return context.Assessments
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
public List<AssessmentViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new CandidateReviewDatabase();
|
|||
|
|
|||
|
return context.Assessments
|
|||
|
.Include(x => x.User)
|
|||
|
.Include(x => x.Resume)
|
|||
|
.Include(x => x.Criterions)
|
|||
|
.ThenInclude(x => x.Criterion)
|
|||
|
.Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
|
|||
|
public AssessmentViewModel? Insert(AssessmentBindingModel model)
|
|||
|
{
|
|||
|
using var context = new CandidateReviewDatabase();
|
|||
|
var newAssessment = Assessment.Create(context, model);
|
|||
|
|
|||
|
if (newAssessment == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
context.Assessments.Add(newAssessment);
|
|||
|
context.SaveChanges();
|
|||
|
|
|||
|
return newAssessment.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public AssessmentViewModel? Update(AssessmentBindingModel model)
|
|||
|
{
|
|||
|
using var context = new CandidateReviewDatabase();
|
|||
|
using var transaction = context.Database.BeginTransaction();
|
|||
|
try
|
|||
|
{
|
|||
|
var assessment = context.Assessments.FirstOrDefault(rec => rec.Id == model.Id);
|
|||
|
if (assessment == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
assessment.Update(model);
|
|||
|
context.SaveChanges();
|
|||
|
assessment.UpdateCriterions(context, model);
|
|||
|
transaction.Commit();
|
|||
|
return assessment.GetViewModel;
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
transaction.Rollback();
|
|||
|
throw;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|