2023-05-17 10:48:52 +04:00

96 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskTrackerContracts.BindingModels;
using TaskTrackerContracts.SearchModels;
using TaskTrackerContracts.StoragesContracts;
using TaskTrackerContracts.ViewModels;
namespace TaskTrackerDatabaseImplement.Implements
{
public class CompanyStorage : ICompanyStorage
{
public CompanyViewModel? Delete(CompanyBindingModel model)
{
using var context = new TasktrackerContext();
var element = context.Companies.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.Login) && !model.Id.HasValue)
{
return null;
}
using var context = new TasktrackerContext();
return context.Companies
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Login) && x.Login ==
model.Login) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public List<CompanyViewModel> GetFilteredList(CompanySearchModel model)
{
if (string.IsNullOrEmpty(model.Login) || !model.Id.HasValue)
{
return new();
}
using var context = new TasktrackerContext();
return context.Companies
.Where(x => x.Login.Equals(model.Login) && x.Password.Equals(model.Password))
.Select(x => x.GetViewModel)
.ToList();
}
public List<CompanyViewModel> GetFullList()
{
using var context = new TasktrackerContext();
return context.Companies
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public CompanyViewModel? Insert(CompanyBindingModel model)
{
var newCompany = Company.Create(model);
if (newCompany == null)
{
return null;
}
using var context = new TasktrackerContext();
context.Companies.Add(newCompany);
context.SaveChanges();
return newCompany.GetViewModel;
}
public CompanyViewModel? Update(CompanyBindingModel model)
{
using var context = new TasktrackerContext();
var company = context.Companies.FirstOrDefault(x => x.Id ==
model.Id);
if (company == null)
{
return null;
}
company.Update(model);
context.SaveChanges();
return company.GetViewModel;
}
}
}