product
This commit is contained in:
@@ -12,6 +12,7 @@ namespace Webshop.Application.DTOs.Products
|
||||
public string Description { get; set; } = string.Empty;
|
||||
public string SKU { get; set; } = string.Empty;
|
||||
public decimal Price { get; set; }
|
||||
public decimal? OldPrice { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
public bool IsInStock { get; set; }
|
||||
public int StockQuantity { get; set; }
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
// RICHTIG - vollständig
|
||||
// src/Webshop.Application/Services/Public/Interfaces/IProductService.cs
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application;
|
||||
using Webshop.Application.DTOs.Products;
|
||||
|
||||
namespace Webshop.Application.Services.Public.Interfaces
|
||||
{
|
||||
public interface IProductService
|
||||
{
|
||||
Task<IEnumerable<ProductDto>> GetAllProductsAsync();
|
||||
|
||||
Task<ProductDto?> GetProductBySlugAsync(string slug);
|
||||
Task<IEnumerable<ProductDto>> GetFeaturedProductsAsync(); // << NEU >>
|
||||
Task<ServiceResult<IEnumerable<ProductDto>>> GetAllProductsAsync();
|
||||
Task<ServiceResult<ProductDto>> GetProductBySlugAsync(string slug);
|
||||
Task<ServiceResult<IEnumerable<ProductDto>>> GetFeaturedProductsAsync();
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,11 @@ using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application;
|
||||
using Webshop.Application.DTOs.Categorie;
|
||||
using Webshop.Application.DTOs.Products;
|
||||
using Webshop.Application.Services.Public.Interfaces;
|
||||
using Webshop.Domain.Interfaces;
|
||||
using Webshop.Domain.Entities;
|
||||
using Webshop.Infrastructure.Data;
|
||||
|
||||
namespace Webshop.Application.Services.Public
|
||||
@@ -20,77 +21,34 @@ namespace Webshop.Application.Services.Public
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ProductDto>> GetAllProductsAsync()
|
||||
public async Task<ServiceResult<IEnumerable<ProductDto>>> GetAllProductsAsync()
|
||||
{
|
||||
var products = await _context.Products
|
||||
.Include(p => p.Productcategories).ThenInclude(pc => pc.categorie)
|
||||
.Include(p => p.Images) // Lade Bilder mit
|
||||
.Include(p => p.Images)
|
||||
.Where(p => p.IsActive)
|
||||
.ToListAsync();
|
||||
|
||||
return products.Select(p => new ProductDto
|
||||
{
|
||||
Id = p.Id,
|
||||
Name = p.Name,
|
||||
Description = p.ShortDescription,
|
||||
Price = p.Price,
|
||||
SKU = p.SKU,
|
||||
IsInStock = p.IsInStock,
|
||||
Slug = p.Slug,
|
||||
categories = p.Productcategories.Select(pc => new CategorieDto
|
||||
{
|
||||
Id = pc.categorie.Id,
|
||||
Name = pc.categorie.Name,
|
||||
Slug = pc.categorie.Slug
|
||||
}).ToList(),
|
||||
Images = p.Images.Select(img => new ProductImageDto
|
||||
{
|
||||
Id = img.Id,
|
||||
Url = img.Url,
|
||||
IsMainImage = img.IsMainImage,
|
||||
DisplayOrder = img.DisplayOrder
|
||||
}).ToList()
|
||||
}).ToList();
|
||||
var dtos = products.Select(p => MapToDto(p, useShortDescription: true)).ToList();
|
||||
return ServiceResult.Ok<IEnumerable<ProductDto>>(dtos);
|
||||
}
|
||||
|
||||
public async Task<ProductDto?> GetProductBySlugAsync(string slug)
|
||||
public async Task<ServiceResult<ProductDto>> GetProductBySlugAsync(string slug)
|
||||
{
|
||||
var product = await _context.Products
|
||||
.Include(p => p.Productcategories).ThenInclude(pc => pc.categorie)
|
||||
.Include(p => p.Images) // Lade Bilder mit
|
||||
.Include(p => p.Images)
|
||||
.FirstOrDefaultAsync(p => p.Slug == slug && p.IsActive);
|
||||
|
||||
if (product == null)
|
||||
{
|
||||
return null;
|
||||
return ServiceResult.Fail<ProductDto>(ServiceResultType.NotFound, $"Produkt mit dem Slug '{slug}' wurde nicht gefunden.");
|
||||
}
|
||||
|
||||
return new ProductDto
|
||||
{
|
||||
Id = product.Id,
|
||||
Name = product.Name,
|
||||
Description = product.Description,
|
||||
Price = product.Price,
|
||||
SKU = product.SKU,
|
||||
IsInStock = product.IsInStock,
|
||||
Slug = product.Slug,
|
||||
categories = product.Productcategories.Select(pc => new CategorieDto
|
||||
{
|
||||
Id = pc.categorie.Id,
|
||||
Name = pc.categorie.Name,
|
||||
Slug = pc.categorie.Slug
|
||||
}).ToList(),
|
||||
Images = product.Images.Select(img => new ProductImageDto
|
||||
{
|
||||
Id = img.Id,
|
||||
Url = img.Url,
|
||||
IsMainImage = img.IsMainImage,
|
||||
DisplayOrder = img.DisplayOrder
|
||||
}).ToList()
|
||||
};
|
||||
return ServiceResult.Ok(MapToDto(product, useShortDescription: false));
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ProductDto>> GetFeaturedProductsAsync()
|
||||
public async Task<ServiceResult<IEnumerable<ProductDto>>> GetFeaturedProductsAsync()
|
||||
{
|
||||
var products = await _context.Products
|
||||
.Where(p => p.IsActive && p.IsFeatured)
|
||||
@@ -99,32 +57,38 @@ namespace Webshop.Application.Services.Public
|
||||
.Include(p => p.Productcategories).ThenInclude(pc => pc.categorie)
|
||||
.ToListAsync();
|
||||
|
||||
return products.Select(p => new ProductDto
|
||||
var dtos = products.Select(p => MapToDto(p, useShortDescription: true)).ToList();
|
||||
return ServiceResult.Ok<IEnumerable<ProductDto>>(dtos);
|
||||
}
|
||||
|
||||
// Private Hilfsmethode, um Codeduplizierung zu vermeiden
|
||||
private ProductDto MapToDto(Product product, bool useShortDescription)
|
||||
{
|
||||
return new ProductDto
|
||||
{
|
||||
Id = p.Id,
|
||||
Name = p.Name,
|
||||
Description = p.ShortDescription, // F<>r die Startseite ist die Kurzbeschreibung ideal
|
||||
SKU = p.SKU,
|
||||
Price = p.Price,
|
||||
IsActive = p.IsActive,
|
||||
IsInStock = p.IsInStock,
|
||||
StockQuantity = p.StockQuantity,
|
||||
Slug = p.Slug,
|
||||
categories = p.Productcategories.Select(pc => new CategorieDto
|
||||
Id = product.Id,
|
||||
Name = product.Name,
|
||||
Description = useShortDescription ? product.ShortDescription : product.Description,
|
||||
Price = product.Price,
|
||||
OldPrice = product.OldPrice,
|
||||
SKU = product.SKU,
|
||||
IsInStock = product.IsInStock,
|
||||
StockQuantity = product.StockQuantity,
|
||||
Slug = product.Slug,
|
||||
categories = product.Productcategories.Select(pc => new CategorieDto
|
||||
{
|
||||
Id = pc.categorie.Id,
|
||||
Name = pc.categorie.Name,
|
||||
Slug = pc.categorie.Slug
|
||||
}).ToList(),
|
||||
Images = p.Images.OrderBy(i => i.DisplayOrder).Select(img => new ProductImageDto
|
||||
Images = product.Images.OrderBy(i => i.DisplayOrder).Select(img => new ProductImageDto
|
||||
{
|
||||
Id = img.Id,
|
||||
Url = img.Url,
|
||||
IsMainImage = img.IsMainImage,
|
||||
DisplayOrder = img.DisplayOrder
|
||||
}).ToList()
|
||||
}).ToList();
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user