Initial commit - Auth
This commit is contained in:
Executable
+154
@@ -0,0 +1,154 @@
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using Auth.Contracts.DTOs.Auth;
|
||||
using Auth.Contracts.DTOs.Users;
|
||||
using Auth.Core;
|
||||
using Auth.Core.Services.Auth;
|
||||
using Auth.Domain;
|
||||
using Auth.Domain.Entities;
|
||||
using Auth.Domain.Entities.HR;
|
||||
using Generic.Services;
|
||||
|
||||
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Auth.Core.Services.HR.Users;
|
||||
|
||||
public class UserService
|
||||
{
|
||||
private readonly UserManager<AppUser> userManager;
|
||||
private readonly TokenGenerator tokenGenerator;
|
||||
private readonly Context context;
|
||||
private readonly EmailService emailService;
|
||||
|
||||
// private readonly OrderService orderService;
|
||||
|
||||
public UserService(
|
||||
UserManager<AppUser> userManager,
|
||||
TokenGenerator tokenGenerator,
|
||||
Context appDbContext,
|
||||
EmailService emailService
|
||||
)
|
||||
{
|
||||
this.emailService = emailService;
|
||||
this.tokenGenerator = tokenGenerator;
|
||||
context = appDbContext;
|
||||
this.userManager = userManager;
|
||||
}
|
||||
|
||||
public async Task<UserView> GetUser(string id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var user = await context.AppUsers.Where(x => x.Id == id).FirstOrDefaultAsync();
|
||||
return new UserView()
|
||||
{
|
||||
Name = user.Name,
|
||||
PhotoPath = user.PhotoPath,
|
||||
PhoneNumber = user.PhoneNumber,
|
||||
Email = user.Email,
|
||||
};
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw new Exception();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<UserVM>> GetAllUsers()
|
||||
{
|
||||
var users = await userManager.Users.ToListAsync();
|
||||
// foreach (var user in users)
|
||||
// {
|
||||
// user.Roles = (await userManager.GetRolesAsync(user)).ToList();
|
||||
// }
|
||||
return users.Select(x => AppUser.ToVM(x)).ToList();
|
||||
}
|
||||
|
||||
public async Task UpdateUserInfo(UserInfoUpdate userVM, string userId)
|
||||
{
|
||||
var user = await userManager.FindByIdAsync(userId);
|
||||
if (user is not null)
|
||||
{
|
||||
user.Name = userVM.Name;
|
||||
user.PhotoPath = userVM.PhotoPath;
|
||||
await userManager.UpdateAsync(user);
|
||||
}
|
||||
}
|
||||
|
||||
// public async Task<Location> GetLocation(string userId)
|
||||
// {
|
||||
// var user = await userManager.FindByIdAsync(userId);
|
||||
// if (user != null)
|
||||
// {
|
||||
// return new Location()
|
||||
// {
|
||||
// Address = user.Address,
|
||||
// Building = user.Building,
|
||||
// Flat = user.Flat,
|
||||
// Floor = user.Floor,
|
||||
// Lat = user.locationLati,
|
||||
// Lng = user.locationLong,
|
||||
// };
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
|
||||
// public async Task<Location> UpdateLocation(Location location, string userId)
|
||||
// {
|
||||
// var user = await userManager.FindByIdAsync(userId);
|
||||
|
||||
// if (user != null)
|
||||
// {
|
||||
// user.Address = location.Address;
|
||||
// user.Building = location.Building;
|
||||
// user.Flat = location.Flat;
|
||||
// user.Floor = location.Floor;
|
||||
// user.locationLati = location.Lat;
|
||||
// user.locationLong = location.Lng;
|
||||
// }
|
||||
|
||||
// context.Update(user);
|
||||
// await context.SaveChangesAsync();
|
||||
// return location;
|
||||
// }
|
||||
|
||||
// public async Task<IQueryable<AppUser>> FilterUsers(IQueryable<AppUser> query, UserFilter userFilter)
|
||||
// {
|
||||
// var list = query.ToList();
|
||||
|
||||
// if (!string.IsNullOrEmpty(userFilter.Role))
|
||||
// query = await userManager.GetUsersInRoleAsync(userFilter.Role);
|
||||
|
||||
// if (userFilter.UserFilterBy == UserFilterBy.Name)
|
||||
// query = query.Where(x => x.Name == userFilter.Search);
|
||||
|
||||
// if (userFilter.UserFilterBy == UserFilterBy.Email)
|
||||
// query = query.Where(x => x.Email == userFilter.Search);
|
||||
|
||||
// if (userFilter.UserFilterBy == UserFilterBy.PhoneNumber)
|
||||
// query = query.Where(x => x.PhoneNumber == userFilter.Search);
|
||||
|
||||
// query.Where(x => true).Include(x => x.Role)
|
||||
|
||||
// return query;
|
||||
|
||||
// }
|
||||
|
||||
// public async Task UpdateUser(string id, AppUser user)
|
||||
// {
|
||||
// //change Email
|
||||
// //change Password
|
||||
// //change
|
||||
// }
|
||||
|
||||
// public async Task SignUp(AppUser user)
|
||||
// {
|
||||
// await userManager.CreateAsync(user);
|
||||
// }
|
||||
|
||||
// public async Task Login()
|
||||
// {
|
||||
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user