117 lines
4.1 KiB
C#
117 lines
4.1 KiB
C#
|
using CandidateReviewContracts.BindingModels;
|
|||
|
using CandidateReviewContracts.ViewModels;
|
|||
|
using CandidateReviewDataModels.Models;
|
|||
|
using System.ComponentModel.DataAnnotations;
|
|||
|
using System.ComponentModel.DataAnnotations.Schema;
|
|||
|
|
|||
|
namespace CandidateReviewDatabaseImplement.Models
|
|||
|
{
|
|||
|
public class Assessment : IAssessmentModel
|
|||
|
{
|
|||
|
[Required]
|
|||
|
public int ResumeId { get; set; }
|
|||
|
[Required]
|
|||
|
public int UserId { get; set; }
|
|||
|
|
|||
|
public int? Rating { get; set; }
|
|||
|
[Required]
|
|||
|
public DateTime CreatedAt { get; set; }
|
|||
|
|
|||
|
public string? Comment { get; set; }
|
|||
|
|
|||
|
public int Id { get; set; }
|
|||
|
public virtual User User { get; set; }
|
|||
|
public virtual Resume Resume { get; set; }
|
|||
|
|
|||
|
private Dictionary<int, (ICriterionModel, int)>? _assessmentCriterions = null;
|
|||
|
|
|||
|
public Dictionary<int, (ICriterionModel, int)> AssessmentCriterions
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_assessmentCriterions == null)
|
|||
|
{
|
|||
|
_assessmentCriterions = Criterions
|
|||
|
.ToDictionary(recAC => recAC.CriterionId, recAC =>
|
|||
|
(recAC.Criterion as ICriterionModel, recAC.Value));
|
|||
|
}
|
|||
|
return _assessmentCriterions;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
[ForeignKey("AssessmentId")]
|
|||
|
public virtual List<AssessmentCriterion> Criterions { get; set; } = new();
|
|||
|
|
|||
|
public static Assessment? Create(CandidateReviewDatabase context, AssessmentBindingModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
return new Assessment()
|
|||
|
{
|
|||
|
Id = model.Id,
|
|||
|
ResumeId = model.ResumeId,
|
|||
|
UserId = model.UserId,
|
|||
|
Rating = model.Rating,
|
|||
|
CreatedAt = model.CreatedAt,
|
|||
|
Comment = model.Comment,
|
|||
|
Criterions = model.AssessmentCriterions.Select(x => new AssessmentCriterion()
|
|||
|
{
|
|||
|
Criterion = context.Criterions.First(y => y.Id == x.Key),
|
|||
|
Value = x.Value.Item2
|
|||
|
}).ToList()
|
|||
|
};
|
|||
|
}
|
|||
|
public void Update(AssessmentBindingModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
ResumeId = model.ResumeId;
|
|||
|
UserId = model.UserId;
|
|||
|
Rating = model.Rating;
|
|||
|
CreatedAt = model.CreatedAt;
|
|||
|
Comment = model.Comment;
|
|||
|
}
|
|||
|
public AssessmentViewModel GetViewModel => new()
|
|||
|
{
|
|||
|
Id = Id,
|
|||
|
ResumeId = ResumeId,
|
|||
|
UserId = UserId,
|
|||
|
Rating = Rating,
|
|||
|
CreatedAt = CreatedAt,
|
|||
|
Comment = Comment
|
|||
|
};
|
|||
|
|
|||
|
public void UpdateCriterions(CandidateReviewDatabase context, AssessmentBindingModel model)
|
|||
|
{
|
|||
|
var assessmentCriterions = context.AssessmentCriterions.Where(rec => rec.AssessmentId == model.Id).ToList();
|
|||
|
if (assessmentCriterions != null && assessmentCriterions.Count > 0)
|
|||
|
{
|
|||
|
context.AssessmentCriterions.RemoveRange(assessmentCriterions.Where(rec => !model.AssessmentCriterions.ContainsKey(rec.CriterionId)));
|
|||
|
context.SaveChanges();
|
|||
|
foreach (var updateCriterion in assessmentCriterions)
|
|||
|
{
|
|||
|
updateCriterion.Value = model.AssessmentCriterions[updateCriterion.CriterionId].Item2;
|
|||
|
model.AssessmentCriterions.Remove(updateCriterion.CriterionId);
|
|||
|
}
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
var assessment = context.Assessments.First(x => x.Id == Id);
|
|||
|
foreach (var ac in model.AssessmentCriterions)
|
|||
|
{
|
|||
|
context.AssessmentCriterions.Add(new AssessmentCriterion
|
|||
|
{
|
|||
|
Assessment = assessment,
|
|||
|
Criterion = context.Criterions.First(x => x.Id == ac.Key),
|
|||
|
Value = ac.Value.Item2
|
|||
|
});
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
_assessmentCriterions = null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|