57 lines
1.6 KiB
C#
57 lines
1.6 KiB
C#
using CarServiceContracts.BindingModels;
|
||
using CarServiceContracts.Models;
|
||
using CarServiceContracts.ViewModels;
|
||
using System.ComponentModel.DataAnnotations;
|
||
|
||
namespace CarServiceDatabase.Models
|
||
{
|
||
public class ItemForRepair : IItemForRepairModel
|
||
{
|
||
public int Id { get; private set; }
|
||
[Required]
|
||
public int Count { get; private set; }
|
||
[Required]
|
||
public int ItemId { get; private set; }
|
||
[Required]
|
||
public int RepairRequestId { get; private set; }
|
||
public virtual Item Item { get; set; } = new();
|
||
public virtual RepairRequest RepairRequest { get; set; } = new();
|
||
public static ItemForRepair? Create(CarServiceDbContext context, ItemForRepairBindingModel? model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
return null;
|
||
}
|
||
var currItem = context.Items.First(x => x.Id == model.ItemId);
|
||
if (currItem.Count < model.Count)
|
||
{
|
||
throw new ArgumentException("На складе нет столько запчастей");
|
||
}
|
||
return new()
|
||
{
|
||
Count = model.Count,
|
||
Item = context.Items.First(x => x.Id == model.ItemId),
|
||
RepairRequest = context.RepairRequests.First(x => x.Id == model.RepairRequestId)
|
||
};
|
||
}
|
||
public void Update(CarServiceDbContext context, ItemForRepairBindingModel? model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
return;
|
||
}
|
||
Count = model.Count;
|
||
Item = context.Items.First(x => x.Id == model.ItemId);
|
||
RepairRequest = context.RepairRequests.First(x => x.Id == model.RepairRequestId);
|
||
}
|
||
public ItemForRepairViewModel GetViewModel => new()
|
||
{
|
||
Id = Id,
|
||
Count = Count,
|
||
ItemId = ItemId,
|
||
RepairRequestId = RepairRequestId,
|
||
ItemName = Item.Name
|
||
};
|
||
}
|
||
}
|