97 lines
2.0 KiB
C#
97 lines
2.0 KiB
C#
using Contracts.BindingModels;
|
|
using Contracts.SearchModels;
|
|
using Contracts.StorageContracts;
|
|
using DatabaseImplement.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
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)
|
|
{
|
|
if (model.Id is null && model.Email is null)
|
|
{
|
|
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)
|
|
{
|
|
if (model.Id is null && model.Email is null)
|
|
{
|
|
return null;
|
|
}
|
|
var context = new Database();
|
|
return context.Users
|
|
.Include(u => u.Role)
|
|
.FirstOrDefault(u => u.Equals(model))
|
|
?.GetBindingModel();
|
|
}
|
|
|
|
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)
|
|
{
|
|
return [];
|
|
}
|
|
return context.Users
|
|
.Where(u => u.Equals(model))
|
|
.Include(u => u.Role)
|
|
.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();
|
|
var user = context.Users
|
|
.Include(u => u.Role)
|
|
.FirstOrDefault(u => u.Id == model.Id);
|
|
|
|
if (user is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
user.Update(model);
|
|
|
|
context.SaveChanges();
|
|
return user.GetBindingModel();
|
|
}
|
|
}
|
|
} |