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-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
|
|
|
|
|
{
|
|
|
|
|
public class UserStorage : IUserStorage
|
|
|
|
|
{
|
|
|
|
|
public UserBindingModel? Delete(UserSearchModel model)
|
|
|
|
|
{
|
2024-06-04 23:43:52 +04:00
|
|
|
|
if (model.Id is null && model.Email is null)
|
2024-06-04 22:26:13 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var context = new Database();
|
|
|
|
|
var user = context.Users.FirstOrDefault(u => u.Equals(model));
|
|
|
|
|
|
|
|
|
|
if (user is null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Remove(user);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return user.GetBindingModel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UserBindingModel? GetElement(UserSearchModel model)
|
|
|
|
|
{
|
2024-06-04 23:43:52 +04:00
|
|
|
|
if (model.Id is null && model.Email is null)
|
2024-06-04 22:26:13 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var context = new Database();
|
|
|
|
|
return context.Users
|
2024-06-04 22:52:59 +04:00
|
|
|
|
.Include(u => u.Role)
|
2024-06-04 22:26:13 +04:00
|
|
|
|
.FirstOrDefault(u => u.Equals(model))
|
|
|
|
|
?.GetBindingModel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<UserBindingModel> GetList(UserSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
var context = new Database();
|
|
|
|
|
if (model is null)
|
|
|
|
|
{
|
2024-06-04 22:52:59 +04:00
|
|
|
|
return context.Users
|
|
|
|
|
.Include(u => u.Role)
|
|
|
|
|
.Select(r => r.GetBindingModel());
|
2024-06-04 22:26:13 +04:00
|
|
|
|
}
|
|
|
|
|
if (model.Id is null && model.Email is null)
|
|
|
|
|
{
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
return context.Users
|
|
|
|
|
.Where(u => u.Equals(model))
|
2024-06-04 22:52:59 +04:00
|
|
|
|
.Include(u => u.Role)
|
2024-06-04 22:26:13 +04:00
|
|
|
|
.Select(r => r.GetBindingModel());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UserBindingModel? Insert(UserBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var context = new Database();
|
|
|
|
|
var newUser = Models.User.ToUserFromBinding(model);
|
|
|
|
|
|
|
|
|
|
context.Users.Add(newUser);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return newUser.GetBindingModel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UserBindingModel? Update(UserBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var context = new Database();
|
2024-06-04 22:52:59 +04:00
|
|
|
|
var user = context.Users
|
|
|
|
|
.Include(u => u.Role)
|
|
|
|
|
.FirstOrDefault(u => u.Id == model.Id);
|
2024-06-04 22:26:13 +04:00
|
|
|
|
|
|
|
|
|
if (user is null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.Update(model);
|
|
|
|
|
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return user.GetBindingModel();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|