Создание классов для роли Поставщик в BankDatabaseImplement.Models. Фикс Percent в ICreditProgramModel. Создание CurrencyStorage.
This commit is contained in:
parent
9292d3c422
commit
748cbe8613
@ -11,9 +11,9 @@ namespace BankContracts.BindingModels
|
||||
{
|
||||
public string Name { get; set; } = String.Empty;
|
||||
|
||||
public int Persent { get; set; }
|
||||
public float Percent { get; set; }
|
||||
|
||||
public Dictionary<int, ICurrencyModel> Currencies { get; set; } = new();
|
||||
public Dictionary<int, ICurrencyModel> CreditProgramCurrencies { get; set; } = new();
|
||||
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
@ -13,8 +13,8 @@ namespace BankContracts.ViewModels
|
||||
[DisplayName("Название кредитной программы")]
|
||||
public string Name { get; set; } = String.Empty;
|
||||
[DisplayName("Процент кредитования")]
|
||||
public float Persent { get; set; }
|
||||
public Dictionary<int, ICurrencyModel> Currencies { get; set; } = new();
|
||||
public float Percent { get; set; }
|
||||
public Dictionary<int, ICurrencyModel> CreditProgramCurrencies { get; set; } = new();
|
||||
[DisplayName("Номер программы")]
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ namespace BankDataModels.Models
|
||||
public interface ICreditProgramModel : IId
|
||||
{
|
||||
string Name { get;}
|
||||
float Persent { get;}
|
||||
Dictionary<int, ICurrencyModel> Currencies { get; }
|
||||
float Percent { get;}
|
||||
Dictionary<int, ICurrencyModel> CreditProgramCurrencies { get; }
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,10 @@ namespace BankDatabaseImplement
|
||||
public virtual DbSet<Transfer> Transfers { set; get; }
|
||||
public virtual DbSet<DealPayment> DealPayments { set; get; }
|
||||
public virtual DbSet<CurrencyPayment> CurrencyPayments { set; get; }
|
||||
|
||||
public virtual DbSet<Currency> Currencies { set; get; }
|
||||
public virtual DbSet<BankOperator> BankOperators { set; get; }
|
||||
public virtual DbSet<CreditProgram> CreditPrograms { set; get; }
|
||||
public virtual DbSet<CurrencyPurchase> CurrencyPurchases { set; get; }
|
||||
public virtual DbSet<CreditProgramCurrency> CreditProgramCurrencies { set; get; }
|
||||
}
|
||||
}
|
||||
|
88
Bank/BankDatabaseImplement/Implements/CurrencyStorage.cs
Normal file
88
Bank/BankDatabaseImplement/Implements/CurrencyStorage.cs
Normal file
@ -0,0 +1,88 @@
|
||||
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 CurrencyStorage : ICurrencyStorage
|
||||
{
|
||||
public CurrencyViewModel? Delete(CurrencyBindingModel model)
|
||||
{
|
||||
using var context = new BankDatabase();
|
||||
var element = context.Currencies.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Currencies.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public CurrencyViewModel? GetElement(CurrencySearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new BankDatabase();
|
||||
return context.Currencies
|
||||
.FirstOrDefault(x => x.Id == model.Id)
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public List<CurrencyViewModel> GetFilteredList(CurrencySearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new BankDatabase();
|
||||
return context.Currencies
|
||||
.Where(x => x.Id == model.Id)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<CurrencyViewModel> GetFullList()
|
||||
{
|
||||
using var context = new BankDatabase();
|
||||
return context.Currencies
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public CurrencyViewModel? Insert(CurrencyBindingModel model)
|
||||
{
|
||||
using var context = new BankDatabase();
|
||||
var newCurrency = Currency.Create(context, model);
|
||||
if (newCurrency == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Currencies.Add(newCurrency);
|
||||
context.SaveChanges();
|
||||
return newCurrency.GetViewModel;
|
||||
}
|
||||
|
||||
public CurrencyViewModel? Update(CurrencyBindingModel model)
|
||||
{
|
||||
using var context = new BankDatabase();
|
||||
var currency = context.Currencies.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (currency == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
currency.Update(model);
|
||||
context.SaveChanges();
|
||||
return currency.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
73
Bank/BankDatabaseImplement/Models/BankOperator.cs
Normal file
73
Bank/BankDatabaseImplement/Models/BankOperator.cs
Normal file
@ -0,0 +1,73 @@
|
||||
using BankContracts.BindingModels;
|
||||
using BankContracts.ViewModels;
|
||||
using BankDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BankDatabaseImplement.Models
|
||||
{
|
||||
public class BankOperator : IBankOperatorModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Login { get; set; } = string.Empty;
|
||||
public string Password { get; set; } = string.Empty;
|
||||
public string FirstName { get; set; } = string.Empty;
|
||||
public string LastName { get; set; } = string.Empty;
|
||||
public string? MiddleName {get; set; }
|
||||
|
||||
public static BankOperator? Create(BankOperatorBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new BankOperator()
|
||||
{
|
||||
Id = model.Id,
|
||||
Login = model.Login,
|
||||
Password = model.Password,
|
||||
LastName = model.LastName,
|
||||
FirstName = model.FirstName,
|
||||
MiddleName = model.MiddleName,
|
||||
};
|
||||
}
|
||||
public static BankOperator? Create(BankOperatorViewModel model)
|
||||
{
|
||||
return new BankOperator()
|
||||
{
|
||||
Id = model.Id,
|
||||
Login = model.Login,
|
||||
Password = model.Password,
|
||||
LastName = model.LastName,
|
||||
FirstName = model.FirstName,
|
||||
MiddleName = model.MiddleName,
|
||||
};
|
||||
}
|
||||
public void Update(BankOperatorBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Password = model.Password;
|
||||
Login = model.Login;
|
||||
LastName = model.LastName;
|
||||
FirstName = model.FirstName;
|
||||
MiddleName = model.MiddleName;
|
||||
}
|
||||
public BankOperatorViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Login = Login,
|
||||
Password = Password,
|
||||
LastName = LastName,
|
||||
FirstName = FirstName,
|
||||
MiddleName = MiddleName,
|
||||
};
|
||||
|
||||
}
|
||||
}
|
96
Bank/BankDatabaseImplement/Models/CreditProgram.cs
Normal file
96
Bank/BankDatabaseImplement/Models/CreditProgram.cs
Normal file
@ -0,0 +1,96 @@
|
||||
using BankContracts.BindingModels;
|
||||
using BankContracts.ViewModels;
|
||||
using BankDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BankDatabaseImplement.Models
|
||||
{
|
||||
public class CreditProgram : ICreditProgramModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public float Percent { get; set; }
|
||||
[Required]
|
||||
public string Name { get; set; }
|
||||
|
||||
private Dictionary<int, ICurrencyModel>? _creditProgramCurrencies = null;
|
||||
[NotMapped]
|
||||
public Dictionary<int, ICurrencyModel> CreditProgramCurrencies
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_creditProgramCurrencies == null)
|
||||
{
|
||||
_creditProgramCurrencies = Currencies.ToDictionary(recDP => recDP.CurrencyId, recDP => recDP.Currency as ICurrencyModel);
|
||||
}
|
||||
return _creditProgramCurrencies;
|
||||
}
|
||||
}
|
||||
[ForeignKey("CreditProgramId")]
|
||||
public virtual List<CreditProgramCurrency> Currencies { get; set; } = new();
|
||||
|
||||
public static CreditProgram? Create(BankDatabase context, CreditProgramBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new CreditProgram()
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name,
|
||||
Percent = model.Percent,
|
||||
Currencies = model.CreditProgramCurrencies.Select(x => new CreditProgramCurrency
|
||||
{
|
||||
Currency = context.Currencies.First(y => y.Id == x.Key)
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
public void Update(CreditProgramBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Name = model.Name;
|
||||
Percent = model.Percent;
|
||||
}
|
||||
public CreditProgramViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
Percent = Percent,
|
||||
CreditProgramCurrencies = CreditProgramCurrencies,
|
||||
};
|
||||
public void UpdateCurrencies(BankDatabase context, CreditProgramBindingModel model)
|
||||
{
|
||||
var creditProgramsCurrencies = context.CreditProgramCurrencies.Where(rec => rec.CreditProgramId == model.Id).ToList();
|
||||
if (creditProgramsCurrencies != null && creditProgramsCurrencies.Count > 0)
|
||||
{
|
||||
context.CreditProgramCurrencies.RemoveRange(creditProgramsCurrencies.Where(rec => !model.CreditProgramCurrencies.ContainsKey(rec.DealId)));
|
||||
context.SaveChanges();
|
||||
var creditProgram = context.CreditPrograms.First(x => x.Id == Id);
|
||||
foreach (var updateCurrency in creditProgramsCurrencies)
|
||||
{
|
||||
model.CreditProgramCurrencies.Remove(updateCurrency.CurrencyId);
|
||||
}
|
||||
foreach (var dp in model.CreditProgramCurrencies)
|
||||
{
|
||||
context.CreditProgramCurrencies.Add(new CreditProgramCurrency
|
||||
{
|
||||
CreditProgram = creditProgram,
|
||||
Currency = context.Currencies.First(x => x.Id == dp.Key),
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_creditProgramCurrencies = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
20
Bank/BankDatabaseImplement/Models/CreditProgramCurrency.cs
Normal file
20
Bank/BankDatabaseImplement/Models/CreditProgramCurrency.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BankDatabaseImplement.Models
|
||||
{
|
||||
public class CreditProgramCurrency
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public int CurrencyId { get; set; }
|
||||
[Required]
|
||||
public int CreditProgramId { get; set; }
|
||||
public virtual Currency Currency { get; set; } = new();
|
||||
public virtual CreditProgram CreditProgram { get; set; } = new();
|
||||
}
|
||||
}
|
54
Bank/BankDatabaseImplement/Models/Currency.cs
Normal file
54
Bank/BankDatabaseImplement/Models/Currency.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using BankDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using BankContracts.BindingModels;
|
||||
using BankContracts.ViewModels;
|
||||
|
||||
namespace BankDatabaseImplement.Models
|
||||
{
|
||||
public class Currency : ICurrencyModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public string Name { get; set; }
|
||||
public static Currency? Create(BankDatabase context, CurrencyBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Currency()
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name,
|
||||
};
|
||||
}
|
||||
public static Currency? Create(BankDatabase context, CurrencyViewModel model)
|
||||
{
|
||||
return new Currency()
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name,
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(CurrencyBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Name = model.Name;
|
||||
}
|
||||
public CurrencyViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
};
|
||||
}
|
||||
}
|
76
Bank/BankDatabaseImplement/Models/CurrencyPurchase.cs
Normal file
76
Bank/BankDatabaseImplement/Models/CurrencyPurchase.cs
Normal file
@ -0,0 +1,76 @@
|
||||
using BankContracts.BindingModels;
|
||||
using BankContracts.ViewModels;
|
||||
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 CurrencyPurchase : ICurrencyPurchaseModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public float Amount { get; set; }
|
||||
[Required]
|
||||
public DateTime PurchaseDate { get; set; } = DateTime.Now;
|
||||
[Required]
|
||||
public int BankOperatorId { get; set; }
|
||||
public virtual BankOperator BankOperator { get; set; } = new();
|
||||
public int CurrencyId { get; set; }
|
||||
public virtual Currency Currency { get; set; } = new();
|
||||
|
||||
public static CurrencyPurchase? Create(BankDatabase context, CurrencyPurchaseBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new CurrencyPurchase()
|
||||
{
|
||||
Id = model.Id,
|
||||
Amount = model.Amount,
|
||||
PurchaseDate = model.PurchaseDate,
|
||||
BankOperatorId = model.BankOperatorId,
|
||||
BankOperator = context.BankOperators.First(x => x.Id == model.BankOperatorId),
|
||||
CurrencyId = model.CurrencyId,
|
||||
Currency = context.Currencies.First(x => x.Id == model.CurrencyId),
|
||||
};
|
||||
}
|
||||
public static CurrencyPurchase? Create(BankDatabase context, CurrencyPurchaseViewModel model)
|
||||
{
|
||||
return new CurrencyPurchase()
|
||||
{
|
||||
Id = model.Id,
|
||||
Amount = model.Amount,
|
||||
PurchaseDate = model.PurchaseDate,
|
||||
BankOperatorId = model.BankOperatorId,
|
||||
BankOperator = context.BankOperators.First(x => x.Id == model.BankOperatorId),
|
||||
CurrencyId = model.CurrencyId,
|
||||
Currency = context.Currencies.First(x => x.Id == model.CurrencyId),
|
||||
};
|
||||
}
|
||||
public void Update(CurrencyPurchaseBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
PurchaseDate = model.PurchaseDate;
|
||||
}
|
||||
public CurrencyPurchaseViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Amount = Amount,
|
||||
PurchaseDate = PurchaseDate,
|
||||
BankOperatorId = BankOperatorId,
|
||||
BankOperatorName = BankOperator.LastName + " " + BankOperator.FirstName + " " + BankOperator.MiddleName,
|
||||
CurrencyId = CurrencyId,
|
||||
CurrencyName = Currency.Name,
|
||||
};
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user