admin adress
This commit is contained in:
12
Webshop.Api/Controllers/Admin/AdminAddressesController.cs
Normal file
12
Webshop.Api/Controllers/Admin/AdminAddressesController.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace Webshop.Api.Controllers.Admin
|
||||||
|
{
|
||||||
|
public class AdminAddressesController : Controller
|
||||||
|
{
|
||||||
|
public IActionResult Index()
|
||||||
|
{
|
||||||
|
return View();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -156,6 +156,7 @@ builder.Services.AddScoped<IReviewService, ReviewService>();
|
|||||||
builder.Services.AddScoped<ICustomerReviewService, CustomerReviewService>();
|
builder.Services.AddScoped<ICustomerReviewService, CustomerReviewService>();
|
||||||
builder.Services.AddScoped<IAdminReviewService, AdminReviewService>();
|
builder.Services.AddScoped<IAdminReviewService, AdminReviewService>();
|
||||||
builder.Services.AddScoped<IAdminAnalyticsService, AdminAnalyticsService>();
|
builder.Services.AddScoped<IAdminAnalyticsService, AdminAnalyticsService>();
|
||||||
|
builder.Services.AddScoped<IAdminAddressService, AdminAddressService>();
|
||||||
|
|
||||||
// Controller und API-Infrastruktur
|
// Controller und API-Infrastruktur
|
||||||
builder.Services.AddControllers()
|
builder.Services.AddControllers()
|
||||||
|
|||||||
116
Webshop.Application/Services/Admin/AdminAddressService.cs
Normal file
116
Webshop.Application/Services/Admin/AdminAddressService.cs
Normal file
@@ -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<ServiceResult<IEnumerable<AddressDto>>> 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<ServiceResult<AddressDto>> GetAddressByIdAsync(Guid addressId)
|
||||||
|
{
|
||||||
|
var address = await _addressRepository.GetByIdAsync(addressId);
|
||||||
|
if (address == null)
|
||||||
|
{
|
||||||
|
return ServiceResult.Fail<AddressDto>(ServiceResultType.NotFound, $"Adresse mit ID '{addressId}' nicht gefunden.");
|
||||||
|
}
|
||||||
|
return ServiceResult.Ok(MapToDto(address));
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ServiceResult<AddressDto>> CreateAddressForCustomerAsync(CreateAddressDto addressDto, Guid customerId)
|
||||||
|
{
|
||||||
|
var customer = await _customerRepository.GetByIdAsync(customerId);
|
||||||
|
if (customer == null)
|
||||||
|
{
|
||||||
|
return ServiceResult.Fail<AddressDto>(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<ServiceResult> 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<ServiceResult> 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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<ServiceResult<IEnumerable<AddressDto>>> GetAllAddressesAsync();
|
||||||
|
Task<ServiceResult<AddressDto>> GetAddressByIdAsync(Guid addressId);
|
||||||
|
// Admins erstellen Adressen typischerweise im Kontext eines Kunden
|
||||||
|
Task<ServiceResult<AddressDto>> CreateAddressForCustomerAsync(CreateAddressDto addressDto, Guid customerId);
|
||||||
|
Task<ServiceResult> UpdateAddressAsync(UpdateAddressDto addressDto);
|
||||||
|
Task<ServiceResult> DeleteAddressAsync(Guid addressId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,5 +13,6 @@ namespace Webshop.Domain.Interfaces
|
|||||||
Task AddAsync(Address address);
|
Task AddAsync(Address address);
|
||||||
Task UpdateAsync(Address address);
|
Task UpdateAsync(Address address);
|
||||||
Task DeleteAsync(Guid id);
|
Task DeleteAsync(Guid id);
|
||||||
|
Task<IEnumerable<Address>> GetAllAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -52,5 +52,9 @@ namespace Webshop.Infrastructure.Repositories
|
|||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public async Task<IEnumerable<Address>> GetAllAsync()
|
||||||
|
{
|
||||||
|
return await _context.Addresses.ToListAsync();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user