Сделал часть моделей хранилища
This commit is contained in:
parent
5c39e8da26
commit
7c431ab723
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -11,5 +11,7 @@ namespace BankDataModels.Models
|
||||
int Sum { get; set; }
|
||||
DateTime RequestTime { get; set; }
|
||||
RequestStatus Status { get; set; }
|
||||
}
|
||||
Dictionary<int, ICardModel> CardRequests { get; }
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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; }
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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]
|
||||
|
90
Bank/BankDatabaseImplement/Models/Request.cs
Normal file
90
Bank/BankDatabaseImplement/Models/Request.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user