реализован BuildStorage

This commit is contained in:
Николай 2023-04-01 17:20:35 +04:00
parent aac6acd899
commit d8423e1e33

View File

@ -2,6 +2,7 @@
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement;
using HardwareShopDatabaseImplement.Models.Worker;
using Microsoft.EntityFrameworkCore;
namespace HardwareShopContracts.StoragesContracts
@ -12,12 +13,13 @@ namespace HardwareShopContracts.StoragesContracts
{
using var context = new HardwareShopDatabase();
return context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Include(x => x.User)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<BuildViewModel> GetFilteredList(UserSearchModel model)
@ -28,33 +30,101 @@ namespace HardwareShopContracts.StoragesContracts
}
using var context = new HardwareShopDatabase();
return context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Where(x => x.UserId == model.Id)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Include(x => x.User)
.Where(x => x.UserId == model.Id)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public BuildViewModel? GetElement(BuildSearchModel model)
{
throw new NotImplementedException();
if (string.IsNullOrEmpty(model.BuildName) && !model.Id.HasValue)
{
return null;
}
using var context = new HardwareShopDatabase();
return context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Include(x => x.User)
.Where(x => x.UserId == model.Id)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.BuildName) && x.BuildName == model.BuildName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public BuildViewModel? Insert(BuildBindingModel model)
{
throw new NotImplementedException();
using var context = new HardwareShopDatabase();
var newBuild = Build.Create(context, model);
if (newBuild == null)
{
return null;
}
context.Builds.Add(newBuild);
context.SaveChanges();
return context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Include(x => x.User)
.Where(x => x.UserId == model.Id)
.FirstOrDefault(x => x.Id == newBuild.Id)
?.GetViewModel;
}
public BuildViewModel? Update(BuildBindingModel model)
{
throw new NotImplementedException();
using var context = new HardwareShopDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var build = context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.FirstOrDefault(x => x.Id == model.Id);
if (build == null)
{
return null;
}
build.Update(model);
context.SaveChanges();
build.UpdateComponents(context, model);
transaction.Commit();
return build?.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public BuildViewModel? Delete(BuildBindingModel model)
{
throw new NotImplementedException();
using var context = new HardwareShopDatabase();
var element = context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Include(x => x.User)
.Where(x => x.UserId == model.Id)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Builds.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}