categorie
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
// src/Webshop.Api/Controllers/Public/categoriesController.cs
|
// src/Webshop.Api/Controllers/Public/categoriesController.cs
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Webshop.Application;
|
||||||
using Webshop.Application.DTOs.Categorie;
|
using Webshop.Application.DTOs.Categorie;
|
||||||
using Webshop.Application.Services.Public;
|
using Webshop.Application.Services.Public;
|
||||||
|
|
||||||
@@ -21,18 +23,26 @@ namespace Webshop.Api.Controllers.Public
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult<IEnumerable<CategorieDto>>> GetActivecategories()
|
[ProducesResponseType(typeof(IEnumerable<CategorieDto>), StatusCodes.Status200OK)]
|
||||||
|
public async Task<IActionResult> GetActivecategories()
|
||||||
{
|
{
|
||||||
var categories = await _categorieservice.GetAllActiveAsync();
|
var result = await _categorieservice.GetAllActiveAsync();
|
||||||
return Ok(categories);
|
return Ok(result.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("{slug}")]
|
[HttpGet("{slug}")]
|
||||||
public async Task<ActionResult<CategorieDto>> GetcategorieBySlug(string slug)
|
[ProducesResponseType(typeof(CategorieDto), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||||||
|
public async Task<IActionResult> GetcategorieBySlug(string slug)
|
||||||
{
|
{
|
||||||
var categorie = await _categorieservice.GetBySlugAsync(slug);
|
var result = await _categorieservice.GetBySlugAsync(slug);
|
||||||
if (categorie == null) return NotFound();
|
|
||||||
return Ok(categorie);
|
return result.Type switch
|
||||||
|
{
|
||||||
|
ServiceResultType.Success => Ok(result.Value),
|
||||||
|
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
||||||
|
_ => StatusCode(StatusCodes.Status500InternalServerError, "Ein unerwarteter Fehler ist aufgetreten.")
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,9 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Webshop.Application;
|
||||||
using Webshop.Application.DTOs.Categorie;
|
using Webshop.Application.DTOs.Categorie;
|
||||||
|
using Webshop.Domain.Entities;
|
||||||
using Webshop.Domain.Interfaces;
|
using Webshop.Domain.Interfaces;
|
||||||
|
|
||||||
namespace Webshop.Application.Services.Public
|
namespace Webshop.Application.Services.Public
|
||||||
@@ -16,14 +18,32 @@ namespace Webshop.Application.Services.Public
|
|||||||
_categorieRepository = categorieRepository;
|
_categorieRepository = categorieRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<CategorieDto>> GetAllActiveAsync()
|
public async Task<ServiceResult<IEnumerable<CategorieDto>>> GetAllActiveAsync()
|
||||||
{
|
{
|
||||||
var categories = await _categorieRepository.GetAllAsync();
|
var categories = await _categorieRepository.GetAllAsync();
|
||||||
|
|
||||||
// Hier könnte man eine Baumstruktur aufbauen, für den Anfang eine flache Liste
|
var activeCategories = categories
|
||||||
return categories
|
|
||||||
.Where(c => c.IsActive)
|
.Where(c => c.IsActive)
|
||||||
.Select(c => new CategorieDto
|
.Select(MapToDto)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
return ServiceResult.Ok<IEnumerable<CategorieDto>>(activeCategories);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ServiceResult<CategorieDto>> GetBySlugAsync(string slug)
|
||||||
|
{
|
||||||
|
var categorie = await _categorieRepository.GetBySlugAsync(slug);
|
||||||
|
if (categorie == null || !categorie.IsActive)
|
||||||
|
{
|
||||||
|
return ServiceResult.Fail<CategorieDto>(ServiceResultType.NotFound, $"Kategorie mit dem Slug '{slug}' wurde nicht gefunden oder ist nicht aktiv.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return ServiceResult.Ok(MapToDto(categorie));
|
||||||
|
}
|
||||||
|
|
||||||
|
private CategorieDto MapToDto(Categorie c)
|
||||||
|
{
|
||||||
|
return new CategorieDto
|
||||||
{
|
{
|
||||||
Id = c.Id,
|
Id = c.Id,
|
||||||
Name = c.Name,
|
Name = c.Name,
|
||||||
@@ -33,24 +53,6 @@ namespace Webshop.Application.Services.Public
|
|||||||
ImageUrl = c.ImageUrl,
|
ImageUrl = c.ImageUrl,
|
||||||
IsActive = c.IsActive,
|
IsActive = c.IsActive,
|
||||||
DisplayOrder = c.DisplayOrder
|
DisplayOrder = c.DisplayOrder
|
||||||
}).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<CategorieDto?> GetBySlugAsync(string slug)
|
|
||||||
{
|
|
||||||
var categorie = await _categorieRepository.GetBySlugAsync(slug);
|
|
||||||
if (categorie == null || !categorie.IsActive) return null;
|
|
||||||
|
|
||||||
return new CategorieDto
|
|
||||||
{
|
|
||||||
Id = categorie.Id,
|
|
||||||
Name = categorie.Name,
|
|
||||||
Slug = categorie.Slug,
|
|
||||||
Description = categorie.Description,
|
|
||||||
ParentcategorieId = categorie.ParentcategorieId,
|
|
||||||
ImageUrl = categorie.ImageUrl,
|
|
||||||
IsActive = categorie.IsActive,
|
|
||||||
DisplayOrder = categorie.DisplayOrder
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
// src/Webshop.Application/Services/Public/Icategorieservice.cs
|
// src/Webshop.Application/Services/Public/Icategorieservice.cs
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Webshop.Application;
|
||||||
using Webshop.Application.DTOs.Categorie;
|
using Webshop.Application.DTOs.Categorie;
|
||||||
|
|
||||||
namespace Webshop.Application.Services.Public
|
namespace Webshop.Application.Services.Public
|
||||||
{
|
{
|
||||||
public interface ICategorieService
|
public interface ICategorieService
|
||||||
{
|
{
|
||||||
Task<IEnumerable<CategorieDto>> GetAllActiveAsync();
|
Task<ServiceResult<IEnumerable<CategorieDto>>> GetAllActiveAsync();
|
||||||
Task<CategorieDto?> GetBySlugAsync(string slug);
|
Task<ServiceResult<CategorieDto>> GetBySlugAsync(string slug);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user