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

67 lines
1.8 KiB
C#
Raw Normal View History

2024-05-03 11:28:27 +04:00
using CarShowroomContracts.AbstractModels;
2024-05-05 22:02:53 +04:00
using CarShowroomDataModels.Views;
2024-05-05 19:18:53 +04:00
using Microsoft.EntityFrameworkCore;
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("model")]
[Index(nameof(Name), IsUnique = true)]
2024-05-03 11:28:27 +04:00
public class Model : IModel
{
2024-05-05 19:18:53 +04:00
[Column("model_id")]
public int Id { get; private set; }
[Required]
[Column("model_name")]
[MaxLength(50)]
public string Name { get; private set; } = string.Empty;
[Required]
[Column("model_price")]
public int Price { get; private set; }
[Required]
[Column("model_make_id")]
public int MakeId { get; private set; }
public virtual Make? Make { get; set; }
public virtual List<Car> Cars { get; set; } = new();
private Model() { }
private Model(IModel model)
2024-05-03 11:28:27 +04:00
{
Id = model.Id;
Name = model.Name;
Price = model.Price;
MakeId = model.MakeId;
}
2024-05-05 19:18:53 +04:00
public static Model? Create(IModel model)
{
if (model == null)
return null;
return new Model(model);
}
public void Update(IModel model)
{
if (model == null)
return;
Name = model.Name;
Price = model.Price;
MakeId = model.MakeId;
}
2024-05-05 22:58:29 +04:00
public ModelView GetView()
2024-05-05 19:18:53 +04:00
{
ModelView model = new ModelView(this);
model.MakeName = Make?.Name ?? string.Empty;
return model;
}
2024-05-03 11:28:27 +04:00
}
}