PIAPS_CW/DatabaseImplement/Models/CartItem.cs

84 lines
2.5 KiB
C#
Raw Normal View History

using Contracts.BindingModels;
using Contracts.ViewModels;
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
using System.Xml.Linq;
using System.ComponentModel.DataAnnotations.Schema;
namespace DatabaseImplement.Models
{
public class CartItem : ICartItem
{
public Guid Id { get; set; }
[Required]
public Guid UserId { get; set; }
[Required]
public int Count { get; set; }
public Guid? PurchaseId { get; set; }
[Required]
public DateTime DateCreated { get; set; }
[Required]
public Guid ProductId { get; set; }
[Required]
public string ProductName { get; set; } = string.Empty;
public virtual Product? Product { get; set; }
public virtual Purchase? Purchase { get; set; }
public static CartItem Create(Database context, CartItemBindingModel model)
{
return new CartItem()
{
Id = model.Id,
UserId = model.UserId,
Count = model.Count,
DateCreated = model.DateCreated,
ProductId = model.ProductId,
ProductName = model.ProductName,
PurchaseId = model.PurchaseId,
};
}
public void Update(CartItemBindingModel model)
{
if (model is null)
{
throw new ArgumentNullException("Update CartItem: binding model is null");
}
Count = model.Count;
PurchaseId = model.PurchaseId;
}
public CartItemViewModel GetViewModel
{
get
{
var context = new Database();
return new()
{
Id = Id,
UserId = UserId,
DateCreated = DateCreated,
ProductId = ProductId,
ProductName = ProductName,
Count = Count,
PurchaseId = PurchaseId,
};
}
}
public CartItemBindingModel GetBindingModel() => new()
{
Id = Id,
UserId = UserId,
DateCreated = DateCreated,
ProductId = ProductId,
ProductName = ProductName,
Count = Count,
PurchaseId = PurchaseId,
};
}
}