SUBD_PIbd-23_ZakharovRA/CarShowroom/CarShowroomDatabaseStorage/Entities/Sale.cs

95 lines
2.7 KiB
C#

using CarShowroomContracts.AbstractModels;
using CarShowroomContracts.Views;
using CarShowroomDataModels.Views;
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 CarShowroomDatabaseStorage.Entities
{
[Table("sale")]
public class Sale : ISale
{
[Column("sale_id")]
public int Id { get; private set; }
[Required]
[Column("sale_time")]
public DateTime SaleTime { get; private set; }
[Required]
[Column("sale_cost")]
public int Cost { get; private set; }
[Column("sale_client_id")]
public int? ClientId { get; private set; }
public virtual Client? Client { get; set; }
[Column("sale_employee_id")]
public int? EmployeeId { get; private set; }
public virtual Employee? Employee { get; set; }
public virtual List<Car> Cars { get; set; } = new();
public virtual List<SaleCar> SaleCars { get; set; } = new();
[NotMapped]
public List<int> CarIds
{
get
{
return Cars.Select(c => c.Id).ToList();
}
}
public virtual List<Service> Services { get; set; } = new();
public virtual List<SaleService> SaleServices { get; set; } = new();
[NotMapped]
public List<int> ServiceIds
{
get
{
return Services.Select(s => s.Id).ToList();
}
}
private Sale() { }
private Sale(ISale sale)
{
Id = sale.Id;
SaleTime = sale.SaleTime;
Cost = sale.Cost;
ClientId = sale.ClientId;
EmployeeId = sale.EmployeeId;
}
public static Sale? Create(ISale sale)
{
if (sale == null)
{
return null;
}
return new Sale(sale);
}
public void Update(ISale sale)
{
if (sale == null)
{
return;
}
SaleTime = sale.SaleTime;
Cost = sale.Cost;
ClientId = sale.ClientId;
EmployeeId = sale.EmployeeId;
}
public SaleView GetSaleView()
{
SaleView sale = new SaleView(this);
sale.ClientName = Client?.Name ?? string.Empty;
sale.EmployeeName = Employee?.Name ?? string.Empty;
sale.Services = Services.Select(s => s.GetServiceView()).ToList();
sale.Cars = Cars.Select(c => c.GetCarView()).ToList();
return sale;
}
}
}