PIbd-22_Chernyshev_Shabunov.../ComputerShopDatabaseImplement/Models/Order.cs
2024-05-01 17:09:02 +04:00

82 lines
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using ComputerShopContracts.BindingModels;
using ComputerShopContracts.ViewModels;
using ComputerShopDataModels.Enums;
using ComputerShopDataModels.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
[Required]
public int UserId { get; private set; }
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
[Required]
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
[Required]
public double Sum { get; private set; }
[ForeignKey("OrderId")]
public virtual List<RequestOrder> Requests { get; set; } = new();
[ForeignKey("OrderId")]
public virtual List<ShipmentOrder> Shipments { get; set; } = new();
public static Order? Create(OrderBindingModel model)
{
if (model == null)
{
return null;
}
//при создании заказа с ним ничего ещё не связано => сумма = 0
return new Order()
{
Id = model.Id,
UserId = model.UserId,
DateCreate = model.DateCreate,
Status = model.Status,
Sum = 0
};
}
public void Update(OrderBindingModel model)
{
if (model == null)
{
return;
}
//DateCreate не обновляю, потому что странно менять дату оформления заказа после его создания
Status = model.Status;
}
//отдельный метод изменения стоимости заказа (+ или -) на стоимость сборки внутри заявки (после действий с заявками, кот. привязаны к заказу)
public void ChangeSum(double price_of_assembly, bool justUpdate = false)
{
Sum += price_of_assembly;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
UserId = UserId,
DateCreate = DateCreate,
Status = Status,
Sum = Sum
};
}
}