2024-06-04 22:26:13 +04:00
|
|
|
|
using Contracts.BindingModels;
|
|
|
|
|
using Contracts.SearchModels;
|
|
|
|
|
using Contracts.StorageContracts;
|
2024-06-04 22:52:59 +04:00
|
|
|
|
using DatabaseImplement.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-06-06 17:52:09 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
2024-06-04 22:26:13 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace DatabaseImplement.Implements
|
|
|
|
|
{
|
2024-06-22 21:15:11 +04:00
|
|
|
|
public class UserStorage : IUserStorage
|
|
|
|
|
{
|
|
|
|
|
public UserBindingModel? Delete(UserSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model.Id is null && model.Email is null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-06-04 22:26:13 +04:00
|
|
|
|
|
2024-06-22 21:15:11 +04:00
|
|
|
|
var context = new Database();
|
|
|
|
|
var user = context.Users.FirstOrDefault(u =>
|
|
|
|
|
(model.Id.HasValue && u.Id == model.Id)
|
|
|
|
|
|| (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email)));
|
2024-06-04 22:26:13 +04:00
|
|
|
|
|
2024-06-22 21:15:11 +04:00
|
|
|
|
if (user is null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Remove(user);
|
|
|
|
|
context.SaveChanges();
|
2024-06-04 22:26:13 +04:00
|
|
|
|
|
2024-06-22 21:15:11 +04:00
|
|
|
|
return user.GetBindingModel();
|
|
|
|
|
}
|
2024-06-04 22:26:13 +04:00
|
|
|
|
|
2024-06-22 21:15:11 +04:00
|
|
|
|
public UserBindingModel? GetElement(UserSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model.Id is null && model.Email is null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var context = new Database();
|
|
|
|
|
return context.Users
|
|
|
|
|
.Include(u => u.Role)
|
|
|
|
|
.FirstOrDefault(u =>
|
|
|
|
|
(model.Id.HasValue && u.Id == model.Id)
|
|
|
|
|
|| (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email)))
|
|
|
|
|
?.GetBindingModel();
|
|
|
|
|
}
|
2024-06-04 22:26:13 +04:00
|
|
|
|
|
2024-06-22 21:15:11 +04:00
|
|
|
|
public IEnumerable<UserBindingModel> GetList(UserSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
var context = new Database();
|
|
|
|
|
if (model is null)
|
|
|
|
|
{
|
|
|
|
|
return context.Users
|
|
|
|
|
.Include(u => u.Role)
|
|
|
|
|
.Select(r => r.GetBindingModel());
|
|
|
|
|
}
|
|
|
|
|
if (model.Id is null && model.Email is null && !model.OnlyImportantMails is null)
|
|
|
|
|
{
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
return context.Users
|
|
|
|
|
.Where(u =>
|
|
|
|
|
(model.Id.HasValue && u.Id == model.Id)
|
|
|
|
|
|| (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email))
|
|
|
|
|
|| (model.OnlyImportantMails.HasValue && u.OnlyImportantMails == model.OnlyImportantMails))
|
|
|
|
|
.Include(u => u.Role)
|
|
|
|
|
.Select(r => r.GetBindingModel());
|
|
|
|
|
}
|
2024-06-04 22:26:13 +04:00
|
|
|
|
|
2024-06-22 21:15:11 +04:00
|
|
|
|
public UserBindingModel? Insert(UserBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var context = new Database();
|
|
|
|
|
var role = context.Roles.FirstOrDefault(r => r.Id == model.Role.Id);
|
|
|
|
|
if (role is null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var newUser = Models.User.ToUserFromBinding(model, role);
|
2024-06-04 22:26:13 +04:00
|
|
|
|
|
2024-06-22 21:15:11 +04:00
|
|
|
|
context.Users.Add(newUser);
|
|
|
|
|
context.SaveChanges();
|
2024-06-04 22:26:13 +04:00
|
|
|
|
|
2024-06-22 21:15:11 +04:00
|
|
|
|
return newUser.GetBindingModel();
|
|
|
|
|
}
|
2024-06-04 22:26:13 +04:00
|
|
|
|
|
2024-06-22 21:15:11 +04:00
|
|
|
|
public UserBindingModel? Update(UserBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var context = new Database();
|
|
|
|
|
var user = context.Users
|
|
|
|
|
.FirstOrDefault(u => u.Id == model.Id);
|
|
|
|
|
var role = context.Roles.FirstOrDefault(r => r.Id == model.Role.Id);
|
2024-06-04 22:26:13 +04:00
|
|
|
|
|
2024-06-22 21:15:11 +04:00
|
|
|
|
if (user is null || role is null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-06-04 22:26:13 +04:00
|
|
|
|
|
2024-06-22 21:15:11 +04:00
|
|
|
|
user.Update(model, role);
|
2024-06-04 22:26:13 +04:00
|
|
|
|
|
2024-06-22 21:15:11 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return user.GetBindingModel();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-04 22:26:13 +04:00
|
|
|
|
}
|