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

128 lines
3.7 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
{
2024-05-24 14:23:37 +04:00
public List<UsersViewModel> GetFullList()
2024-05-24 12:23:50 +04:00
{
using var context = new FurnitureAssemblyDatabase();
2024-05-24 14:23:37 +04:00
return context.Userss
.Include(x => x.Komments)
.ThenInclude(x => x.Komment)
2024-05-24 12:23:50 +04:00
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
2024-05-24 14:23:37 +04:00
public List<UsersViewModel> 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();
2024-05-24 14:23:37 +04:00
return context.Userss
.Include(x => x.Komments)
.ThenInclude(x => x.Komment)
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 14:23:37 +04:00
public UsersViewModel? 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();
2024-05-24 14:23:37 +04:00
return context.Userss
.Include(x => x.Komments)
.ThenInclude(x => x.Komment)
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 14:23:37 +04:00
public UsersViewModel? Insert(UsersBindingModel model)
2024-05-24 12:23:50 +04:00
{
using var context = new FurnitureAssemblyDatabase();
2024-05-24 14:23:37 +04:00
var newUser = Users.Create(context, model);
2024-05-24 12:23:50 +04:00
2024-05-24 14:23:37 +04:00
if (newUser == null)
2024-05-24 12:23:50 +04:00
{
return null;
}
2024-05-24 14:23:37 +04:00
context.Userss.Add(newUser);
2024-05-24 12:23:50 +04:00
context.SaveChanges();
2024-05-24 14:23:37 +04:00
return newUser.GetViewModel;
2024-05-24 12:23:50 +04:00
}
2024-05-24 14:23:37 +04:00
public UsersViewModel? Update(UsersBindingModel model)
2024-05-24 12:23:50 +04:00
{
using var context = new FurnitureAssemblyDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
2024-05-24 14:23:37 +04:00
var user = context.Userss.FirstOrDefault(rec => rec.Id == model.Id);
2024-05-24 12:23:50 +04:00
2024-05-24 14:23:37 +04:00
if (user == null)
2024-05-24 12:23:50 +04:00
{
return null;
}
2024-05-24 14:23:37 +04:00
user.Update(model);
2024-05-24 12:23:50 +04:00
context.SaveChanges();
2024-05-24 14:23:37 +04:00
user.UpdateWorkPieces(context, model);
2024-05-24 12:23:50 +04:00
transaction.Commit();
2024-05-24 14:23:37 +04:00
return user.GetViewModel;
2024-05-24 12:23:50 +04:00
}
catch
{
transaction.Rollback();
throw;
}
}
2024-05-24 14:23:37 +04:00
public UsersViewModel? Delete(UsersBindingModel model)
2024-05-24 12:23:50 +04:00
{
using var context = new FurnitureAssemblyDatabase();
2024-05-24 14:23:37 +04:00
var element = context.Userss
.Include(x => x.Komments)
.Include(x => x.Ads)
2024-05-24 12:23:50 +04:00
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
2024-05-24 14:23:37 +04:00
context.Userss.Remove(element);
2024-05-24 12:23:50 +04:00
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}