aufrüumen
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.Services.Admin.Interfaces;
|
||||
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.Services.Admin.Interfaces;
|
||||
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.Services.Admin.Interfaces;
|
||||
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
// src/Webshop.Application/Services/Admin/AdminProductService.cs
|
||||
using Webshop.Application.DTOs; // AdminProductDto
|
||||
using Webshop.Domain.Entities;
|
||||
using Webshop.Domain.Interfaces;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System; // F<>r Guid
|
||||
using System.Linq; // F<>r Select
|
||||
using System.Linq;
|
||||
using Webshop.Application.DTOs.Products;
|
||||
using Webshop.Application.Services.Admin.Interfaces; // F<>r Select
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.Services.Admin.Interfaces;
|
||||
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
// src/Webshop.Application/Services/Admin/AdminSupplierService.cs
|
||||
using Webshop.Application.DTOs;
|
||||
using Webshop.Domain.Entities;
|
||||
using Webshop.Domain.Interfaces; // Wichtig für ISupplierRepository
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs.Suppliers;
|
||||
using Webshop.Application.Services.Admin.Interfaces;
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
{
|
||||
|
||||
@@ -5,27 +5,34 @@ 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
|
||||
using Webshop.Application.Services.Admin.Interfaces;
|
||||
using Webshop.Domain.Entities;
|
||||
using Webshop.Infrastructure.Data; // WICHTIG: Stellt sicher, dass ApplicationDbContext gefunden wird.
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
{
|
||||
// --- SCHRITT 1: Die fehlende Klassendeklaration ---
|
||||
public class AdminUserService : IAdminUserService
|
||||
{
|
||||
// Wir verwenden nun den UserManager mit unserer neuen ApplicationUser-Klasse
|
||||
// --- SCHRITT 2: Die fehlenden Feld-Deklarationen ---
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public AdminUserService(UserManager<ApplicationUser> userManager)
|
||||
// --- SCHRITT 3: Der Konstruktor, der die Felder zuweist ---
|
||||
public AdminUserService(UserManager<ApplicationUser> userManager, ApplicationDbContext context)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// --- AB HIER: Alle Ihre Methoden, unverändert ---
|
||||
public async Task<IEnumerable<UserDto>> GetAllUsersAsync()
|
||||
{
|
||||
// Alle Benutzer aus der Datenbank laden
|
||||
var users = await _userManager.Users.ToListAsync();
|
||||
var userDtos = new List<UserDto>();
|
||||
var users = await _userManager.Users
|
||||
.Include(u => u.Customer)
|
||||
.ToListAsync();
|
||||
|
||||
// Für jeden Benutzer ein DTO erstellen und die Daten mappen
|
||||
var userDtos = new List<UserDto>();
|
||||
foreach (var user in users)
|
||||
{
|
||||
userDtos.Add(new UserDto
|
||||
@@ -33,24 +40,28 @@ namespace Webshop.Application.Services.Admin
|
||||
Id = user.Id,
|
||||
Email = user.Email ?? string.Empty,
|
||||
UserName = user.UserName ?? string.Empty,
|
||||
CreatedDate = user.CreatedDate, // Dieses Feld ist jetzt verfügbar!
|
||||
CreatedDate = user.CreatedDate,
|
||||
EmailConfirmed = user.EmailConfirmed,
|
||||
Roles = (await _userManager.GetRolesAsync(user)).ToList() // Rollen des Benutzers abrufen
|
||||
Roles = (await _userManager.GetRolesAsync(user)).ToList(),
|
||||
LastActive = user.LastActive,
|
||||
FirstName = user.Customer?.FirstName ?? string.Empty,
|
||||
LastName = user.Customer?.LastName ?? string.Empty
|
||||
});
|
||||
}
|
||||
|
||||
return userDtos;
|
||||
}
|
||||
|
||||
public async Task<UserDto?> GetUserByIdAsync(string userId)
|
||||
{
|
||||
var user = await _userManager.FindByIdAsync(userId);
|
||||
var user = await _userManager.Users
|
||||
.Include(u => u.Customer)
|
||||
.FirstOrDefaultAsync(u => u.Id == userId);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Den gefundenen Benutzer in ein DTO umwandeln
|
||||
return new UserDto
|
||||
{
|
||||
Id = user.Id,
|
||||
@@ -58,8 +69,60 @@ namespace Webshop.Application.Services.Admin
|
||||
UserName = user.UserName ?? string.Empty,
|
||||
CreatedDate = user.CreatedDate,
|
||||
EmailConfirmed = user.EmailConfirmed,
|
||||
Roles = (await _userManager.GetRolesAsync(user)).ToList()
|
||||
Roles = (await _userManager.GetRolesAsync(user)).ToList(),
|
||||
LastActive = user.LastActive,
|
||||
FirstName = user.Customer?.FirstName ?? string.Empty,
|
||||
LastName = user.Customer?.LastName ?? string.Empty
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateUserRolesAsync(string userId, List<string> newRoles)
|
||||
{
|
||||
var user = await _userManager.FindByIdAsync(userId);
|
||||
if (user == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var existingRoles = await _userManager.GetRolesAsync(user);
|
||||
var removeResult = await _userManager.RemoveFromRolesAsync(user, existingRoles);
|
||||
if (!removeResult.Succeeded)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var addResult = await _userManager.AddToRolesAsync(user, newRoles);
|
||||
return addResult.Succeeded;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteUserAsync(string userId)
|
||||
{
|
||||
var user = await _userManager.FindByIdAsync(userId);
|
||||
if (user == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Kaskadierendes Löschen der abhängigen Daten
|
||||
var customer = await _context.Customers
|
||||
.Include(c => c.Orders)
|
||||
.Include(c => c.Reviews)
|
||||
.Include(c => c.Addresses)
|
||||
.FirstOrDefaultAsync(c => c.AspNetUserId == userId);
|
||||
|
||||
if (customer != null)
|
||||
{
|
||||
_context.Reviews.RemoveRange(customer.Reviews);
|
||||
_context.Orders.RemoveRange(customer.Orders);
|
||||
_context.Addresses.RemoveRange(customer.Addresses);
|
||||
_context.Customers.Remove(customer);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
// Zum Schluss den Identity-Benutzer löschen
|
||||
var result = await _userManager.DeleteAsync(user);
|
||||
return result.Succeeded;
|
||||
}
|
||||
|
||||
} // <-- Schließende Klammer für die Klasse
|
||||
} // <-- Schließende Klammer für den Namespace
|
||||
@@ -1,15 +0,0 @@
|
||||
// src/Webshop.Application/Services/Admin/IAdminUserService.cs
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs.Users; // UserDto
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
{
|
||||
public interface IAdminUserService
|
||||
{
|
||||
Task<IEnumerable<UserDto>> GetAllUsersAsync();
|
||||
Task<UserDto?> GetUserByIdAsync(string userId);
|
||||
// Task<bool> UpdateUserRolesAsync(string userId, List<string> newRoles); // Beispiel für zukünftige Methode
|
||||
// Task<bool> DeleteUserAsync(string userId); // Beispiel für zukünftige Methode
|
||||
}
|
||||
}
|
||||
@@ -7,10 +7,10 @@ using Webshop.Application.DTOs.Auth;
|
||||
using Webshop.Application.DTOs.Users;
|
||||
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
namespace Webshop.Application.Services.Admin.Interfaces
|
||||
{
|
||||
public interface IAdminCategoryService
|
||||
{
|
||||
// Fügen Sie hier Methodensignaturen hinzu
|
||||
// Fügen Sie hier Methodensignaturen hinzu
|
||||
}
|
||||
}
|
||||
@@ -7,10 +7,10 @@ using Webshop.Application.DTOs.Auth;
|
||||
using Webshop.Application.DTOs.Users;
|
||||
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
namespace Webshop.Application.Services.Admin.Interfaces
|
||||
{
|
||||
public interface IAdminDiscountService
|
||||
{
|
||||
// Fügen Sie hier Methodensignaturen hinzu
|
||||
// Fügen Sie hier Methodensignaturen hinzu
|
||||
}
|
||||
}
|
||||
@@ -7,10 +7,10 @@ using Webshop.Application.DTOs.Auth;
|
||||
using Webshop.Application.DTOs.Users;
|
||||
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
namespace Webshop.Application.Services.Admin.Interfaces
|
||||
{
|
||||
public interface IAdminOrderService
|
||||
{
|
||||
// Fügen Sie hier Methodensignaturen hinzu
|
||||
// Fügen Sie hier Methodensignaturen hinzu
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
// src/Webshop.Application/Services/Admin/IAdminProductService.cs
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs; // AdminProductDto
|
||||
using Webshop.Application.DTOs.Products;
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
namespace Webshop.Application.Services.Admin.Interfaces
|
||||
{
|
||||
public interface IAdminProductService
|
||||
{
|
||||
@@ -7,10 +7,10 @@ using Webshop.Application.DTOs.Auth;
|
||||
using Webshop.Application.DTOs.Users;
|
||||
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
namespace Webshop.Application.Services.Admin.Interfaces
|
||||
{
|
||||
public interface IAdminSettingService
|
||||
{
|
||||
// Fügen Sie hier Methodensignaturen hinzu
|
||||
// Fügen Sie hier Methodensignaturen hinzu
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,9 @@
|
||||
using System; // Für Guid
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs; // Für SupplierDto
|
||||
using Webshop.Application.DTOs.Suppliers;
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
namespace Webshop.Application.Services.Admin.Interfaces
|
||||
{
|
||||
public interface IAdminSupplierService
|
||||
{
|
||||
@@ -0,0 +1,32 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs.Users;
|
||||
|
||||
namespace Webshop.Application.Services.Admin.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// Definiert den Vertrag für den Dienst zur Verwaltung von Benutzern durch einen Admin.
|
||||
/// </summary>
|
||||
public interface IAdminUserService
|
||||
{
|
||||
/// <summary>
|
||||
/// Ruft eine Liste aller Benutzer mit ihren zugehörigen Daten ab.
|
||||
/// </summary>
|
||||
Task<IEnumerable<UserDto>> GetAllUsersAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Ruft einen einzelnen Benutzer anhand seiner ID ab.
|
||||
/// </summary>
|
||||
Task<UserDto?> GetUserByIdAsync(string userId);
|
||||
|
||||
/// <summary>
|
||||
/// Aktualisiert die Rollen eines bestimmten Benutzers.
|
||||
/// </summary>
|
||||
Task<bool> UpdateUserRolesAsync(string userId, List<string> newRoles);
|
||||
|
||||
/// <summary>
|
||||
/// Löscht einen Benutzer und alle seine abhängigen Daten (Kundenprofil, Bestellungen etc.).
|
||||
/// </summary>
|
||||
Task<bool> DeleteUserAsync(string userId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user