PIbd-22-Ismailov_SUBD/BlogDataModels/BlogDatabaseImplement/Implements/UserStorage.cs
2023-09-06 22:02:39 +04:00

116 lines
3.7 KiB
C#

using BlogContracts.BindingModels;
using BlogContracts.SearchModels;
using BlogContracts.StoragesContracts;
using BlogContracts.ViewModels;
using BlogDatabase.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
namespace BlogDatabase.Implements
{
public class UserStorage : IUserStorage
{
public UserViewModel? Delete(UserBindingModel model)
{
using var context = new BlogDatabase();
var element = context.Users.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Users.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public UserViewModel? GetElement(UserSearchModel model)
{
if (string.IsNullOrEmpty(model.Username) && !model.Id.HasValue && string.IsNullOrEmpty(model.Email))
{
return null;
}
using var context = new BlogDatabase();
return context.Users
.Include(x => x.Role)
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Username) && x.Username ==
model.Username) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public List<UserViewModel> GetFilteredList(UserSearchModel model)
{
if (string.IsNullOrEmpty(model.Username) || string.IsNullOrEmpty(model.Email))
{
return new();
}
using var context = new BlogDatabase();
return context.Users
.Include(x => x.Role)
.Where(x => x.Username.Contains(model.Username))
.Select(x => x.GetViewModel)
.ToList();
}
public List<UserViewModel> GetFullList()
{
using var context = new BlogDatabase();
return context.Users
.Include(x => x.Role)
.Select(x => x.GetViewModel)
.ToList();
}
public UserViewModel? Insert(UserBindingModel model)
{
var newUser = User.Create(model);
if (newUser == null)
{
return null;
}
using var context = new BlogDatabase();
context.Users.Add(newUser);
context.SaveChanges();
return newUser.GetViewModel;
}
public UserViewModel? Update(UserBindingModel model)
{
using var context = new BlogDatabase();
var component = context.Users.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
public void UserInsertList(int num, List<RoleViewModel> roles)
{
Random rnd = new Random();
using var context = new BlogDatabase();
for (int i = 0; i < num; ++i)
{
var model = new UserBindingModel
{
Id = 0,
Username = rnd.Next(1000).ToString(),
Email = rnd.Next(1000).ToString(),
Password = rnd.Next(1000).ToString(),
RoleId = roles[rnd.Next(roles.Count)].Id,
};
context.Users.Add(User.Create(model));
}
context.SaveChanges();
}
}
}