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

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 Price { 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;
Price = sale.Price;
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;
Price = sale.Price;
ClientId = sale.ClientId;
EmployeeId = sale.EmployeeId;
}
public SaleView GetView()
{
SaleView sale = new SaleView(this);
sale.ClientName = Client?.Name ?? string.Empty;
sale.EmployeeName = Employee?.Name ?? string.Empty;
sale.Services = SaleServices.Select(s => s.Service.GetView()).ToList();
sale.Cars = SaleCars.Select(c => c.Car.GetView()).ToList();
return sale;
}
}
}