SUBD/CarRentDatabase/Models/Car.cs

84 lines
2.0 KiB
C#

using CarRentContracts.BindingModels;
using CarRentContracts.ViewModels;
using CarRentDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace CarRentDatabase.Models
{
public class Car : ICarModel
{
[Required]
public string Brand { get; set; } = string.Empty;
[Required]
public string Model { get; set; } = string.Empty;
[Required]
public DateTime YearOfManuf { get; set; } = DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc);
[Required]
public string Color { get; set; } = string.Empty;
[Required]
public string LicensePlate { get; set; } = string.Empty;
[Required]
public double RentalCostPerHour { get; set; }
[Required]
public int BranchId { get; set; }
public int Id { get; private set; }
[ForeignKey("CarId")]
List<Rental> RentalCars { get; set; } = new();
public virtual Branch Branch { get; set; }
public static Car? Create(CarBindingModel model)
{
if (model == null)
{
return null;
}
return new Car()
{
Id = model.Id,
Brand = model.Brand,
Model = model.Model,
YearOfManuf = model.YearOfManuf,
Color = model.Color,
LicensePlate = model.LicensePlate,
RentalCostPerHour = model.RentalCostPerHour,
BranchId = model.BranchId,
};
}
public void Update(CarBindingModel model)
{
if (model == null)
{
return;
}
Brand = model.Brand;
Model = model.Model;
YearOfManuf = model.YearOfManuf;
Color = model.Color;
LicensePlate = model.LicensePlate;
RentalCostPerHour = model.RentalCostPerHour;
}
public CarViewModel GetViewModel => new()
{
Id = Id,
Brand = Brand,
Model = Model,
YearOfManuf = YearOfManuf,
Color = Color,
LicensePlate = LicensePlate,
RentalCostPerHour = RentalCostPerHour,
BranchId = BranchId,
BranchName = Branch == null ? string.Empty : Branch.Name,
};
}
}