SUBD_PIbd-23_ZakharovRA/CarShowroom/CarShowroomDatabaseStorage/Entities/Sale.cs
2024-05-05 22:02:53 +04:00

92 lines
2.6 KiB
C#

using CarShowroomContracts.AbstractModels;
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<SaleCar> SaleCars { get; set; } = new();
[NotMapped]
public List<int> CarIds
{
get
{
return SaleCars.Select(c => c.CarId).ToList();
}
}
public virtual List<SaleService> SaleServices { get; set; } = new();
[NotMapped]
public List<int> ServiceIds
{
get
{
return SaleServices.Select(s => s.ServiceId).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 = SaleServices.Select(s => s.Service.GetServiceView()).ToList();
sale.Cars = SaleCars.Select(c => c.Car.GetCarView()).ToList();
return sale;
}
}
}