почти готово

This commit is contained in:
Extrimal 2024-04-16 23:28:39 +04:00
parent 2f7603387c
commit 4badc422e1
5 changed files with 295 additions and 2 deletions

View File

@ -22,5 +22,8 @@ namespace GarmentFactoryDatabaseImplement
public virtual DbSet<Textile> Textiles { set; get; } public virtual DbSet<Textile> Textiles { set; get; }
public virtual DbSet<TextileComponent> TextileComponents { set; get; } public virtual DbSet<TextileComponent> TextileComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; } public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<ShopTextile> ShopTextiles { set; get; }
} }
} }

View File

@ -0,0 +1,146 @@
using GarmentFactoryContracts.BindingModels;
using GarmentFactoryContracts.SearchModels;
using GarmentFactoryContracts.StoragesContracts;
using GarmentFactoryContracts.ViewModels;
using GarmentFactoryDatabaseImplement.Models;
using GarmentFactoryDataModels.Models;
using Microsoft.EntityFrameworkCore;
namespace GarmentFactoryDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public List<ShopViewModel> GetFullList()
{
using var context = new GarmentFactoryDatabase();
return context.Shops
.Include(x => x.Textiles)
.ThenInclude(x => x.Textile)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
using var context = new GarmentFactoryDatabase();
return context.Shops
.Include(x => x.Textiles)
.ThenInclude(x => x.Textile)
.Where(x => x.ShopName.Contains(model.ShopName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
{
return null;
}
using var context = new GarmentFactoryDatabase();
return context.Shops
.Include(x => x.Textiles)
.ThenInclude(x => x.Textile)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ShopViewModel? Insert(ShopBindingModel model)
{
using var context = new GarmentFactoryDatabase();
var newShop = Shop.Create(context, model);
if (newShop == null)
{
return null;
}
context.Shops.Add(newShop);
context.SaveChanges();
return newShop.GetViewModel;
}
public ShopViewModel? Update(ShopBindingModel model)
{
using var context = new GarmentFactoryDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var shop = context.Shops.FirstOrDefault(rec => rec.Id == model.Id);
if (shop == null)
{
return null;
}
shop.Update(model);
context.SaveChanges();
shop.UpdateTextiles(context, model);
transaction.Commit();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new GarmentFactoryDatabase();
var element = context.Shops
.Include(x => x.Textiles)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Shops.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public bool MakeSale(ITextileModel model, int count)
{
using var context = new GarmentFactoryDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
foreach (var shop in context.Shops.Include(x => x.Textiles).ThenInclude(x => x.Textile)
.Where(x => x.Textiles.Any(x => x.TextileId == model.Id))
.ToList())
{
var textile = shop.ShopTextiles[model.Id];
int min = Math.Min(textile.Item2, count);
if (min == textile.Item2)
{
shop.ShopTextiles.Remove(model.Id);
}
else
{
shop.ShopTextiles[model.Id] = (textile.Item1, textile.Item2 - min);
}
shop.UpdateTextiles(context, new() { Id = shop.Id, ShopTextiles = shop.ShopTextiles });
count -= min;
if (count == 0)
{
context.SaveChanges();
transaction.Commit();
return true;
}
}
transaction.Rollback();
return false;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -0,0 +1,115 @@
using GarmentFactoryContracts.BindingModels;
using GarmentFactoryContracts.ViewModels;
using GarmentFactoryDataModels.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 GarmentFactoryDatabaseImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; set; }
[Required]
public string ShopName { get; set; } = string.Empty;
[Required]
public string Address { get; set; } = string.Empty;
[Required]
public DateTime DateOpening { get; set; }
[Required]
public int TextilesMaximum { get; set; }
private Dictionary<int, (ITextileModel, int)>? _shopTextiles = null;
[NotMapped]
public Dictionary<int, (ITextileModel, int)> ShopTextiles
{
get
{
if (_shopTextiles == null)
{
_shopTextiles = Textiles
.ToDictionary(x => x.TextileId, x => (x.Textile as ITextileModel, x.Count));
}
return _shopTextiles;
}
}
[ForeignKey("ShopId")]
public virtual List<ShopTextile> Textiles { get; set; } = new();
public static Shop Create(GarmentFactoryDatabase context, ShopBindingModel model)
{
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
Address = model.Address,
DateOpening = model.DateOpening,
TextilesMaximum = model.TextilesMaximum,
Textiles = model.ShopTextiles.Select(x => new ShopTextile
{
Textile = context.Textiles.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(ShopBindingModel model)
{
ShopName = model.ShopName;
Address = model.Address;
DateOpening = model.DateOpening;
TextilesMaximum = model.TextilesMaximum;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Address = Address,
DateOpening = DateOpening,
TextilesMaximum = TextilesMaximum,
ShopTextiles = ShopTextiles
};
public void UpdateTextiles(GarmentFactoryDatabase context, ShopBindingModel model)
{
var shopTextiles = context.ShopTextiles.Where(rec => rec.ShopId == model.Id).ToList();
if (shopTextiles != null && shopTextiles.Count > 0)
{
context.ShopTextiles.RemoveRange(shopTextiles.Where(rec => !model.ShopTextiles.ContainsKey(rec.TextileId)));
context.SaveChanges();
foreach (var updateTextile in shopTextiles)
{
if (model.ShopTextiles.ContainsKey(updateTextile.TextileId))
{
updateTextile.Count = model.ShopTextiles[updateTextile.TextileId].Item2;
model.ShopTextiles.Remove(updateTextile.TextileId);
}
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var ic in model.ShopTextiles)
{
context.ShopTextiles.Add(new ShopTextile
{
Shop = shop,
Textile = context.Textiles.First(x => x.Id == ic.Key),
Count = ic.Value.Item2
});
context.SaveChanges();
}
_shopTextiles = null;
}
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GarmentFactoryDatabaseImplement.Models
{
public class ShopTextile
{
public int Id { get; set; }
[Required]
public int ShopId { get; set; }
[Required]
public int TextileId { get; set; }
[Required]
public int Count { get; set; }
public virtual Textile Textile { get; set; } = new();
public virtual Shop Shop { get; set; } = new();
}
}

View File

@ -34,6 +34,8 @@ namespace GarmentFactoryDatabaseImplement.Models
public virtual List<TextileComponent> Components { get; set; } = new(); public virtual List<TextileComponent> Components { get; set; } = new();
[ForeignKey("TextileId")] [ForeignKey("TextileId")]
public virtual List<Order> Orders { get; set; } = new(); public virtual List<Order> Orders { get; set; } = new();
[ForeignKey("TextileId")]
public virtual List<ShopTextile> ShopTextiles { get; set; } = new();
public static Textile Create(GarmentFactoryDatabase context, public static Textile Create(GarmentFactoryDatabase context,
TextileBindingModel model) TextileBindingModel model)
{ {