Contractor: Fixes

This commit is contained in:
Safgerd 2023-04-07 20:46:13 +04:00
parent c2baea1737
commit fbf7292ee2
22 changed files with 457 additions and 31 deletions

View File

@ -58,12 +58,13 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
OrderBindingModel model = new OrderBindingModel
{
ID = viewModel.ID,
ConsignmentID = viewModel.ConsignmentID,
RequestID = viewModel.RequestID,
Status = viewModel.Status,
DateCreate = viewModel.DateCreate,
DateImplement = viewModel.DateImplement,
Price = viewModel.Price
Price = viewModel.Price,
SellerID = viewModel.SellerID,
OrderConsignments = viewModel.OrderConsignments,
OrderRequests = viewModel.OrderRequests
};
CheckModel(model, false);
@ -121,19 +122,6 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
{
return;
}
if (string.IsNullOrEmpty(model.RequestID.ToString()) && string.IsNullOrEmpty(model.ConsignmentID.ToString()))
{
throw new ArgumentNullException("Incorrect consignment or request ID", nameof(model.ConsignmentID));
}
if (string.IsNullOrEmpty(model.RequestID.ToString()) && model.ConsignmentID < 0)
{
throw new ArgumentNullException("Incorrect consignment ID", nameof(model.ConsignmentID));
}
if (string.IsNullOrEmpty(model.ConsignmentID.ToString()) && model.RequestID < 0)
{
throw new ArgumentNullException("Incorrect request ID", nameof(model.RequestID));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Incorrect Price", nameof(model.Price));
@ -142,7 +130,7 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
{
throw new ArgumentNullException("Incorrect seller ID", nameof(model.SellerID));
}
_logger.LogInformation("Order. OrderID:{ ID}.RequestID:{ RequestID}. ConsignmentID: { ConsignmentID}. SellerID: { SellerID}", model.ID, model.RequestID, model.ConsignmentID, model.SellerID);
_logger.LogInformation("Order. OrderID:{ ID}. SellerID: { SellerID}", model.ID, model.SellerID);
}
}
}

View File

@ -98,8 +98,9 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
if (!withParams) { return; }
if (model.Price <= 0) { throw new ArgumentNullException("Invalid request's price", nameof(model)); }
if (string.IsNullOrEmpty(model.OrderID.ToString())) { throw new ArgumentNullException("Invalid Request's order ID", nameof(model)); }
if (string.IsNullOrEmpty(model.PCID.ToString())) { throw new ArgumentNullException("Invalid Request's PC ID", nameof(model)); }
_logger.LogInformation("Request. Request ID:{ ID}. Order ID: { OrderID}", model.ID, model.OrderID);
_logger.LogInformation("Request. Request ID:{ ID}. Order ID: { OrderID}. PC ID: { PCID}", model.ID, model.OrderID, model.PCID);
}
}
}

View File

@ -12,5 +12,6 @@ namespace ComputerStoreContracts.BindingModels
public int ID { get; set; }
public int OrderID { get; set; }
public double Price { get; set; }
public Dictionary<int, (IProductModel, int)> ConsignmentProducts { get; set; } = new();
}
}

View File

@ -12,8 +12,8 @@ namespace ComputerStoreContracts.BindingModels
{
public int ID { get; set; }
public double Price { get; set; }
public int? ConsignmentID { get; set; }
public int? RequestID { get; set; }
public List<IConsignmentModel> OrderConsignments { get; set; } = new();
public List<IRequestModel> OrderRequests { get; set; } = new();
public OrderStatus Status { get; set; } = OrderStatus.Unknown;
public DateTime DateCreate { get; set; } = DateTime.Now;
public DateTime? DateImplement { get; set; }

View File

@ -12,5 +12,6 @@ namespace ComputerStoreContracts.BindingModels
public int ID { get; set; }
public int OrderID { get; set; }
public double Price { get; set; }
public int PCID { get; set; }
}
}

View File

@ -12,9 +12,7 @@ namespace ComputerStoreContracts.BusinessLogicContracts
public interface IRequestLogic
{
List<RequestViewModel>? ReadList(RequestSearchModel? model);
RequestViewModel? ReadElement(RequestSearchModel model);
bool Create(RequestBindingModel model);
bool Update(RequestBindingModel model);
bool Delete(RequestBindingModel model);

View File

@ -1,4 +1,5 @@
using System;
using ComputerStoreDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -10,5 +11,6 @@ namespace ComputerStoreContracts.SearchModels
{
public int? ID { get; set; }
public int? OrderID { get; set; }
public int? ProductID { get; set; }
}
}

View File

@ -10,5 +10,6 @@ namespace ComputerStoreContracts.SearchModels
{
public int? ID { get; set; }
public int? OrderID { get; set; }
public int? PCID { get; set; }
}
}

View File

