CourseWork_Bank/Bank/BankDatabaseImplement/Models/Transfer.cs

72 lines
2.3 KiB
C#

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 Transfer : ITransferModel
{
public int Id {get; set;}
[Required]
public float Amount { get; set; }
[Required]
public DateTime TransferDateTime { get; set; } = DateTime.Now;
[Required]
public int PaymentId { get; set; }
public virtual Payment Payment { get; set; } = new();
public int OperatorId { get; set; }
public virtual Operator Operator { get; set; } = new();
public static Transfer? Create(BankDatabase context, TransferBindingModel model)
{
if (model == null)
{
return null;
}
return new Transfer()
{
Id = model.Id,
Amount = model.Amount,
TransferDateTime = model.TransferDateTime,
PaymentId = model.PaymentId,
Payment = context.Payments.First(x => x.Id == model.PaymentId),
OperatorId = model.OperatorId,
Operator = context.Operators.First(x => x.Id == model.OperatorId)
};
}
public static Transfer? Create(BankDatabase context, TransferViewModel model)
{
return new Transfer()
{
Id = model.Id,
Amount = model.Amount,
TransferDateTime = model.TransferDateTime,
PaymentId = model.PaymentId,
Payment = context.Payments.First(x => x.Id == model.PaymentId)
};
}
public void Update(TransferBindingModel? model)
{
if (model == null)
{
return;
}
Amount = model.Amount;
}
public TransferViewModel GetViewModel => new()
{
Id = Id,
TransferDateTime = TransferDateTime,
Amount = Amount,
PaymentId = PaymentId,
OperatorId = OperatorId,
OperatorName = Operator.LastName + " " + Operator.FirstName + " " + Operator.MiddleName,
};
}
}