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

92 lines
2.6 KiB
C#
Raw Normal View History

2024-05-03 11:28:27 +04:00
using CarShowroomContracts.AbstractModels;
2024-05-05 19:18:53 +04:00
using CarShowroomDataModels.Views;
2024-05-03 11:28:27 +04:00
using System;
using System.Collections.Generic;
2024-05-05 19:18:53 +04:00
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
2024-05-03 11:28:27 +04:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarShowroomDatabaseStorage.Entities
{
2024-05-05 19:18:53 +04:00
[Table("sale")]
2024-05-03 11:28:27 +04:00
public class Sale : ISale
{
2024-05-05 19:18:53 +04:00
[Column("sale_id")]
public int Id { get; private set; }
[Required]
[Column("sale_time")]
public DateTime SaleTime { get; private set; }
[Required]
[Column("sale_cost")]
2024-05-13 15:21:18 +04:00
public int Price { get; private set; }
2024-05-05 19:18:53 +04:00
[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
{
2024-05-05 21:21:25 +04:00
return SaleCars.Select(c => c.CarId).ToList();
2024-05-05 19:18:53 +04:00
}
}
public virtual List<SaleService> SaleServices { get; set; } = new();
[NotMapped]
public List<int> ServiceIds
{
get
{
2024-05-05 21:21:25 +04:00
return SaleServices.Select(s => s.ServiceId).ToList();
2024-05-05 19:18:53 +04:00
}
}
private Sale() { }
private Sale(ISale sale)
2024-05-03 11:28:27 +04:00
{
Id = sale.Id;
SaleTime = sale.SaleTime;
2024-05-13 15:21:18 +04:00
Price = sale.Price;
2024-05-03 11:28:27 +04:00
ClientId = sale.ClientId;
EmployeeId = sale.EmployeeId;
2024-05-05 19:18:53 +04:00
}
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;
2024-05-13 15:21:18 +04:00
Price = sale.Price;
2024-05-05 19:18:53 +04:00
ClientId = sale.ClientId;
EmployeeId = sale.EmployeeId;
}
2024-05-05 22:58:29 +04:00
public SaleView GetView()
2024-05-05 19:18:53 +04:00
{
SaleView sale = new SaleView(this);
sale.ClientName = Client?.Name ?? string.Empty;
sale.EmployeeName = Employee?.Name ?? string.Empty;
2024-05-05 22:58:29 +04:00
sale.Services = SaleServices.Select(s => s.Service.GetView()).ToList();
sale.Cars = SaleCars.Select(c => c.Car.GetView()).ToList();
2024-05-05 19:18:53 +04:00
return sale;
2024-05-03 11:28:27 +04:00
}
}
}