Сделал часть моделей хранилища

This commit is contained in:
Artyom_Yashin 2024-04-27 15:07:30 +04:00
parent 5c39e8da26
commit 7c431ab723
7 changed files with 114 additions and 3 deletions

View File

@ -13,5 +13,6 @@ namespace BankContracts.BindingModels
public int Sum { get; set; }
public DateTime RequestTime { get; set; }
public RequestStatus Status { get; set; }
public Dictionary<int, ICardModel> CardRequests { get; set; } = new();
}
}

View File

@ -18,5 +18,7 @@ namespace BankContracts.ViewModels
public DateTime RequestTime { get; set; }
[DisplayName("Статус заявки")]
public RequestStatus Status { get; set; }
public Dictionary<int, ICardModel> CardRequests { get; set; } = new();
}
}

View File

@ -11,5 +11,7 @@ namespace BankDataModels.Models
int Sum { get; set; }
DateTime RequestTime { get; set; }
RequestStatus Status { get; set; }
}
Dictionary<int, ICardModel> CardRequests { get; }
}
}

View File

@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using BankDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace BankDatabaseImplement
{
@ -13,5 +14,11 @@ namespace BankDatabaseImplement
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Client> Clients { get; set; }
public virtual DbSet<Card> Cards { get; set; }
public virtual DbSet<Operation> Operations { get; set; }
public virtual DbSet<Request> Requests { get; set; }
public virtual DbSet<CardRequest> CardRequests { get; set; }
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -8,5 +9,12 @@ namespace BankDatabaseImplement.Models
{
public class CardRequest
{
public int Id { get; set; }
[Required]
public int CardId { get; set; }
[Required]
public int RequestId { get; set; }
public virtual Card Card { get; set; } = new();
public virtual Request Request { get; set; } = new();
}
}

View File

@ -1,6 +1,7 @@
using Azure;
using BankContracts.BindingModels;
using BankContracts.ViewModels;
using BankDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
@ -10,7 +11,7 @@ using System.Threading.Tasks;
namespace BankDatabaseImplement.Models
{
public class Operation
public class Operation : IOperationModel
{
public int Id { get; set; }
[Required]

View File

@ -0,0 +1,90 @@
using BankContracts.BindingModels;
using BankDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Models
{
public class Request : IRequestModel
{
public int Id { get; set; }
[Required]
public int Sum { get; set; }
[Required]
public DateTime RequestTime { get; set; }
[Required]
public RequestStatus Status { get; set; }
[ForeignKey("RequestId")]
public virtual List<CardRequest> Cards { get; set; } = new();
private Dictionary<int, ICardModel>? _cardRequests { get; set; } = null;
[NotMapped]
public Dictionary<int, ICardModel> CardRequests
{
get
{
if (_cardRequests == null)
{
_cardRequests = Cards.ToDictionary(recPC => recPC.CardId, recPC => recPC.Card as ICardModel);
}
return _cardRequests;
}
}
public static Request Create(BankDatabase context, RequestBindingModel model)
{
return new Request
{
Id = model.Id,
Sum = model.Sum,
RequestTime = model.RequestTime,
Status = model.Status,
Cards = model.CardRequests.Select(x => new CardRequest
{
Card = context.Cards.First(y => y.Id == x.Key)
}).ToList()
};
}
public void Update(RequestBindingModel model)
{
Sum = model.Sum;
RequestTime = model.RequestTime;
Status = model.Status;
}
public RequestBindingModel GetViewModel => new()
{
Id = Id,
Sum = Sum,
RequestTime = RequestTime,
Status = Status,
CardRequests = CardRequests
};
public void UpdateCards(BankDatabase context, RequestBindingModel model)
{
var CardRequests = context.CardRequests.Where(rec => rec.RequestId == model.Id).ToList();
if (CardRequests != null && CardRequests.Count > 0)
{ // удалили те, которых нет в модели
context.CardRequests.RemoveRange(CardRequests.Where(rec => !model.CardRequests.ContainsKey(rec.CardId)));
context.SaveChanges();
}
var Request = context.Requests.First(x => x.Id == Id);
foreach (var request in model.CardRequests)
{
context.CardRequests.Add(new CardRequest
{
Request = Request,
Card = context.Cards.First(x => x.Id == request.Key),
});
context.SaveChanges();
}
_cardRequests = null;
}
}
}