CourseWork_BankYouBankrupt/BankYouBankrupt/BankYouBankruptDatabaseImplement/Models/Card.cs
2023-05-20 00:25:24 +04:00

78 lines
2.1 KiB
C#

using BankYouBankruptContracts.BindingModels;
using BankYouBankruptContracts.ViewModels.Client.Default;
using BankYouBankruptDataModels.Models;
using Microsoft.EntityFrameworkCore;
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 BankYouBankruptDatabaseImplement.Models
{
public class Card : ICardModel
{
public int Id { get; set; }
[Required]
public int ClientID { get; set; }
public virtual Client Client { get; set; }
[Required]
public int AccountId { get; set; }
public virtual Account Account { get; set; }
[Required]
public string Number { get; set; } = string.Empty;
[Required]
public string CVC { get; set; } = string.Empty;
[Required]
public DateTime Period { get; set; } = DateTime.Now;
[ForeignKey("CardId")]
public virtual List<Crediting> Creditings { get; set; } = new();
[ForeignKey("CardId")]
public virtual List<Debiting> Debitings { get; set; } = new();
public CardViewModel GetViewModel => new()
{
Id = Id,
AccountId = AccountId,
ClientID = ClientID,
ClientSurname = Client.Surname,
Number = Number,
Sum = Account.Balance,
Period = Period,
CVC = CVC
};
public static Card Create(BankYouBancruptDatabase context, CardBindingModel model)
{
return new Card()
{
Id = model.Id,
AccountId = model.AccountId,
Account = context.Accounts.First(x => x.Id == model.AccountId),
ClientID = model.ClientID,
Client = context.Clients.First(x => x.Id == model.ClientID),
Number = model.Number,
Period = model.Period,
CVC = model.CVC
};
}
public void Update(CardBindingModel model)
{
Period = model.Period;
}
}
}