From 7b1aaacfb5d25fe0474a4d853fb0d6d729117184 Mon Sep 17 00:00:00 2001 From: Lars Unruh Date: Thu, 8 Jun 2023 12:36:57 +0200 Subject: [PATCH] add put vor customer --- AB-API/Program.cs | 3 +- .../BusinessPartner/CustomerController.cs | 9 +++++ AB.Contracts/CustomerForCreationDto.cs | 13 +++----- AB.Contracts/CustomerForUpdateDto.cs | 26 +++++++++++++++ AB.Services.Abstractions/ICustomerService.cs | 1 + AB.Services/CustomerService.cs | 33 +++++++++++++++++++ 6 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 AB.Contracts/CustomerForUpdateDto.cs diff --git a/AB-API/Program.cs b/AB-API/Program.cs index 5fa3f4f..26edc72 100644 --- a/AB-API/Program.cs +++ b/AB-API/Program.cs @@ -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); @@ -14,7 +13,7 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers().AddApplicationPart(typeof(AB.API.AssemblyReference).Assembly); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApiInfo { Title = "AB-API", Version = "v1" , Description = "An Api for the AB-Application"})); +builder.Services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApiInfo { Title = "AB-API", Version = "v1", Description = "An Api for the AB-Application" })); builder.Services.AddScoped(); builder.Services.AddScoped(); diff --git a/AB.API/Controllers/BusinessPartner/CustomerController.cs b/AB.API/Controllers/BusinessPartner/CustomerController.cs index a1af599..07baf92 100644 --- a/AB.API/Controllers/BusinessPartner/CustomerController.cs +++ b/AB.API/Controllers/BusinessPartner/CustomerController.cs @@ -48,4 +48,13 @@ public class CustomerController : ControllerBase return NoContent(); } + [HttpPut("{customerId:guid}")] + public async Task> UpdateCustomer(Guid customerId, CustomerForUpdateDto customerForUpdate, CancellationToken cancellationToken) + { + var customerDto = await _customerService.UpdateAsync(customerId, customerForUpdate, cancellationToken); + + + return Ok(customerDto); + } + } diff --git a/AB.Contracts/CustomerForCreationDto.cs b/AB.Contracts/CustomerForCreationDto.cs index 0edf220..c0f641f 100644 --- a/AB.Contracts/CustomerForCreationDto.cs +++ b/AB.Contracts/CustomerForCreationDto.cs @@ -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; @@ -11,16 +6,16 @@ public class CustomerForCreationDto { [Required] public string Salutaion { get; set; } - + [Required] 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; } diff --git a/AB.Contracts/CustomerForUpdateDto.cs b/AB.Contracts/CustomerForUpdateDto.cs new file mode 100644 index 0000000..0ffd0ee --- /dev/null +++ b/AB.Contracts/CustomerForUpdateDto.cs @@ -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; } + + } +} diff --git a/AB.Services.Abstractions/ICustomerService.cs b/AB.Services.Abstractions/ICustomerService.cs index 0bea1de..f6278d2 100644 --- a/AB.Services.Abstractions/ICustomerService.cs +++ b/AB.Services.Abstractions/ICustomerService.cs @@ -13,4 +13,5 @@ public interface ICustomerService Task DeleteAsync(Guid customerId, CancellationToken cancellationToken); Task> GetAllAsync(CancellationToken cancellationToken); Task GetByIdAsync(Guid customerId, CancellationToken cancellationToken); + Task UpdateAsync(Guid customerId, CustomerForUpdateDto customerForUpdate, CancellationToken cancellationToken); } diff --git a/AB.Services/CustomerService.cs b/AB.Services/CustomerService.cs index c7c04e5..8bb7e68 100644 --- a/AB.Services/CustomerService.cs +++ b/AB.Services/CustomerService.cs @@ -82,6 +82,29 @@ public class CustomerService : ICustomerService return customerDto; } + public async Task 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 value, Customer customer, string propertyName) + { + if (value is not null) + { + customer.GetType().GetProperty(propertyName).SetValue(customer, value); + + } + + } }