Поправил модели хранилища, начал стореджи

This commit is contained in:
Artyom_Yashin 2024-04-27 15:58:47 +04:00
parent 7c431ab723
commit 3f00c6f2f3
5 changed files with 172 additions and 5 deletions

View File

@ -19,8 +19,4 @@
<ProjectReference Include="..\BankContracts\BankContracts.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Implements\" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,79 @@
using BankContracts.BindingModels;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Implements
{
public class CardStorage : ICardStorage
{
public List<CardViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.Cards.Include(x => x.Client).Select(x => x.GetViewModel).ToList();
}
public List<CardViewModel> GetFilteredList(CardSearchModel model)
{
using var context = new BankDatabase();
return context.Cards.Include(x => x.Client)
.Where(x => (
(!model.Id.HasValue || x.Id == model.Id) &&
(!model.ClientId.HasValue || x.ClientId == model.ClientId) &&
(model.Number == null || x.Number == model.Number)
))
.Select(x => x.GetViewModel)
.ToList();
}
public CardViewModel? GetElement(CardSearchModel model)
{
if (!model.Id.HasValue) return null;
using var context = new BankDatabase();
return context.Cards.Include(x => x.Client).FirstOrDefault(
x => (model.Number != null && x.Number == model.Number)
)?.GetViewModel;
}
public CardViewModel? Insert(CardBindingModel model)
{
var newCard = Card.Create(model);
if (newCard == null)
{
return null;
}
using var context = new BankDatabase();
context.Cards.Add(newCard);
context.SaveChanges();
return newCard.GetViewModel;
}
public CardViewModel? Update(CardBindingModel model)
{
using var context = new BankDatabase();
var card = context.Cards.Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id);
if (card == null)
{
return null;
}
card.Update(model);
context.SaveChanges();
return card.GetViewModel;
}
public CardViewModel? Delete(CardBindingModel model)
{
using var context = new BankDatabase();
var card = context.Cards.Include(x => x.Client).FirstOrDefault(rec => rec.Id == model.Id);
if (card != null)
{
context.Cards.Remove(card);
context.SaveChanges();
return card.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,84 @@
using BankContracts.BindingModels;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Implements
{
public class ClientStorage : IClientStorage
{
public List<ClientViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.Clients.Select(x => x.GetViewModel).ToList();
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.Fio) && string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password))
{
return new();
}
using var context = new BankDatabase();
return context.Clients
.Where(x => string.IsNullOrEmpty(model.Fio) || x.Fio.Contains(model.Fio) &&
(string.IsNullOrEmpty(model.Email) || x.Email.Contains(model.Email)) &&
(string.IsNullOrEmpty(model.Password) || x.Password.Contains(model.Password)))
.Select(x => x.GetViewModel).ToList();
}
public ClientViewModel? GetElement(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.Fio) && string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
{
return null;
}
using var context = new BankDatabase();
return context.Clients
.FirstOrDefault(x => (string.IsNullOrEmpty(model.Fio) || x.Fio == model.Fio) &&
(!model.Id.HasValue || x.Id == model.Id) && (string.IsNullOrEmpty(model.Email) || x.Email == model.Email) &&
(string.IsNullOrEmpty(model.Password) || x.Password == model.Password))
?.GetViewModel;
}
public ClientViewModel? Insert(ClientBindingModel model)
{
var newClient = Client.Create(model);
if (newClient == null)
{
return null;
}
using var context = new BankDatabase();
context.Clients.Add(newClient);
context.SaveChanges();
return newClient.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel model)
{
using var context = new BankDatabase();
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 BankDatabase();
var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Clients.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -13,6 +13,7 @@ namespace BankDatabaseImplement.Models
{
public class Card : ICardModel
{
//убрать интерфейс
public int Id { get; set; }
[Required]
public string Number { get; set; } = string.Empty;
@ -26,8 +27,10 @@ namespace BankDatabaseImplement.Models
public DateOnly ExpirationDate { get; set; }
[Required]
public int ClientId { get; set; }
public virtual Client? Client { get; private set; }
[Required]
public int? AccountId { get; set; } = null;
public virtual IAccountModel? Account { get; private set; }
[ForeignKey("CardId")]
public virtual List<CardRequest> CardRequests { get; set; } = new();
[ForeignKey("SenderCardId")]
@ -73,6 +76,8 @@ namespace BankDatabaseImplement.Models
ExpirationDate = ExpirationDate,
ClientId = ClientId,
AccountId = AccountId,
ClientName = Client?.Fio ?? string.Empty,
AccountNumber = Account?.Number ?? string.Empty,
};
}
}

View File

@ -18,10 +18,11 @@ namespace BankDatabaseImplement.Models
public int Sum { get; set; }
[Required]
public DateTime OperationTime { get; set; }
[Required]
public int? SenderCardId { get; set; } = null;
public virtual Card? SenderCard { get; private set; }
[Required]
public int RecipientCardId { get; set; }
public virtual Card? RecipientCard { get; private set; }
public static Operation? Create(OperationBindingModel model)
{
@ -52,6 +53,8 @@ namespace BankDatabaseImplement.Models
OperationTime = OperationTime,
SenderCardId = SenderCardId,
RecipientCardId = RecipientCardId,
SenderCardNumber = SenderCard?.Number ?? string.Empty,
RecipientCardNumber = RecipientCard?.Number ?? string.Empty,
};
}
}