You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
186 lines
6.1 KiB
186 lines
6.1 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Entity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Shared;
|
|
|
|
namespace Contextlib
|
|
{
|
|
public class DbUsersManager : IUserService<Users>
|
|
{
|
|
private WTFContext _context;
|
|
private GenericRepository<Users> _repo;
|
|
|
|
public DbUsersManager( WTFContext context )
|
|
{
|
|
_context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null.");
|
|
_repo = new GenericRepository<Users>(_context);
|
|
}
|
|
|
|
|
|
|
|
public async Task AddUser(Users user)
|
|
{
|
|
/*if (await ExistEmail(user.Email) || (await ExistUsername(user.UserName)))
|
|
{
|
|
throw new ArgumentException("The given Email or Username Already exist");
|
|
}*/
|
|
if (user == null) {
|
|
throw new ArgumentNullException(nameof(user), "user cannot be null.");
|
|
}
|
|
var dbI = new DbImagesManager(_context);
|
|
var image = await dbI.GetImageByPath(user.Images.ImgPath);
|
|
if(image != null)
|
|
{
|
|
user.IdImage = image.Id;
|
|
user.Images = image;
|
|
}
|
|
_repo.Insert(user);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<int> CountUser()
|
|
{
|
|
return _repo.Count();
|
|
}
|
|
|
|
public async Task<bool> ExistEmail(string email)
|
|
{
|
|
Users? users = await _context.users.Where(u=>u.Email == email).FirstOrDefaultAsync();
|
|
return users != null;
|
|
}
|
|
|
|
public async Task<bool> ExistUsername(string username)
|
|
{
|
|
Users? users = await _context.users.Where(u => u.UserName == username).FirstOrDefaultAsync();
|
|
return users != null;
|
|
}
|
|
|
|
public async Task<PaginationResult<Users>> GetAllUser()
|
|
{
|
|
List<Users> users = _repo.GetItems(0,_repo.Count(),[nameof(Users.Images)]).ToList();
|
|
return new PaginationResult<Users>(users.Count,0,users.Count,users);
|
|
}
|
|
|
|
public async Task<string> GetHashPassword(string username)
|
|
{
|
|
Users? user = _context.users.Where(u=>u.UserName == username).FirstOrDefault();
|
|
if (user == null)
|
|
{
|
|
return "";
|
|
}
|
|
return user.Password;
|
|
}
|
|
|
|
public async Task<int> GetLastUserId()
|
|
{
|
|
PaginationResult<Users> users = await GetAllUser();
|
|
int lastUserId = 0;
|
|
foreach (Users user in users.items)
|
|
{
|
|
if (user.Id >= lastUserId)
|
|
{
|
|
lastUserId = user.Id + 1;
|
|
}
|
|
}
|
|
return lastUserId;
|
|
}
|
|
|
|
public async Task<PaginationResult<Users>> GetSomeUser(int index, int pageSize)
|
|
{
|
|
List<Users> users = _repo.GetItems(index, pageSize, [nameof(Users.Images)]).ToList();
|
|
return new PaginationResult<Users>(users.Count, index, pageSize, users);
|
|
}
|
|
|
|
public async Task<Users> GetUserByEmail(string email)
|
|
{
|
|
var user = _repo.GetItems(item => item.Email == email, 0, 1, [nameof(Character.Images)]).FirstOrDefault();
|
|
|
|
if (user == null)
|
|
{
|
|
throw new KeyNotFoundException($"Error : No user found with the email: {email}.");
|
|
}
|
|
|
|
return user;
|
|
}
|
|
|
|
public async Task<Users> GetUserById(int id)
|
|
{
|
|
Users? user =_repo.GetById(id, item => item.Id == id, nameof(Users.Images));
|
|
if (user == null)
|
|
{
|
|
throw new KeyNotFoundException($"Error : No users found with the ID: {id}.");
|
|
}
|
|
return user;
|
|
}
|
|
|
|
public async Task<Users> GetUserByUsername(string username)
|
|
{
|
|
var user = _repo.GetItems(item => item.UserName == username, 0, 1, [nameof(Character.Images)]).FirstOrDefault();
|
|
|
|
if (user == null)
|
|
{
|
|
throw new KeyNotFoundException($"Error : No user found with the name: {username}.");
|
|
}
|
|
|
|
return user;
|
|
}
|
|
|
|
public async Task RemoveUser(int id)
|
|
{
|
|
Users? user = _repo.GetById(id);
|
|
if (user == null)
|
|
{
|
|
throw new KeyNotFoundException($"Error : No users found with the ID: {id}.");
|
|
}
|
|
_repo.Delete(user);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task SetAdminRole(bool isAdmin)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public async Task<Users> UpdateUser(int userId, Users user)
|
|
{
|
|
Users? u = _repo.GetById(userId, item => item.Id == userId, nameof(Users.Images));
|
|
if (u != null)
|
|
{
|
|
bool change = false;
|
|
if (user.Images.ImgPath != null)
|
|
{
|
|
var dbI = new DbImagesManager(_context);
|
|
var img = await dbI.GetImageByPath(user.Images.ImgPath);
|
|
if (img != null)
|
|
{
|
|
u.IdImage = dbI.GetImageByPath(user.Images.ImgPath).Id;
|
|
change = true;
|
|
}
|
|
}
|
|
if (user.UserName != null)
|
|
{
|
|
u.UserName = user.UserName;
|
|
change = true;
|
|
}
|
|
if (user.Email != null)
|
|
{
|
|
u.Email = user.Email;
|
|
change = true;
|
|
}
|
|
if (user.Password != null)
|
|
{
|
|
u.Password = user.Password;
|
|
change = true;
|
|
}
|
|
_repo.Update(u);
|
|
if (change)_context.SaveChanges();
|
|
}
|
|
return u;
|
|
}
|
|
}
|
|
}
|