72 lines
2.2 KiB
C#
72 lines
2.2 KiB
C#
using DinerContracts.BindingModels;
|
|
using DinerContracts.SearchModels;
|
|
using DinerContracts.StoragesContracts;
|
|
using DinerContracts.ViewModels;
|
|
using DinerDataBaseImplement.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DinerDataBaseImplement.Implements {
|
|
public class ClientStorage : IClientStorage {
|
|
|
|
public ClientViewModel? Insert(ClientBindingModel model) {
|
|
var newClient = Client.Create(model);
|
|
if (newClient == null) {
|
|
return null;
|
|
}
|
|
using var context = new DinerDataBase();
|
|
context.Clients.Add(newClient);
|
|
context.SaveChanges();
|
|
return newClient.GetViewModel;
|
|
}
|
|
|
|
public ClientViewModel? Update(ClientBindingModel model) {
|
|
using var context = new DinerDataBase();
|
|
var client = context.Clients.FirstOrDefault(x => x.ID == model.ID);
|
|
if (client == null) {
|
|
return null;
|
|
}
|
|
client.Update(model);
|
|
context.SaveChanges();
|
|
return client.GetViewModel;
|
|
}
|
|
|
|
public ClientViewModel? Delete(ClientBindingModel model) {
|
|
using var context = new DinerDataBase();
|
|
var element = context.Clients.FirstOrDefault(x => x.ID == model.ID);
|
|
if (element != null) {
|
|
context.Clients.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public ClientViewModel? GetElement(ClientSearchModel model) {
|
|
if (string.IsNullOrEmpty(model.ClientFIO) && !model.ID.HasValue) {
|
|
return null;
|
|
}
|
|
using var context = new DinerDataBase();
|
|
return context.Clients.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ClientFIO) &&
|
|
x.ClientFIO == model.ClientFIO) || model.ID.HasValue &&
|
|
x.ID == model.ID)?.GetViewModel;
|
|
}
|
|
|
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel model) {
|
|
if (string.IsNullOrEmpty(model.ClientFIO)) {
|
|
return new();
|
|
}
|
|
using var context = new DinerDataBase();
|
|
return context.Clients.Where(x => x.ClientFIO.Contains(model.ClientFIO)).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<ClientViewModel> GetFullList() {
|
|
using var context = new DinerDataBase();
|
|
return context.Clients.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
}
|
|
}
|