@ -16,5 +16,7 @@ namespace ComputerStoreContracts.ViewModels
public int OrderID { get; set; }
[DisplayName("Price")]
public double Price { get; set; }
public Dictionary<int, (IProductModel, int)> ConsignmentProducts { get; set; } = new();
}
}

View File

@ -15,17 +15,18 @@ namespace ComputerStoreContracts.ViewModels
public int ID { get; set; }
[DisplayName("Price")]
public double Price { get; set; }
[DisplayName("Consignment ID")]
public int? ConsignmentID { get; set; }
[DisplayName("Request ID")]
public int? RequestID { get; set; }
[DisplayName("Status")]
public OrderStatus Status { get; set; }
[DisplayName("Creation date")]
[DisplayName("Creation date")]
public DateTime DateCreate { get; set; }
[DisplayName("Implementation date")]
public DateTime? DateImplement { get; set; }
[DisplayName("Seller ID")]
public int SellerID { get; set; }
[DisplayName("SellerUsername")]
public string? SellerUsername { get; set; } = string.Empty;
public List<IConsignmentModel> OrderConsignments { get; set; } = new();
public List<IRequestModel> OrderRequests { get; set; } = new();
}
}

View File

@ -16,5 +16,8 @@ namespace ComputerStoreContracts.ViewModels
public int OrderID { get; set; }
[DisplayName("Price")]
public double Price { get; set; }
[DisplayName("PC ID")]
public int PCID { get; set; }
}
}

View File

@ -10,5 +10,6 @@ namespace ComputerStoreDataModels.Models
{
int OrderID { get; }
double Price { get; }
Dictionary<int, (IProductModel, int)> ConsignmentProducts { get; }
}
}

View File

@ -10,8 +10,8 @@ namespace ComputerStoreDataModels.Models
public interface IOrderModel : IID
{
double Price { get; }
int? ConsignmentID { get; }
int? RequestID { get; }
List<IConsignmentModel> OrderConsignments { get; }
List<IRequestModel> OrderRequests { get; }
OrderStatus Status { get; }
DateTime DateCreate { get; }
DateTime? DateImplement { get; }

View File

@ -9,6 +9,7 @@ namespace ComputerStoreDataModels.Models
public interface IRequestModel : IID
{
int OrderID { get; }
int PCID { get; }
double Price { get; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreDatabaseImplement.Implements
{
public class ConsignmentStorage //: IConsignmentStorage
{
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreDatabaseImplement.Implements
{
public class OrderStorage //: IOrderStorage
{
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreDatabaseImplement.Implements
{
public class RequestStorage //: IRequestStorage
{
}
}

View File

@ -0,0 +1,78 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.StorageContracts;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreDatabaseImplement.Implements
{
public class SellerStorage : ISellerStorage
{
public SellerViewModel? GetElement(SellerSearchModel model)
{
if (string.IsNullOrEmpty(model.Username) && !model.ID.HasValue)
{
return null;
}
using var context = new ComputerStoreDatabase();
return context.Sellers.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Username) && model.Username == x.Username) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel;
}
public List<SellerViewModel> GetFilteredList(SellerSearchModel model)
{
if (string.IsNullOrEmpty(model.Username))
{
return new();
}
using var context = new ComputerStoreDatabase();
return context.Sellers.Where(x => x.Username.Equals(model.Username)).Select(x => x.GetViewModel).ToList();
}
public List<SellerViewModel> GetFullList()
{
using var context = new ComputerStoreDatabase();
return context.Sellers.Select(x => x.GetViewModel).ToList();
}
public SellerViewModel? Insert(SellerBindingModel model)
{
using var context = new ComputerStoreDatabase();
var newSeller = Seller.Create(model);
if (newSeller == null) { return null; }
context.Sellers.Add(newSeller);
context.SaveChanges();
return newSeller.GetViewModel;
}
public SellerViewModel? Update(SellerBindingModel model)
{
using var context = new ComputerStoreDatabase();
var specSeller = context.Sellers.FirstOrDefault(x => x.ID == model.ID);
if (specSeller == null) { return null; }
specSeller.Update(model);
context.SaveChanges();
return specSeller.GetViewModel;
}
public SellerViewModel? Delete(SellerBindingModel model)
{
using var context = new ComputerStoreDatabase();
var specSeller = context.Sellers.FirstOrDefault(x => x.ID == model.ID);
if (specSeller == null) { return null; }
context.Sellers.Remove(specSeller);
context.SaveChanges();
return specSeller.GetViewModel;
}
}
}

View File

@ -0,0 +1,95 @@
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;
namespace ComputerStoreDatabaseImplement.Models
{
public class Consignment : IConsignmentModel
{
public int ID { get; private set; }
[Required]
public int OrderID { get; private set; }
[Required]
public double Price { get; private set; }
private Dictionary<int, (IProductModel, int)>? _consignmentProducts = null;
[NotMapped]
public Dictionary<int, (IProductModel, int)> ConsignmentProducts
{
get
{
if (_consignmentProducts == null)
{
_consignmentProducts = Products.ToDictionary(recPC => recPC.ProductID, recPC => (recPC.Product as IProductModel, recPC.Count));
}
return _consignmentProducts;
}
}
[ForeignKey("ConsignmentId")]
public virtual List<CarComponent> Components { get; set; } = new();
public virtual Product Product { get; private set; }
private Dictionary<int, (IProductModel, int)>? Products = null;
[ForeignKey("ConsignmentID")]
public virtual List<ConsignmentComponent> ConsignmentComponent { get; private set; } = new();
public static Consignment Create(ComputerStoreDatabase context, ConsignmentBindingModel model)
{
if (model == null)
{
return null;
}
return new Consignment()
{
ID = model.ID,
OrderID = model.OrderID,
Price = model.Price,
_orderProducts = model.ConsignmentProducts
};
//return new Consignment()
//{
// ID = model.ID,
// OrderID = model.OrderID,
// Price = model.Price,
// Products = model.ConsignmentProducts.Select(x => new ConsignmentComponent
// {
// Component = context.Components.First(y => y.ID == x.Key),
// Count = x.Value.Item2
// }).ToList()
//};
}
public void Update(ConsignmentBindingModel? model)
{
if (model == null)
{
return;
}
OrderID = model.OrderID;
Price = model.Price;
}
public ConsignmentViewModel GetViewModel => new()
{
ID = ID,
OrderID = OrderID,
Price = Price
};
}
}

View File

@ -0,0 +1,92 @@
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;
using ComputerStoreDataModels.Enums;
using System.Runtime.ConstrainedExecution;
namespace ComputerStoreDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int ID { get; private set; }
[Required]
public double Price { get; private set; }
public int? ConsignmentID { get; private set; }
public int? RequestID { get; private set; }
[Required]
public OrderStatus Status { get; private set; } = OrderStatus.Unknown;
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
[Required]
public int SellerID { get; private set; }
public virtual Seller Seller { get; set; }
public virtual Consignment Consignment { get; private set; }
public virtual Request Request { get; private set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order
{
Price = model.Price,
ConsignmentID = model.ConsignmentID,
RequestID = model.RequestID,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
SellerID = model.SellerID,
ID = model.ID,
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel
{
get
{
var context = new ComputerStoreDatabase();
return new()
{
ID = ID,
ConsignmentID = ConsignmentID,
RequestID = RequestID,
DateCreate = DateCreate,
DateImplement = DateImplement,
Status = Status,
Price = Price,
SellerID = SellerID,
SellerUsername = context.Sellers.FirstOrDefault(x => x.ID == SellerID)?.Username ?? string.Empty
};
}
}
}
}

View File

@ -0,0 +1,58 @@
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;
namespace ComputerStoreDatabaseImplement.Models
{
public class Request : IRequestModel
{
public int ID { get; private set; }
[Required]
public int OrderID { get; private set; }
[Required]
public double Price { get; private set; }
[ForeignKey("RequestID")]
public virtual List<RequestComponent> RequestComponents { get; private set; } = new();
public static Request? Create(RequestBindingModel model)
{
if (model == null)
{
return null;
}
return new Request()
{
ID = model.ID,
OrderID = model.OrderID,
Price = model.Price
};
}
public void Update(RequestBindingModel? model)
{
if (model == null)
{
return;
}
OrderID = model.OrderID;
Price = model.Price;
}
public RequestViewModel GetViewModel => new()
{
ID = ID,
OrderID = OrderID,
Price = Price
};
}
}

View File

@ -0,0 +1,66 @@
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;
namespace ComputerStoreDatabaseImplement.Models
{
public class Seller : ISellerModel
{
public int ID { get; private set; }
[Required]
public string Username { get; private set; } = string.Empty;
[Required]
public string Password { get; private set; } = string.Empty;
public string? FirstName { get; private set; } = string.Empty;
public string? LastName { get; private set; } = string.Empty;
public string? MiddleName { get; private set; } = string.Empty;
[ForeignKey("OrderID")]
public virtual List<Order> Orders { get; set; } = new();
public static Seller? Create(SellerBindingModel? model)
{
if (model == null)
{
return null;
}
return new Seller()
{
ID = model.ID,
Username = model.Username,
Password = model.Password,
FirstName = model.FirstName,
LastName = model.LastName,
MiddleName = model.MiddleName
};
}
public void Update(SellerBindingModel? model)
{
if (model == null) { return; }
Username = model.Username;
Password = model.Password;
FirstName = model.FirstName;
LastName = model.LastName;
MiddleName = model.MiddleName;
}
public SellerViewModel GetViewModel => new()
{
ID = ID,
Username = Username,
Password = Password,
FirstName = FirstName,
LastName = LastName,
MiddleName = MiddleName
};
}
}