82 lines
2.6 KiB
C#
82 lines
2.6 KiB
C#
using HotelContracts.BindingModels;
|
|
using HotelContracts.SearchModels;
|
|
using HotelContracts.StoragesContracts;
|
|
using HotelContracts.ViewModels;
|
|
using HotelDatabaseImplement.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace HotelDatabaseImplement.Implements
|
|
{
|
|
public class WorkerStorage : IWorkerStorage
|
|
{
|
|
public WorkerViewModel? Delete(WorkerBindingModel model)
|
|
{
|
|
using var context = new HotelDatabase();
|
|
var element = context.Workers.FirstOrDefault(x => x.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Workers.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public WorkerViewModel? GetElement(WorkerSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.FIO) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new HotelDatabase();
|
|
return context.Workers.FirstOrDefault(x => (!string.IsNullOrEmpty(model.FIO)) && x.FIO == model.FIO || model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
|
}
|
|
|
|
public List<WorkerViewModel> GetFilteredList(WorkerSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.FIO))
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new HotelDatabase();
|
|
return context.Workers.Where(x => x.FIO.Contains(model.FIO)).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<WorkerViewModel> GetFullList()
|
|
{
|
|
using var context = new HotelDatabase();
|
|
return context.Workers.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public WorkerViewModel? Insert(WorkerBindingModel model)
|
|
{
|
|
var newWorker = Worker.Create(model);
|
|
if (newWorker == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new HotelDatabase();
|
|
context.Workers.Add(newWorker);
|
|
context.SaveChanges();
|
|
return newWorker.GetViewModel;
|
|
}
|
|
|
|
public WorkerViewModel? Update(WorkerBindingModel model)
|
|
{
|
|
using var context = new HotelDatabase();
|
|
var component = context.Workers.FirstOrDefault(x => x.Id == model.Id);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
component.Update(model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
}
|
|
}
|