2023-04-07 19:22:59 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using UniversityContracts.BindingModels;
|
|
|
|
|
using UniversityContracts.SearchModels;
|
|
|
|
|
using UniversityContracts.StoragesContracts;
|
|
|
|
|
using UniversityContracts.ViewModels;
|
|
|
|
|
using UniversityDataBaseImplemet.Models;
|
|
|
|
|
|
|
|
|
|
namespace UniversityDataBaseImplemet.Implements
|
|
|
|
|
{
|
|
|
|
|
public class UserStorage : IUserStorage
|
|
|
|
|
{
|
|
|
|
|
public UserViewModel? Delete(UserBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
var user = context.User
|
2023-04-07 19:38:26 +04:00
|
|
|
|
.Include(record => record.Role)
|
2023-04-07 19:22:59 +04:00
|
|
|
|
.FirstOrDefault(record => record.Id.Equals(model.Id));
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.User.Remove(user);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return user.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UserViewModel? GetElement(UserSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
return context.User
|
2023-04-07 19:38:26 +04:00
|
|
|
|
.Include(record => record.Role)
|
2023-04-07 19:22:59 +04:00
|
|
|
|
.FirstOrDefault(record => record.Id.Equals(model.Id))
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<UserViewModel> GetFilteredList(UserSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.User
|
2023-04-07 19:38:26 +04:00
|
|
|
|
.Include(record => record.Role)
|
2023-04-07 19:22:59 +04:00
|
|
|
|
.Where(record => record.Id.Equals(model.Id))
|
|
|
|
|
.Select(record => record.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else return new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<UserViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
return context.User
|
2023-04-07 19:38:26 +04:00
|
|
|
|
.Include(record => record.Role)
|
2023-04-07 19:22:59 +04:00
|
|
|
|
.Select(record => record.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UserViewModel? Insert(UserBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var newUser = User.Create(model);
|
|
|
|
|
if (newUser == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
context.User.Add(newUser);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newUser.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UserViewModel? Update(UserBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var user = context.User
|
2023-04-07 19:38:26 +04:00
|
|
|
|
.Include(record => record.Role)
|
2023-04-07 19:22:59 +04:00
|
|
|
|
.FirstOrDefault(record => record.Id.Equals(model.Id));
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
user.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
return user.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|