using CandidateReviewContracts.BindingModels; using CandidateReviewContracts.SearchModels; using CandidateReviewContracts.StoragesContracts; using CandidateReviewContracts.ViewModels; using CandidateReviewDatabaseImplement.Models; using Microsoft.EntityFrameworkCore; namespace CandidateReviewDatabaseImplement.Implements { public class VacancyStorage : IVacancyStorage { public VacancyViewModel? Delete(VacancyBindingModel model) { using var context = new CandidateReviewDatabase(); var element = context.Vacancies .Include(x => x.Company) .Include(x => x.Resumes) .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Vacancies.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } public VacancyViewModel? GetElement(VacancySearchModel model) { if (!model.Id.HasValue) { return null; } using var context = new CandidateReviewDatabase(); if (model.CompanyId.HasValue && !string.IsNullOrEmpty(model.JobTitle)) { return context.Vacancies .Include(x => x.Company) .Include(x => x.Resumes) .FirstOrDefault(x => x.CompanyId == model.CompanyId && x.JobTitle.Equals(model.JobTitle)) ?.GetViewModel; } if (!string.IsNullOrEmpty(model.Tags)) { return context.Vacancies .Include(x => x.Company) .Include(x => x.Resumes) .FirstOrDefault(x => x.Tags.Contains(model.Tags)) ?.GetViewModel; } return context.Vacancies .Include(x => x.Company) .Include(x => x.Resumes) .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) ?.GetViewModel; } public List GetFilteredList(VacancySearchModel model) { if (model is null) { return new(); } using var context = new CandidateReviewDatabase(); if (model.CompanyId.HasValue && !string.IsNullOrEmpty(model.JobTitle)) { return context.Vacancies .Include(x => x.Company) .Include(x => x.Resumes) .Where(x => x.CompanyId == model.CompanyId && x.JobTitle.Equals(model.JobTitle)) .ToList() .Select(x => x.GetViewModel) .ToList(); } if (!string.IsNullOrEmpty(model.Tags)) { var tags = model.Tags.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(t => t.ToLowerInvariant()).ToArray(); return context.Vacancies .Include(x => x.Company) .Include(x => x.Resumes) .Where(x => tags.Any(tag => x.Tags.Contains(tag))) .ToList() .Select(x => x.GetViewModel) .ToList(); } return context.Vacancies .Include(x => x.Company) .Include(x => x.Resumes) .ToList() .Select(x => x.GetViewModel) .ToList(); } public List GetFullList() { using var context = new CandidateReviewDatabase(); return context.Vacancies .Include(x => x.Company) .Include(x => x.Resumes) .Select(x => x.GetViewModel).ToList(); } public VacancyViewModel? Insert(VacancyBindingModel model) { var newVacancy = Vacancy.Create(model); if (newVacancy == null) { return null; } using var context = new CandidateReviewDatabase(); context.Vacancies.Add(newVacancy); context.SaveChanges(); return context.Vacancies .Include(x => x.Company) .Include(x => x.Resumes) .FirstOrDefault(x => x.Id == newVacancy.Id) ?.GetViewModel; } public VacancyViewModel? Update(VacancyBindingModel model) { using var context = new CandidateReviewDatabase(); var vacancy = context.Vacancies .Include(x => x.Company) .Include(x => x.Resumes) .FirstOrDefault(x => x.Id == model.Id); if (vacancy == null) { return null; } vacancy.Update(model); context.SaveChanges(); return vacancy.GetViewModel; } } }