Coursework_ComputerStore_Li.../ComputerStoreDatabaseImplement/Models/Request.cs

80 lines
2.1 KiB
C#
Raw Normal View History

2023-04-07 20:46:13 +04:00
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2023-04-09 01:07:38 +04:00
using Azure.Core;
using ComputerStoreContracts.SearchModels;
using Microsoft.EntityFrameworkCore;
using System.Xml.Linq;
2023-04-07 20:46:13 +04:00
namespace ComputerStoreDatabaseImplement.Models
{
public class Request : IRequestModel
{
public int ID { get; private set; }
[Required]
public int OrderID { get; private set; }
2023-04-09 01:07:38 +04:00
public virtual Order Order { get; set; }
public int? PCID { get; private set; }
2023-04-07 20:46:13 +04:00
[Required]
public double Price { get; private set; }
[ForeignKey("RequestID")]
2023-04-09 01:07:38 +04:00
public virtual List<RequestComponent> PCs { get; set; } = new();
2023-04-07 20:46:13 +04:00
2023-04-09 01:07:38 +04:00
public static Request? Create(ComputerStoreDatabase context, RequestBindingModel model)
2023-04-07 20:46:13 +04:00
{
if (model == null)
{
return null;
}
return new Request()
{
ID = model.ID,
OrderID = model.OrderID,
Price = model.Price
};
}
2023-04-09 01:07:38 +04:00
public void Update(RequestBindingModel model)
2023-04-07 20:46:13 +04:00
{
if (model == null)
{
return;
}
Price = model.Price;
2023-04-09 01:07:38 +04:00
PCID = model.PCID;
}
public void UpdatePCID(ComputerStoreDatabase context)
{
PCID = context.RequestComponents.First(x => x.RequestID == ID).PCID;
2023-04-07 20:46:13 +04:00
}
2023-04-09 01:07:38 +04:00
public RequestViewModel GetViewModel
2023-04-07 20:46:13 +04:00
{
2023-04-09 01:07:38 +04:00
get
{
var context = new ComputerStoreDatabase();
return new RequestViewModel()
{
ID = ID,
OrderID = OrderID,
Price = Price,
PCID = PCID,
PCName = context.PCs.First(rec => rec.ID == PCID).Name
};
}
}
2023-04-07 20:46:13 +04:00
}
}