add put vor customer
This commit is contained in:
parent
c805ce20a8
commit
7b1aaacfb5
@ -4,7 +4,6 @@ using AB.Persistence.Repos;
|
||||
using AB.Services;
|
||||
using AB.Services.Abstractions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.OpenApi.Models;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
@ -48,4 +48,13 @@ public class CustomerController : ControllerBase
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,9 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace AB.Contracts;
|
||||
|
||||
|
26
AB.Contracts/CustomerForUpdateDto.cs
Normal file
26
AB.Contracts/CustomerForUpdateDto.cs
Normal 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; }
|
||||
|
||||
}
|
||||
}
|
@ -13,4 +13,5 @@ public interface ICustomerService
|
||||
Task DeleteAsync(Guid customerId, CancellationToken cancellationToken);
|
||||
Task<IEnumerable<CustomerDto>> GetAllAsync(CancellationToken cancellationToken);
|
||||
Task<CustomerDto> GetByIdAsync(Guid customerId, CancellationToken cancellationToken);
|
||||
Task<CustomerDto> UpdateAsync(Guid customerId, CustomerForUpdateDto customerForUpdate, CancellationToken cancellationToken);
|
||||
}
|
||||
|
@ -82,6 +82,29 @@ public class CustomerService : ICustomerService
|
||||
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)
|
||||
{
|
||||
var customerDto = new CustomerDto
|
||||
@ -96,4 +119,14 @@ public class CustomerService : ICustomerService
|
||||
};
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user