ClientsStorages

This commit is contained in:
shadowik 2023-04-01 19:08:55 +04:00
parent 2e30751157
commit 08f2d15991
2 changed files with 174 additions and 4 deletions

View File

@ -1,4 +1,8 @@
using System;
using BankYouBankruptContracts.BindingModels;
using BankYouBankruptContracts.SearchModels;
using BankYouBankruptContracts.ViewModels;
using BankYouBankruptDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -8,5 +12,100 @@ namespace BankYouBankruptDatabaseImplement.Implements
{
public class CardStorage
{
}
public CardViewModel? Delete(CardBindingModel model)
{
using var context = new BankYouBancruptDatabase();
var element = context.Cards.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Cards.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public CardViewModel? GetElement(CardSearchModel model)
{
if (string.IsNullOrEmpty(model.Number) && !model.Id.HasValue)
{
return null;
}
using var context = new BankYouBancruptDatabase();
return context.Cards
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Number) && x.Number == model.Number) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public List<CardViewModel> GetFilteredList(CardSearchModel model)
{
if (string.IsNullOrEmpty(model.Number))
{
return new();
}
using var context = new BankYouBancruptDatabase();
return context.Cards
.Where(x => x.Number.Contains(model.Number))
.Select(x => x.GetViewModel)
.ToList();
}
public List<CardViewModel> GetFullList()
{
using var context = new BankYouBancruptDatabase();
return context.Cards
.Select(x => x.GetViewModel)
.ToList();
}
public CardViewModel? Insert(CardBindingModel model)
{
using var context = new BankYouBancruptDatabase();
var newCard = Card.Create(context, model);
if (newCard == null)
{
return null;
}
context.Cards.Add(newCard);
context.SaveChanges();
return newCard.GetViewModel;
}
public CardViewModel? Update(CardBindingModel model)
{
using var context = new BankYouBancruptDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var card = context.Cards.FirstOrDefault(rec => rec.Id == model.Id);
if (card == null)
{
return null;
}
card.Update(model);
context.SaveChanges();
transaction.Commit();
return card.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -1,4 +1,8 @@
using System;
using BankYouBankruptContracts.BindingModels;
using BankYouBankruptContracts.SearchModels;
using BankYouBankruptContracts.ViewModels;
using BankYouBankruptDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -8,5 +12,72 @@ namespace BankYouBankruptDatabaseImplement.Implements
{
public class ClientStorage
{
}
public ClientViewModel? Delete(ClientBindingModel model)
{
using var context = new BankYouBancruptDatabase();
var element = context.Clients.FirstOrDefault(rec => rec.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.Email) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue)
{
return null;
}
using var context = new BankYouBancruptDatabase();
return context.Clients.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Email) && x.Email == model.Email && !string.IsNullOrEmpty(model.Password) && x.Password == model.Password) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new BankYouBancruptDatabase();
return context.Clients.Where(x => x.Name.Contains(model.Name)).Select(x => x.GetViewModel).ToList();
}
public List<ClientViewModel> GetFullList()
{
using var context = new BankYouBancruptDatabase();
return context.Clients.Select(x => x.GetViewModel).ToList();
}
public ClientViewModel? Insert(ClientBindingModel model)
{
var newComponent = Client.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new BankYouBancruptDatabase();
context.Clients.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel model)
{
using var context = new BankYouBancruptDatabase();
var component = context.Clients.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
}
}