|
|
|
@ -4,6 +4,7 @@ using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Entity;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Shared;
|
|
|
|
|
|
|
|
|
|
namespace Contextlib
|
|
|
|
@ -23,9 +24,20 @@ namespace Contextlib
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
@ -37,13 +49,13 @@ namespace Contextlib
|
|
|
|
|
|
|
|
|
|
public async Task<bool> ExistEmail(string email)
|
|
|
|
|
{
|
|
|
|
|
IQueryable<Users> users = _context.users.Where(u=>u.Email == email);
|
|
|
|
|
Users? users = await _context.users.Where(u=>u.Email == email).FirstOrDefaultAsync();
|
|
|
|
|
return users != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> ExistUsername(string username)
|
|
|
|
|
{
|
|
|
|
|
IQueryable<Users> users = _context.users.Where(u => u.UserName == username);
|
|
|
|
|
Users? users = await _context.users.Where(u => u.UserName == username).FirstOrDefaultAsync();
|
|
|
|
|
return users != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -56,7 +68,7 @@ namespace Contextlib
|
|
|
|
|
public async Task<string> GetHashPassword(string username)
|
|
|
|
|
{
|
|
|
|
|
Users? user = _context.users.Where(u=>u.UserName == username).FirstOrDefault();
|
|
|
|
|
if (user != null)
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
@ -130,14 +142,19 @@ namespace Contextlib
|
|
|
|
|
|
|
|
|
|
public async Task<Users> UpdateUser(int userId, Users user)
|
|
|
|
|
{
|
|
|
|
|
Users? u = _repo.GetById(userId);
|
|
|
|
|
Users? u = _repo.GetById(userId, item => item.Id == userId, nameof(Users.Images));
|
|
|
|
|
if (u != null)
|
|
|
|
|
{
|
|
|
|
|
bool change = false;
|
|
|
|
|
if (user.IdImage != 0)
|
|
|
|
|
if (user.Images.ImgPath != null)
|
|
|
|
|
{
|
|
|
|
|
u.IdImage = user.IdImage;
|
|
|
|
|
change = true;
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|