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.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<ICustomerService, CustomerService>();
builder.Services.AddScoped<ICustomerRepository, CustomerRepository>();

View File

@ -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);
}
}

View File

@ -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;

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<IEnumerable<CustomerDto>> GetAllAsync(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;
}
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);
}
}
}