add put vor customer

This commit is contained in:
Lars Unruh 2023-06-08 12:36:57 +02:00
parent c805ce20a8
commit 7b1aaacfb5
6 changed files with 74 additions and 11 deletions

View File

@ -4,7 +4,6 @@ using AB.Persistence.Repos;
using AB.Services; using AB.Services;
using AB.Services.Abstractions; using AB.Services.Abstractions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);

View File

@ -48,4 +48,13 @@ public class CustomerController : ControllerBase
return NoContent(); return NoContent();
} }
[HttpPut("{customerId:guid}")]
public async Task<ActionResult<CustomerDto>> UpdateCustomer(Guid customerId, CustomerForUpdateDto customerForUpdate, CancellationToken cancellationToken)
{
var customerDto = await _customerService.UpdateAsync(customerId, customerForUpdate, cancellationToken);
return Ok(customerDto);
}
} }

View File

@ -1,9 +1,4 @@
using System; using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AB.Contracts; namespace AB.Contracts;

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AB.Contracts
{
public class CustomerForUpdateDto
{
public string? Salutaion { get; set; }
public string? Name1 { get; set; }
public string? Name2 { get; set; }
public string? Email { get; set; }
public string? Iban { get; set; }
public string? PhoneNumber { get; set; }
}
}

View File

@ -13,4 +13,5 @@ public interface ICustomerService
Task DeleteAsync(Guid customerId, CancellationToken cancellationToken); Task DeleteAsync(Guid customerId, CancellationToken cancellationToken);
Task<IEnumerable<CustomerDto>> GetAllAsync(CancellationToken cancellationToken); Task<IEnumerable<CustomerDto>> GetAllAsync(CancellationToken cancellationToken);
Task<CustomerDto> GetByIdAsync(Guid customerId, CancellationToken cancellationToken); Task<CustomerDto> GetByIdAsync(Guid customerId, CancellationToken cancellationToken);
Task<CustomerDto> UpdateAsync(Guid customerId, CustomerForUpdateDto customerForUpdate, CancellationToken cancellationToken);
} }

View File

@ -82,6 +82,29 @@ public class CustomerService : ICustomerService
return customerDto; return customerDto;
} }
public async Task<CustomerDto> UpdateAsync(Guid customerId, CustomerForUpdateDto customerForUpdate, CancellationToken cancellationToken)
{
var customer = await _customerRepository.GetByIdAsync(customerId, cancellationToken);
if (customer is null)
{
throw new BusinessPartnerNotFoundException(customerId);
}
SetIfContains(customerForUpdate.Salutaion, customer, nameof(customer.Salutation));
SetIfContains(customerForUpdate.Name1, customer, nameof(customer.Name1));
SetIfContains(customerForUpdate.Name2, customer, nameof(customer.Name2));
SetIfContains(customerForUpdate.Email, customer, nameof(customer.Email));
SetIfContains(customerForUpdate.Iban, customer, nameof(customer.Iban));
SetIfContains(customerForUpdate.PhoneNumber, customer, nameof(customer.PhoneNumber));
await _unitOfWork.SaveChangesAsync(cancellationToken);
var customerDto = ConvertToCustomerDto(customer);
return customerDto;
}
private static CustomerDto ConvertToCustomerDto(Customer customer) private static CustomerDto ConvertToCustomerDto(Customer customer)
{ {
var customerDto = new CustomerDto var customerDto = new CustomerDto
@ -96,4 +119,14 @@ public class CustomerService : ICustomerService
}; };
return customerDto; return customerDto;
} }
public static void SetIfContains<T>(T value, Customer customer, string propertyName)
{
if (value is not null)
{
customer.GetType().GetProperty(propertyName).SetValue(customer, value);
}
}
} }