using CandidateReviewContracts.BindingModels; using CandidateReviewContracts.SearchModels; using CandidateReviewContracts.StoragesContracts; using CandidateReviewContracts.ViewModels; using CandidateReviewDatabaseImplement.Models; namespace CandidateReviewDatabaseImplement.Implements { public class CriterionStorage : ICriterionStorage { public List GetFullList() { using var context = new CandidateReviewDatabase(); return context.Criterions .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(CriterionSearchModel model) { if (string.IsNullOrEmpty(model.Name)) { return new(); } using var context = new CandidateReviewDatabase(); return context.Criterions .Where(x => x.Name.Contains(model.Name)) .Select(x => x.GetViewModel) .ToList(); } public CriterionViewModel? GetElement(CriterionSearchModel model) { if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) { return null; } using var context = new CandidateReviewDatabase(); return context.Criterions .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; } public CriterionViewModel? Insert(CriterionBindingModel model) { var newCriterion = Criterion.Create(model); if (newCriterion == null) { return null; } using var context = new CandidateReviewDatabase(); context.Criterions.Add(newCriterion); context.SaveChanges(); return newCriterion.GetViewModel; } public CriterionViewModel? Update(CriterionBindingModel model) { using var context = new CandidateReviewDatabase(); var criterion = context.Criterions.FirstOrDefault(x => x.Id == model.Id); if (criterion == null) { return null; } criterion.Update(model); context.SaveChanges(); return criterion.GetViewModel; } public CriterionViewModel? Delete(CriterionBindingModel model) { using var context = new CandidateReviewDatabase(); var element = context.Criterions.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Criterions.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } } }