Creating models and some realization
This commit is contained in:
parent
a07e4a771a
commit
4ee76a9693
@ -17,8 +17,10 @@ namespace ComputerShopContracts.BindingModels
|
||||
|
||||
public DateTime? DateImplement { get; set; }
|
||||
|
||||
public int OrderId { get; set; }
|
||||
|
||||
public int ReceivingId { get; set; }
|
||||
public Dictionary<int, (IOrderModel, int)> SupplyOrders
|
||||
{
|
||||
get;
|
||||
set;
|
||||
} = new();
|
||||
}
|
||||
}
|
||||
|
@ -20,15 +20,13 @@ namespace ComputerShopContracts.ViewModels
|
||||
[DisplayName("Дата выполнения")]
|
||||
public DateTime? DateImplement { get; set; }
|
||||
|
||||
[DisplayName("Номер заказа")]
|
||||
public int OrderId { get; set; }
|
||||
|
||||
[DisplayName("Номер получения")]
|
||||
public int ReceivingId { get; set; }
|
||||
|
||||
[DisplayName("Номер")]
|
||||
public int Id { get; set; }
|
||||
|
||||
public Dictionary<int, (IOrderModel, int)> SupplyOrders
|
||||
{
|
||||
get;
|
||||
set;
|
||||
} = new();
|
||||
|
||||
|
||||
|
||||
|
@ -7,7 +7,6 @@ namespace ComputerShopDataModels.Models
|
||||
SupplyStatus Status { get; }
|
||||
DateTime DateCreate { get; }
|
||||
DateTime? DateImplement { get; }
|
||||
int OrderId { get; }
|
||||
int ReceivingId { get; }
|
||||
Dictionary<int, (IOrderModel, int)> SupplyOrders { get; }
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,87 @@
|
||||
using ComputerShopContracts.BindingModels;
|
||||
using ComputerShopContracts.SearchModels;
|
||||
using ComputerShopContracts.StorageContracts;
|
||||
using ComputerShopContracts.ViewModels;
|
||||
using ComputerShopDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerShopDatabaseImplement.Implements
|
||||
{
|
||||
internal class EquipmentReceivingStorage : IEquipmentReceivingStorage
|
||||
{
|
||||
public EquipmentReceivingViewModel? Delete(EquipmentReceivingBindingModel model)
|
||||
{
|
||||
using var context = new ComputerShopDatabase();
|
||||
var element = context.EquipmentReceivings.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.EquipmentReceivings.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public EquipmentReceivingViewModel? GetElement(EquipmentReceivingSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new ComputerShopDatabase();
|
||||
return context.EquipmentReceivings
|
||||
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?
|
||||
.GetViewModel;
|
||||
}
|
||||
|
||||
public List<EquipmentReceivingViewModel> GetFilteredList(EquipmentReceivingSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new ComputerShopDatabase();
|
||||
return context.EquipmentReceivings
|
||||
.Where(x => x.Id == model.Id)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<EquipmentReceivingViewModel> GetFullList()
|
||||
{
|
||||
using var context = new ComputerShopDatabase();
|
||||
return context.EquipmentReceivings.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public EquipmentReceivingViewModel? Insert(EquipmentReceivingBindingModel model)
|
||||
{
|
||||
var newOrder = EquipmentReceiving.Create(model);
|
||||
if (newOrder == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new ComputerShopDatabase();
|
||||
context.EquipmentReceivings.Add(newOrder);
|
||||
context.SaveChanges();
|
||||
return context.EquipmentReceivings.FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel;
|
||||
}
|
||||
|
||||
public EquipmentReceivingViewModel? Update(EquipmentReceivingBindingModel model)
|
||||
{
|
||||
using var context = new ComputerShopDatabase();
|
||||
var order = context.EquipmentReceivings.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (order == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
order.Update(model);
|
||||
context.SaveChanges();
|
||||
return context.EquipmentReceivings.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
@ -15,17 +15,42 @@ namespace ComputerShopDatabaseImplement.Implements
|
||||
{
|
||||
public OrderViewModel? Delete(OrderBindingModel model)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
using var context = new ComputerShopDatabase();
|
||||
var element = context.Orders.FirstOrDefault(rec => rec.Id ==
|
||||
model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Orders.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public OrderViewModel? GetElement(OrderSearchModel model)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new ComputerShopDatabase();
|
||||
return context.Orders
|
||||
.FirstOrDefault(x =>
|
||||
(model.Id.HasValue && x.Id == model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new ComputerShopDatabase();
|
||||
return context.Orders
|
||||
.Where(x => x.Id == model.Id)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<OrderViewModel> GetFullList()
|
||||
|
@ -0,0 +1,113 @@
|
||||
using ComputerShopContracts.BindingModels;
|
||||
using ComputerShopContracts.SearchModels;
|
||||
using ComputerShopContracts.StorageContracts;
|
||||
using ComputerShopContracts.ViewModels;
|
||||
using ComputerShopDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerShopDatabaseImplement.Implements
|
||||
{
|
||||
internal class SupplyStorage : ISupplyStorage
|
||||
{
|
||||
public SupplyViewModel? Delete(SupplyBindingModel model)
|
||||
{
|
||||
using var context = new ComputerShopDatabase();
|
||||
var element = context.Supplies
|
||||
.Include(x => x.Orders)
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Supplies.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public SupplyViewModel? GetElement(SupplySearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new ComputerShopDatabase();
|
||||
return context.Supplies
|
||||
.Include(x => x.Orders)
|
||||
.ThenInclude(x => x.Order)
|
||||
.FirstOrDefault(x =>
|
||||
(model.Id.HasValue && x.Id ==
|
||||
model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public List<SupplyViewModel> GetFilteredList(SupplySearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new ComputerShopDatabase();
|
||||
return context.Supplies
|
||||
.Include(x => x.Orders)
|
||||
.ThenInclude(x => x.Order)
|
||||
.Where(x => x.Id == model.Id)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<SupplyViewModel> GetFullList()
|
||||
{
|
||||
using var context = new ComputerShopDatabase();
|
||||
return context.Supplies
|
||||
.Include(x => x.Orders)
|
||||
.ThenInclude(x => x.Order)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public SupplyViewModel? Insert(SupplyBindingModel model)
|
||||
{
|
||||
using var context = new ComputerShopDatabase();
|
||||
var newProduct = Supply.Create(context, model);
|
||||
if (newProduct == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Supplies.Add(newProduct);
|
||||
context.SaveChanges();
|
||||
return newProduct.GetViewModel;
|
||||
}
|
||||
|
||||
public SupplyViewModel? Update(SupplyBindingModel model)
|
||||
{
|
||||
using var context = new ComputerShopDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var product = context.Supplies.FirstOrDefault(rec =>
|
||||
rec.Id == model.Id);
|
||||
if (product == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
product.Update(model);
|
||||
context.SaveChanges();
|
||||
product.UpdateOrders(context, model);
|
||||
transaction.Commit();
|
||||
return product.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
using ComputerShopContracts.BindingModels;
|
||||
using ComputerShopContracts.ViewModels;
|
||||
using ComputerShopDataModels.Enums;
|
||||
using ComputerShopDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerShopDatabaseImplement.Models
|
||||
{
|
||||
internal class EquipmentReceiving : IEquipmentReceivingModel
|
||||
{
|
||||
|
||||
public DateTime? DateImplement { get; private set; }
|
||||
|
||||
public int Id { get; set; }
|
||||
public EquipmentReceivingStatus Status { get; private set; } = EquipmentReceivingStatus.Неизвестен;
|
||||
|
||||
public static EquipmentReceiving? Create(EquipmentReceivingBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new EquipmentReceiving
|
||||
{
|
||||
Status = model.Status,
|
||||
DateImplement = model.DateImplement,
|
||||
Id = model.Id,
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(EquipmentReceivingBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Status = model.Status;
|
||||
DateImplement = model.DateImplement;
|
||||
}
|
||||
|
||||
public EquipmentReceivingViewModel GetViewModel => new()
|
||||
{
|
||||
DateImplement = DateImplement,
|
||||
Id = Id,
|
||||
Status = Status,
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,114 @@
|
||||
using ComputerShopContracts.BindingModels;
|
||||
using ComputerShopContracts.ViewModels;
|
||||
using ComputerShopDataModels.Enums;
|
||||
using ComputerShopDataModels.Models;
|
||||
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
|
||||
{
|
||||
internal class Supply : ISupplyModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public SupplyStatus Status { get; private set; } = SupplyStatus.Неизвестен;
|
||||
[Required]
|
||||
|
||||
public DateTime DateCreate { get; private set; } = DateTime.Now;
|
||||
|
||||
public DateTime? DateImplement { get; private set; }
|
||||
|
||||
public int OrderId { get; set; }
|
||||
|
||||
public int ReceivingId { get; set; }
|
||||
private Dictionary<int, (IOrderModel, int)>? _supplyOrders =
|
||||
null;
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IOrderModel, int)> SupplyOrders
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_supplyOrders == null)
|
||||
{
|
||||
_supplyOrders = Orders
|
||||
.ToDictionary(recPC => recPC.OrderId, recPC =>
|
||||
(recPC.Order as IOrderModel, recPC.Count));
|
||||
}
|
||||
return _supplyOrders;
|
||||
}
|
||||
}
|
||||
[ForeignKey("SupplyId")]
|
||||
public virtual List<SupplyOrder> Orders { get; set; } = new();
|
||||
[ForeignKey("SupplyId")]
|
||||
public virtual List<EquipmentReceiving> Receivings { get; set; } = new();
|
||||
|
||||
public static Supply Create(ComputerShopDatabase context, SupplyBindingModel model)
|
||||
{
|
||||
return new Supply
|
||||
{
|
||||
Id = model.Id,
|
||||
Status = model.Status,
|
||||
DateCreate = model.DateCreate,
|
||||
DateImplement = model.DateImplement,
|
||||
Orders = model.SupplyOrders.Select(x => new
|
||||
SupplyOrder
|
||||
{
|
||||
Order = context.Orders.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
public void Update(SupplyBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Status = model.Status;
|
||||
DateImplement = model.DateImplement;
|
||||
}
|
||||
public SupplyViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Status = Status,
|
||||
DateCreate = DateCreate,
|
||||
DateImplement = DateImplement,
|
||||
SupplyOrders = SupplyOrders
|
||||
};
|
||||
public void UpdateOrders(ComputerShopDatabase context, SupplyBindingModel model)
|
||||
{
|
||||
var SupplyOrders = context.SupplyOrders.Where(rec =>
|
||||
rec.Id == model.Id).ToList();
|
||||
if (SupplyOrders != null && SupplyOrders.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.SupplyOrders.RemoveRange(SupplyOrders.Where(rec
|
||||
=> !model.SupplyOrders.ContainsKey(rec.OrderId)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateOrder in SupplyOrders)
|
||||
{
|
||||
updateOrder.Count = model.SupplyOrders[updateOrder.OrderId].Item2;
|
||||
model.SupplyOrders.Remove(updateOrder.OrderId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var supply = context.Supplies.First(x => x.Id == Id);
|
||||
foreach (var pc in model.SupplyOrders)
|
||||
{
|
||||
context.SupplyOrders.Add(new SupplyOrder
|
||||
{
|
||||
Supply = supply,
|
||||
Order = context.Orders.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_supplyOrders = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -11,10 +11,10 @@ namespace ComputerShopDatabaseImplement.Models
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public int SupplyId { get; set; }
|
||||
[Required]
|
||||
public int OrderId { get; set; }
|
||||
[Required]
|
||||
public int SupplyId { get; set; }
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
public virtual Order Order { get; set; } = new();
|
||||
public virtual Supply Supply { get; set; } = new();
|
||||
|
Loading…
Reference in New Issue
Block a user