This commit is contained in:
Tizian.Breuch
2025-07-22 17:26:04 +02:00
parent dcf6e428ab
commit c8635756f1
8 changed files with 359 additions and 77 deletions

View File

@@ -1,12 +1,125 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// src/Webshop.Application/Services/Admin/AdminProductService.cs
using Webshop.Application.DTOs; // AdminProductDto
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
using System.Collections.Generic; // Sicherstellen, dass für IEnumerable vorhanden
namespace Webshop.Application.Services.Admin
{
public class AdminProductService
{
private readonly IProductRepository _productRepository;
public AdminProductService(IProductRepository productRepository)
{
_productRepository = productRepository;
}
public async Task<IEnumerable<AdminProductDto>> GetAllAdminProductsAsync()
{
var products = await _productRepository.GetAllProductsAsync();
return products.Select(p => new AdminProductDto
{
Id = p.Id,
Name = p.Name,
Description = p.Description,
SKU = p.SKU,
Price = p.Price,
OldPrice = p.OldPrice,
IsActive = p.IsActive,
IsInStock = p.IsInStock,
StockQuantity = p.StockQuantity,
Weight = p.Weight,
ImageUrl = p.ImageUrl,
Slug = p.Slug,
CreatedDate = p.CreatedDate,
LastModifiedDate = p.LastModifiedDate,
SupplierId = p.SupplierId,
PurchasePrice = p.PurchasePrice
}).ToList();
}
public async Task<AdminProductDto?> GetAdminProductByIdAsync(Guid id)
{
var product = await _productRepository.GetProductByIdAsync(id);
if (product == null) return null;
return new AdminProductDto
{
Id = product.Id,
Name = product.Name,
Description = product.Description,
SKU = product.SKU,
Price = product.Price,
OldPrice = product.OldPrice,
IsActive = product.IsActive,
IsInStock = product.IsInStock,
StockQuantity = product.StockQuantity,
Weight = product.Weight,
ImageUrl = product.ImageUrl,
Slug = product.Slug,
CreatedDate = product.CreatedDate,
LastModifiedDate = product.LastModifiedDate,
SupplierId = product.SupplierId,
PurchasePrice = product.PurchasePrice
};
}
public async Task<AdminProductDto> CreateAdminProductAsync(AdminProductDto productDto)
{
var newProduct = new Product
{
Id = Guid.NewGuid(),
Name = productDto.Name,
Description = productDto.Description,
SKU = productDto.SKU,
Price = productDto.Price,
OldPrice = productDto.OldPrice,
IsActive = productDto.IsActive,
IsInStock = productDto.IsInStock,
StockQuantity = productDto.StockQuantity,
Weight = productDto.Weight,
ImageUrl = productDto.ImageUrl,
Slug = productDto.Slug,
CreatedDate = DateTimeOffset.UtcNow,
SupplierId = productDto.SupplierId,
PurchasePrice = productDto.PurchasePrice
};
await _productRepository.AddProductAsync(newProduct);
productDto.Id = newProduct.Id;
return productDto;
}
public async Task<bool> UpdateAdminProductAsync(AdminProductDto productDto)
{
var existingProduct = await _productRepository.GetProductByIdAsync(productDto.Id);
if (existingProduct == null) return false;
existingProduct.Name = productDto.Name;
existingProduct.Description = productDto.Description;
existingProduct.SKU = productDto.SKU;
existingProduct.Price = productDto.Price;
existingProduct.OldPrice = productDto.OldPrice;
existingProduct.IsActive = productDto.IsActive;
existingProduct.IsInStock = productDto.IsInStock;
existingProduct.StockQuantity = productDto.StockQuantity;
existingProduct.Weight = productDto.Weight;
existingProduct.ImageUrl = productDto.ImageUrl;
existingProduct.Slug = productDto.Slug;
existingProduct.LastModifiedDate = DateTimeOffset.UtcNow;
existingProduct.SupplierId = productDto.SupplierId;
existingProduct.PurchasePrice = productDto.PurchasePrice;
await _productRepository.UpdateProductAsync(existingProduct);
return true;
}
public async Task<bool> DeleteAdminProductAsync(Guid id)
{
var product = await _productRepository.GetProductByIdAsync(id);
if (product == null) return false;
await _productRepository.DeleteProductAsync(product.Id); // Verwende product.Id
return true;
}
}
}
}

View File

@@ -1,12 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// src/Webshop.Application/Services/Admin/AdminUserService.cs
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Webshop.Application.DTOs.Users; // UserDto
using System.Collections.Generic; // Sicherstellen, dass für IEnumerable vorhanden
namespace Webshop.Application.Services.Admin
{
public class AdminUserService
{
private readonly UserManager<IdentityUser> _userManager;
public AdminUserService(UserManager<IdentityUser> userManager)
{
_userManager = userManager;
}
public async Task<IEnumerable<UserDto>> GetAllUsersAsync()
{
var users = await _userManager.Users.ToListAsync();
var userDtos = new List<UserDto>();
foreach (var user in users)
{
var roles = await _userManager.GetRolesAsync(user);
userDtos.Add(new UserDto
{
Id = user.Id,
Email = user.Email ?? string.Empty,
UserName = user.UserName ?? string.Empty,
Roles = roles.ToList(),
// LockoutEnd kann als Näherung für CreatedDate verwendet werden,
// da IdentityUser keine direkte CreatedDate-Eigenschaft hat.
// Ideal wäre es, ein CustomUser-Modell zu verwenden, das von IdentityUser erbt.
CreatedDate = user.LockoutEnd ?? DateTimeOffset.MinValue,
EmailConfirmed = user.EmailConfirmed
});
}
return userDtos;
}
public async Task<UserDto?> GetUserByIdAsync(string userId)
{
var user = await _userManager.FindByIdAsync(userId);
if (user == null) return null;
var roles = await _userManager.GetRolesAsync(user);
return new UserDto
{
Id = user.Id,
Email = user.Email ?? string.Empty,
UserName = user.UserName ?? string.Empty,
Roles = roles.ToList(),
CreatedDate = user.LockoutEnd ?? DateTimeOffset.MinValue,
EmailConfirmed = user.EmailConfirmed
};
}
// TODO: Methoden zum Aktualisieren von Rollen, Löschen von Benutzern etc.
}
}
}