diff --git a/Webshop.Api/Controllers/Admin/AdminAddressesController.cs b/Webshop.Api/Controllers/Admin/AdminAddressesController.cs new file mode 100644 index 0000000..8212971 --- /dev/null +++ b/Webshop.Api/Controllers/Admin/AdminAddressesController.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Webshop.Api.Controllers.Admin +{ + public class AdminAddressesController : Controller + { + public IActionResult Index() + { + return View(); + } + } +} diff --git a/Webshop.Api/Program.cs b/Webshop.Api/Program.cs index 3a6932a..381c798 100644 --- a/Webshop.Api/Program.cs +++ b/Webshop.Api/Program.cs @@ -156,6 +156,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); // Controller und API-Infrastruktur builder.Services.AddControllers() diff --git a/Webshop.Application/Services/Admin/AdminAddressService.cs b/Webshop.Application/Services/Admin/AdminAddressService.cs new file mode 100644 index 0000000..acfcbc2 --- /dev/null +++ b/Webshop.Application/Services/Admin/AdminAddressService.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Webshop.Application; +using Webshop.Application.DTOs.Customers; +using Webshop.Application.Services.Admin.Interfaces; +using Webshop.Domain.Entities; +using Webshop.Domain.Interfaces; + +namespace Webshop.Application.Services.Admin +{ + public class AdminAddressService : IAdminAddressService + { + private readonly IAddressRepository _addressRepository; + private readonly ICustomerRepository _customerRepository; + + public AdminAddressService(IAddressRepository addressRepository, ICustomerRepository customerRepository) + { + _addressRepository = addressRepository; + _customerRepository = customerRepository; + } + + public async Task>> GetAllAddressesAsync() + { + // Hinweis: Um alle Adressen zu bekommen, brauchen wir eine neue Repository-Methode + var addresses = await _addressRepository.GetAllAsync(); + return ServiceResult.Ok(addresses.Select(MapToDto)); + } + + public async Task> GetAddressByIdAsync(Guid addressId) + { + var address = await _addressRepository.GetByIdAsync(addressId); + if (address == null) + { + return ServiceResult.Fail(ServiceResultType.NotFound, $"Adresse mit ID '{addressId}' nicht gefunden."); + } + return ServiceResult.Ok(MapToDto(address)); + } + + public async Task> CreateAddressForCustomerAsync(CreateAddressDto addressDto, Guid customerId) + { + var customer = await _customerRepository.GetByIdAsync(customerId); + if (customer == null) + { + return ServiceResult.Fail(ServiceResultType.NotFound, $"Kunde mit ID '{customerId}' nicht gefunden."); + } + + var newAddress = new Address + { + CustomerId = customer.Id, + FirstName = customer.FirstName, + LastName = customer.LastName, + Street = addressDto.Street, + HouseNumber = addressDto.HouseNumber, + City = addressDto.City, + PostalCode = addressDto.PostalCode, + Country = addressDto.Country, + Type = addressDto.Type + }; + + await _addressRepository.AddAsync(newAddress); + return ServiceResult.Ok(MapToDto(newAddress)); + } + + public async Task UpdateAddressAsync(UpdateAddressDto addressDto) + { + var address = await _addressRepository.GetByIdAsync(addressDto.Id); + if (address == null) + { + return ServiceResult.Fail(ServiceResultType.NotFound, "Adresse nicht gefunden."); + } + + // Hier keine Berechtigungsprüfung, da der Admin alle Adressen bearbeiten darf. + address.FirstName = addressDto.FirstName; + address.LastName = addressDto.LastName; + address.Street = addressDto.Street; + address.HouseNumber = addressDto.HouseNumber; + address.City = addressDto.City; + address.PostalCode = addressDto.PostalCode; + address.Country = addressDto.Country; + address.Type = addressDto.Type; + + await _addressRepository.UpdateAsync(address); + return ServiceResult.Ok(); + } + + public async Task DeleteAddressAsync(Guid addressId) + { + var address = await _addressRepository.GetByIdAsync(addressId); + if (address == null) + { + return ServiceResult.Fail(ServiceResultType.NotFound, "Adresse nicht gefunden."); + } + + await _addressRepository.DeleteAsync(addressId); + return ServiceResult.Ok(); + } + + private AddressDto MapToDto(Address address) + { + return new AddressDto + { + Id = address.Id, + FirstName = address.FirstName, + LastName = address.LastName, + Street = address.Street, + HouseNumber = address.HouseNumber, + City = address.City, + PostalCode = address.PostalCode, + Country = address.Country, + Type = address.Type + }; + } + } +} \ No newline at end of file diff --git a/Webshop.Application/Services/Admin/Interfaces/IAdminAddressService.cs b/Webshop.Application/Services/Admin/Interfaces/IAdminAddressService.cs new file mode 100644 index 0000000..2217a74 --- /dev/null +++ b/Webshop.Application/Services/Admin/Interfaces/IAdminAddressService.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Webshop.Application; +using Webshop.Application.DTOs.Customers; + +namespace Webshop.Application.Services.Admin.Interfaces +{ + public interface IAdminAddressService + { + Task>> GetAllAddressesAsync(); + Task> GetAddressByIdAsync(Guid addressId); + // Admins erstellen Adressen typischerweise im Kontext eines Kunden + Task> CreateAddressForCustomerAsync(CreateAddressDto addressDto, Guid customerId); + Task UpdateAddressAsync(UpdateAddressDto addressDto); + Task DeleteAddressAsync(Guid addressId); + } +} \ No newline at end of file diff --git a/Webshop.Domain/Interfaces/IAddressRepository.cs b/Webshop.Domain/Interfaces/IAddressRepository.cs index 8f027e3..ca052db 100644 --- a/Webshop.Domain/Interfaces/IAddressRepository.cs +++ b/Webshop.Domain/Interfaces/IAddressRepository.cs @@ -13,5 +13,6 @@ namespace Webshop.Domain.Interfaces Task AddAsync(Address address); Task UpdateAsync(Address address); Task DeleteAsync(Guid id); + Task> GetAllAsync(); } } \ No newline at end of file diff --git a/Webshop.Infrastructure/Repositories/AddressRepository.cs b/Webshop.Infrastructure/Repositories/AddressRepository.cs index d0b99bd..07af56d 100644 --- a/Webshop.Infrastructure/Repositories/AddressRepository.cs +++ b/Webshop.Infrastructure/Repositories/AddressRepository.cs @@ -52,5 +52,9 @@ namespace Webshop.Infrastructure.Repositories await _context.SaveChangesAsync(); } } + public async Task> GetAllAsync() + { + return await _context.Addresses.ToListAsync(); + } } } \ No newline at end of file