SUBD_Gerimovich_Ilya_PIbd-22/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Implements/UsersStorage.cs

128 lines
3.9 KiB
C#
Raw Normal View History

2024-05-24 12:23:50 +04:00
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.StoragesContracts;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureAssemblyDatabaseImplement.Implements
{
2024-05-24 13:05:47 +04:00
public class UsersStorage : IUsersStorage
2024-05-24 12:23:50 +04:00
{
public List<FurnitureViewModel> GetFullList()
{
using var context = new FurnitureAssemblyDatabase();
return context.Furnitures
.Include(x => x.WorkPieces)
.ThenInclude(x => x.WorkPiece)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
2024-05-24 13:05:47 +04:00
public List<FurnitureViewModel> GetFilteredList(UsersSearchModel model)
2024-05-24 12:23:50 +04:00
{
2024-05-24 13:05:47 +04:00
if (string.IsNullOrEmpty(model.UsersName))
2024-05-24 12:23:50 +04:00
{
return new();
}
using var context = new FurnitureAssemblyDatabase();
return context.Furnitures
.Include(x => x.WorkPieces)
.ThenInclude(x => x.WorkPiece)
2024-05-24 13:05:47 +04:00
.Where(x => x.UsersName.Contains(model.UsersName))
2024-05-24 12:23:50 +04:00
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
2024-05-24 13:05:47 +04:00
public FurnitureViewModel? GetElement(UsersSearchModel model)
2024-05-24 12:23:50 +04:00
{
2024-05-24 13:05:47 +04:00
if (string.IsNullOrEmpty(model.UsersName) && !model.Id.HasValue)
2024-05-24 12:23:50 +04:00
{
return null;
}
using var context = new FurnitureAssemblyDatabase();
return context.Furnitures
.Include(x => x.WorkPieces)
.ThenInclude(x => x.WorkPiece)
2024-05-24 13:05:47 +04:00
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.UsersName) && x.UsersName == model.UsersName) ||
2024-05-24 12:23:50 +04:00
model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
}
2024-05-24 13:05:47 +04:00
public FurnitureViewModel? Insert(UsersBindingModel model)
2024-05-24 12:23:50 +04:00
{
using var context = new FurnitureAssemblyDatabase();
2024-05-24 13:05:47 +04:00
var newFurniture = Users.Create(context, model);
2024-05-24 12:23:50 +04:00
if (newFurniture == null)
{
return null;
}
context.Furnitures.Add(newFurniture);
context.SaveChanges();
return newFurniture.GetViewModel;
}
2024-05-24 13:05:47 +04:00
public FurnitureViewModel? Update(UsersBindingModel model)
2024-05-24 12:23:50 +04:00
{
using var context = new FurnitureAssemblyDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var furniture = context.Furnitures.FirstOrDefault(rec => rec.Id == model.Id);
if (furniture == null)
{
return null;
}
furniture.Update(model);
context.SaveChanges();
furniture.UpdateWorkPieces(context, model);
transaction.Commit();
return furniture.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
2024-05-24 13:05:47 +04:00
public FurnitureViewModel? Delete(UsersBindingModel model)
2024-05-24 12:23:50 +04:00
{
using var context = new FurnitureAssemblyDatabase();
var element = context.Furnitures
.Include(x => x.WorkPieces)
.Include(x => x.Orders)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Furnitures.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}