admin user
This commit is contained in:
@@ -1,26 +1,65 @@
|
||||
// Auto-generiert von CreateWebshopFiles.ps1
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs.Users;
|
||||
// src/Webshop.Application/Services/Admin/AdminUserService.cs
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs.Users;
|
||||
using Webshop.Domain.Entities; // Wichtiges using für ApplicationUser
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
{
|
||||
public class AdminUserService : IAdminUserService
|
||||
{
|
||||
private readonly UserManager<IdentityUser> _userManager;
|
||||
// Wir verwenden nun den UserManager mit unserer neuen ApplicationUser-Klasse
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
|
||||
public AdminUserService(UserManager<IdentityUser> userManager)
|
||||
public AdminUserService(UserManager<ApplicationUser> userManager)
|
||||
{
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
// HIER KOMMT DER VORHERIGE ADMINUSERSERVICE CODE HIN (GetAllUsersAsync, GetUserByIdAsync etc.)
|
||||
// Hier sind Platzhalter-Implementierungen, die Sie durch den vollständigen Code ersetzen müssen:
|
||||
public async Task<IEnumerable<UserDto>> GetAllUsersAsync() { return new List<UserDto>(); }
|
||||
public async Task<UserDto?> GetUserByIdAsync(string userId) { return null; }
|
||||
public async Task<IEnumerable<UserDto>> GetAllUsersAsync()
|
||||
{
|
||||
// Alle Benutzer aus der Datenbank laden
|
||||
var users = await _userManager.Users.ToListAsync();
|
||||
var userDtos = new List<UserDto>();
|
||||
|
||||
// Für jeden Benutzer ein DTO erstellen und die Daten mappen
|
||||
foreach (var user in users)
|
||||
{
|
||||
userDtos.Add(new UserDto
|
||||
{
|
||||
Id = user.Id,
|
||||
Email = user.Email ?? string.Empty,
|
||||
UserName = user.UserName ?? string.Empty,
|
||||
CreatedDate = user.CreatedDate, // Dieses Feld ist jetzt verfügbar!
|
||||
EmailConfirmed = user.EmailConfirmed,
|
||||
Roles = (await _userManager.GetRolesAsync(user)).ToList() // Rollen des Benutzers abrufen
|
||||
});
|
||||
}
|
||||
|
||||
return userDtos;
|
||||
}
|
||||
|
||||
public async Task<UserDto?> GetUserByIdAsync(string userId)
|
||||
{
|
||||
var user = await _userManager.FindByIdAsync(userId);
|
||||
if (user == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Den gefundenen Benutzer in ein DTO umwandeln
|
||||
return new UserDto
|
||||
{
|
||||
Id = user.Id,
|
||||
Email = user.Email ?? string.Empty,
|
||||
UserName = user.UserName ?? string.Empty,
|
||||
CreatedDate = user.CreatedDate,
|
||||
EmailConfirmed = user.EmailConfirmed,
|
||||
Roles = (await _userManager.GetRolesAsync(user)).ToList()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user