CourseWork_KPO/CandidateReviewDatabaseImplement/Implements/CompanyStorage.cs

98 lines
3.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 CompanyStorage : ICompanyStorage
{
public CompanyViewModel? Delete(CompanyBindingModel model)
{
using var context = new CandidateReviewDatabase();
var element = context.Companies.Include(x => x.Users).Include(x => x.Vacancies).FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Companies.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public CompanyViewModel? GetElement(CompanySearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
using var context = new CandidateReviewDatabase();
return context.Companies
.Include(x => x.Users)
.Include(x => x.Vacancies)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?
.GetViewModel;
}
public List<CompanyViewModel> GetFilteredList(CompanySearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new CandidateReviewDatabase();
return context.Companies
.Include(x => x.Users)
.Include(x => x.Vacancies)
.Where(x => x.Name.Equals(model.Name))
.Select(x => x.GetViewModel)
.ToList();
}
public List<CompanyViewModel> GetFullList()
{
using var context = new CandidateReviewDatabase();
return context.Companies
.Include(x => x.Users)
.Include(x => x.Vacancies)
.Select(x => x.GetViewModel)
.ToList();
}
public CompanyViewModel? Insert(CompanyBindingModel model)
{
var newCompany = Company.Create(model);
if (newCompany == null)
{
return null;
}
using var context = new CandidateReviewDatabase();
context.Companies.Add(newCompany);
context.SaveChanges();
return context.Companies
.Include(x => x.Users)
.Include(x => x.Vacancies)
.FirstOrDefault(x => x.Id == model.Id)?
.GetViewModel;
}
public CompanyViewModel? Update(CompanyBindingModel model)
{
using var context = new CandidateReviewDatabase();
var company = context.Companies
.Include(x => x.Users)
.Include(x => x.Vacancies)
.FirstOrDefault(x => x.Id == model.Id);
if (company == null)
{
return null;
}
company.Update(model);
context.SaveChanges();
return company.GetViewModel;
}
}
}