PIbd-23_Zargarov_M.A._Cours.../CarCenter/CarCenterDatabaseImplement/Models/Sale.cs

84 lines
3.0 KiB
C#

using CarCenterContracts.BindingModels;
using CarCenterContracts.ViewModels;
using CarCenterDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarCenterDatabaseImplement.Models
{
public class Sale : ISaleModel
{
public int Id {get; set;}
[Required]
public float Sum { get; set; }
[Required]
public DateTime SaleDateTime { get; set; } = DateTime.Now;
[Required]
public int CarId { get; set; }
public virtual Car Car { get; set; } = new();
public int ReceiptId { get; set; }
public virtual Receipt Receipt { get; set; } = new();
public int ConfigurationId { get; set; }
public virtual Configuration Configuration { get; set; } = new();
public int EmployeeId { get; set; }
public virtual Employee Employee { get; set; } = new();
public static Sale? Create(CarCenterDatabase context, SaleBindingModel model)
{
if (model == null)
{
return null;
}
return new Sale()
{
Id = model.Id,
Sum = model.Sum,
SaleDateTime = model.SaleDateTime,
CarId = model.CarId,
Car = context.Cars.First(x => x.Id == model.CarId),
ReceiptId = model.ReceiptId,
Receipt = context.Receipts.First(x => x.Id == model.ReceiptId),
ConfigurationId = model.ConfigurationId,
Configuration = context.Configurations.First(x => x.Id == model.ConfigurationId),
EmployeeId = model.EmployeeId,
Employee = context.Employees.First(x => x.Id == model.EmployeeId)
};
}
public static Sale? Create(CarCenterDatabase context, SaleViewModel model)
{
return new Sale()
{
Id = model.Id,
Sum = model.Sum,
SaleDateTime = model.SaleDateTime,
CarId = model.CarId,
Car = context.Cars.First(x => x.Id == model.CarId),
ReceiptId = model.ReceiptId,
Receipt = context.Receipts.First(x => x.Id == model.ReceiptId),
ConfigurationId = model.ConfigurationId,
Configuration = context.Configurations.First(x => x.Id == model.ConfigurationId)
};
}
public void Update(SaleBindingModel? model)
{
if (model == null)
{
return;
}
Sum = model.Sum;
}
public SaleViewModel GetViewModel => new()
{
Id = Id,
SaleDateTime = SaleDateTime,
Sum = Sum,
CarId = CarId,
EmployeeId = EmployeeId,
EmployeeName = Employee.Surname + " " + Employee.Name + " " + Employee.Patronymic,
};
}
}