реализован 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.SearchModels;
using HardwareShopContracts.ViewModels; using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement; using HardwareShopDatabaseImplement;
using HardwareShopDatabaseImplement.Models.Worker;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace HardwareShopContracts.StoragesContracts namespace HardwareShopContracts.StoragesContracts
@ -15,6 +16,7 @@ namespace HardwareShopContracts.StoragesContracts
.Include(x => x.Components) .Include(x => x.Components)
.ThenInclude(x => x.Component) .ThenInclude(x => x.Component)
.Include(x => x.Comments) .Include(x => x.Comments)
.Include(x => x.User)
.ToList() .ToList()
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
@ -31,6 +33,7 @@ namespace HardwareShopContracts.StoragesContracts
.Include(x => x.Components) .Include(x => x.Components)
.ThenInclude(x => x.Component) .ThenInclude(x => x.Component)
.Include(x => x.Comments) .Include(x => x.Comments)
.Include(x => x.User)
.Where(x => x.UserId == model.Id) .Where(x => x.UserId == model.Id)
.ToList() .ToList()
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
@ -39,22 +42,89 @@ namespace HardwareShopContracts.StoragesContracts
public BuildViewModel? GetElement(BuildSearchModel model) 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) 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) 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) 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;
} }
} }
} }