From c805ce20a89a634076f8eebf9e0db8bd9fd7753f Mon Sep 17 00:00:00 2001 From: Semikolon Date: Wed, 7 Jun 2023 14:47:00 +0200 Subject: [PATCH] working customer post, get and delete --- AB-API/AB.WEB.csproj | 6 ++ AB-API/Program.cs | 21 +++- .../BusinessPartner/CustomerController.cs | 51 ++++++++++ .../BusinessPartner/SupplierController.cs | 48 +++++++++ AB.API/Controllers/CustomerController.cs | 53 ---------- AB.Contracts/CustomerDto.cs | 25 +++-- AB.Contracts/CustomerForCreationDto.cs | 23 ++++- AB.Contracts/SupplierDto.cs | 14 +++ AB.Contracts/SupplierForCreationDto.cs | 7 +- AB.Domain/Entities/Customer.cs | 24 +++-- AB.Domain/Entities/Product.cs | 11 +-- AB.Domain/Entities/Supplier.cs | 11 +-- .../BusinessPartnerNotFoundException.cs | 13 ++- AB.Domain/Exceptions/NotFoundException.cs | 23 +++-- AB.Domain/Repositories/ICustomerRepository.cs | 14 ++- AB.Domain/Repositories/ISupplierRepository.cs | 7 +- AB.Domain/Repositories/IUnitOfWork.cs | 12 +-- AB.Persistence/AB.Persistence.csproj | 8 ++ AB.Persistence/RepoDbContext.cs | 25 +++++ AB.Persistence/Repos/CustomerRepository.cs | 36 +++++++ AB.Persistence/Repos/SupplierRepository.cs | 14 +++ AB.Persistence/Repos/UnitOfWork.cs | 19 ++++ AB.Services.Abstractions/ICustomerService.cs | 15 ++- AB.Services.Abstractions/ISupplierService.cs | 11 +++ AB.Services/AB.Services.csproj | 1 + AB.Services/CustomerService.cs | 98 +++++++++++++++++-- 26 files changed, 448 insertions(+), 142 deletions(-) create mode 100644 AB.API/Controllers/BusinessPartner/CustomerController.cs create mode 100644 AB.API/Controllers/BusinessPartner/SupplierController.cs delete mode 100644 AB.API/Controllers/CustomerController.cs create mode 100644 AB.Contracts/SupplierDto.cs create mode 100644 AB.Persistence/RepoDbContext.cs create mode 100644 AB.Persistence/Repos/CustomerRepository.cs create mode 100644 AB.Persistence/Repos/SupplierRepository.cs create mode 100644 AB.Persistence/Repos/UnitOfWork.cs create mode 100644 AB.Services.Abstractions/ISupplierService.cs diff --git a/AB-API/AB.WEB.csproj b/AB-API/AB.WEB.csproj index 732bbff..13d8260 100644 --- a/AB-API/AB.WEB.csproj +++ b/AB-API/AB.WEB.csproj @@ -8,11 +8,17 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/AB-API/Program.cs b/AB-API/Program.cs index 3450655..5fa3f4f 100644 --- a/AB-API/Program.cs +++ b/AB-API/Program.cs @@ -1,3 +1,12 @@ +using AB.Domain.Repositories; +using AB.Persistence; +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); // Add services to the container. @@ -5,7 +14,17 @@ 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(); +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(); + +builder.Services.AddScoped(); + +builder.Services.AddDbContextPool(builder => +{ + builder.UseInMemoryDatabase("test"); +}); var app = builder.Build(); diff --git a/AB.API/Controllers/BusinessPartner/CustomerController.cs b/AB.API/Controllers/BusinessPartner/CustomerController.cs new file mode 100644 index 0000000..a1af599 --- /dev/null +++ b/AB.API/Controllers/BusinessPartner/CustomerController.cs @@ -0,0 +1,51 @@ +using AB.Contracts; +using AB.Services.Abstractions; +using Microsoft.AspNetCore.Mvc; + +namespace AB.API.Controllers.BusinessPartner; + +[ApiController] +[Route("api/customers")] +public class CustomerController : ControllerBase +{ + + private readonly ICustomerService _customerService; + + public CustomerController(ICustomerService customerService) + { + _customerService = customerService; + } + + [HttpGet] + public async Task>> GetCustomers(CancellationToken cancellationToken) + { + var customers = await _customerService.GetAllAsync(cancellationToken); + + return Ok(customers); + } + + [HttpGet("{customerId:guid}")] + public async Task> GetCustomerById(Guid customerId, CancellationToken cancellationToken) + { + var customer = await _customerService.GetByIdAsync(customerId, cancellationToken); + + return Ok(customer); + } + + [HttpPost] + public async Task CreateCustomer([FromBody] CustomerForCreationDto customerForCreation) + { + var customerDto = await _customerService.CreateAsync(customerForCreation); + + return CreatedAtAction(nameof(CreateCustomer), new { customerId = customerDto.Id }, customerDto); + } + + [HttpDelete("{customerId:guid}")] + public async Task DeleteCustomer(Guid customerId, CancellationToken cancellationToken) + { + await _customerService.DeleteAsync(customerId, cancellationToken); + + return NoContent(); + } + +} diff --git a/AB.API/Controllers/BusinessPartner/SupplierController.cs b/AB.API/Controllers/BusinessPartner/SupplierController.cs new file mode 100644 index 0000000..b0abe21 --- /dev/null +++ b/AB.API/Controllers/BusinessPartner/SupplierController.cs @@ -0,0 +1,48 @@ +using AB.Contracts; +using AB.Services.Abstractions; +using Microsoft.AspNetCore.Mvc; + +namespace AB.API.Controllers.BusinessPartner; + +[ApiController] +[Route("api/suppliers")] +public class SupplierController : ControllerBase +{ + + public readonly ISupplierService _supplierService; + + [HttpGet] + public async Task>> GetSupplieres(CancellationToken cancellationToken) + { + var suppliers = await _supplierService.GetAllAsync(cancellationToken); + + return Ok(suppliers); + } + + [HttpGet("{supplierId:guid}")] + public async Task> GetSupplierById(Guid supplierId, CancellationToken cancellationToken) + { + var supplierDto = await _supplierService.GetSupplierByIdAsync(supplierId, cancellationToken); + + return Ok(supplierDto); + } + + [HttpPost] + public async Task CreateSupplier([FromBody] SupplierForCreationDto supplierForCreation) + { + var supplierDto = await _supplierService.CreateAsync(supplierForCreation); + + return CreatedAtAction(nameof(CreateSupplier), new { id = supplierDto.Id }, supplierDto); + } + + [HttpDelete("{supplierId:guid}")] + public async Task DeleteSupplier(Guid supplierId, CancellationToken cancellationToken) + { + await _supplierService.DeleteAsync(supplierId, cancellationToken); + + return NoContent(); + } + + + +} diff --git a/AB.API/Controllers/CustomerController.cs b/AB.API/Controllers/CustomerController.cs deleted file mode 100644 index 83438fc..0000000 --- a/AB.API/Controllers/CustomerController.cs +++ /dev/null @@ -1,53 +0,0 @@ -using AB.Contracts; -using AB.Services.Abstractions; -using Microsoft.AspNetCore.Mvc; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AB.API.Controllers -{ - [ApiController] - [Route("api/customers")] - public class CustomerController : ControllerBase - { - - private readonly ICustomerService _customerService; - - - [HttpGet] - public async Task GetCustomers(CancellationToken cancellationToken) - { - var customers = await _customerService.GetAllAsync(cancellationToken); - - return Ok(customers); - } - - [HttpGet("{customerId:guid}")] - public async Task GetCustomerById(Guid customerId, CancellationToken cancellationToken) - { - var customer = await _customerService.GetByIdAsync(customerId, cancellationToken); - - return Ok(customer); - } - - [HttpPost] - public async Task CreateCustomer([FromBody] CustomerForCreationDto customerForCreation) - { - var customerDto = await _customerService.CreateAsync(customerForCreation); - - return CreatedAtAction(nameof(CreateCustomer), new {customerId = customerDto.Id}, customerDto); - } - - [HttpDelete("{customerId:guid}")] - public async Task DeleteCustomer(Guid customerId, CancellationToken cancellationToken) - { - await _customerService.DeleteAsync(customerId, cancellationToken); - - return NoContent(); - } - - } -} diff --git a/AB.Contracts/CustomerDto.cs b/AB.Contracts/CustomerDto.cs index a29e822..b73c023 100644 --- a/AB.Contracts/CustomerDto.cs +++ b/AB.Contracts/CustomerDto.cs @@ -1,15 +1,20 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace AB.Contracts; -namespace AB.Contracts +public class CustomerDto { - public class CustomerDto - { - public Guid Id { get; set; } + public Guid Id { get; set; } + + 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.Contracts/CustomerForCreationDto.cs b/AB.Contracts/CustomerForCreationDto.cs index c10ad58..0edf220 100644 --- a/AB.Contracts/CustomerForCreationDto.cs +++ b/AB.Contracts/CustomerForCreationDto.cs @@ -1,12 +1,27 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace AB.Contracts +namespace AB.Contracts; + +public class CustomerForCreationDto { - 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/SupplierDto.cs b/AB.Contracts/SupplierDto.cs new file mode 100644 index 0000000..444ea3b --- /dev/null +++ b/AB.Contracts/SupplierDto.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AB.Contracts; + +public class SupplierDto +{ + + public Guid Id { get; set; } + +} diff --git a/AB.Contracts/SupplierForCreationDto.cs b/AB.Contracts/SupplierForCreationDto.cs index 46cb5e8..04c7b74 100644 --- a/AB.Contracts/SupplierForCreationDto.cs +++ b/AB.Contracts/SupplierForCreationDto.cs @@ -4,9 +4,8 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace AB.Contracts +namespace AB.Contracts; + +public class SupplierForCreationDto { - public class SupplierForCreationDto - { - } } diff --git a/AB.Domain/Entities/Customer.cs b/AB.Domain/Entities/Customer.cs index 3981bd5..379655f 100644 --- a/AB.Domain/Entities/Customer.cs +++ b/AB.Domain/Entities/Customer.cs @@ -4,11 +4,23 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace AB.Domain.Entities -{ - public class Customer - { - Guid coutomerId; +namespace AB.Domain.Entities; + +public class Customer +{ + + public Guid CustomerId { get; set; } + + public string Salutation { 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.Domain/Entities/Product.cs b/AB.Domain/Entities/Product.cs index f4d68b0..1b757a4 100644 --- a/AB.Domain/Entities/Product.cs +++ b/AB.Domain/Entities/Product.cs @@ -4,11 +4,10 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace AB.Domain.Entities -{ - public class Product - { - Guid productId; +namespace AB.Domain.Entities; + +public class Product +{ + public Guid ProductId { get; set; } - } } diff --git a/AB.Domain/Entities/Supplier.cs b/AB.Domain/Entities/Supplier.cs index 6ef5a7e..fc1a5ef 100644 --- a/AB.Domain/Entities/Supplier.cs +++ b/AB.Domain/Entities/Supplier.cs @@ -4,11 +4,10 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace AB.Domain.Entities -{ - public class Supplier - { +namespace AB.Domain.Entities; - Guid supplierId; - } +public class Supplier +{ + + public Guid SupplierId { get; set; } } diff --git a/AB.Domain/Exceptions/BusinessPartnerNotFoundException.cs b/AB.Domain/Exceptions/BusinessPartnerNotFoundException.cs index e19959e..9dc9d8e 100644 --- a/AB.Domain/Exceptions/BusinessPartnerNotFoundException.cs +++ b/AB.Domain/Exceptions/BusinessPartnerNotFoundException.cs @@ -4,14 +4,13 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace AB.Domain.Exceptions +namespace AB.Domain.Exceptions; + +public class BusinessPartnerNotFoundException : NotFoundException { - public class BusinessPartnerNotFoundException : NotFoundException - { - public BusinessPartnerNotFoundException(Guid businessPartnerId) - : base ($"The BusinessPartner with the indetifier {businessPartnerId} was not found.") - { } + public BusinessPartnerNotFoundException(Guid businessPartnerId) + : base ($"The BusinessPartner with the indetifier {businessPartnerId} was not found.") + { } - } } diff --git a/AB.Domain/Exceptions/NotFoundException.cs b/AB.Domain/Exceptions/NotFoundException.cs index 474d3cc..4b914a0 100644 --- a/AB.Domain/Exceptions/NotFoundException.cs +++ b/AB.Domain/Exceptions/NotFoundException.cs @@ -4,17 +4,16 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace AB.Domain.Exceptions -{ +namespace AB.Domain.Exceptions; - [Serializable] - public abstract class NotFoundException : Exception - { - public NotFoundException() { } - public NotFoundException(string message) : base(message) { } - public NotFoundException(string message, Exception inner) : base(message, inner) { } - protected NotFoundException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - } + +[Serializable] +public abstract class NotFoundException : Exception +{ + public NotFoundException() { } + public NotFoundException(string message) : base(message) { } + public NotFoundException(string message, Exception inner) : base(message, inner) { } + protected NotFoundException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } } diff --git a/AB.Domain/Repositories/ICustomerRepository.cs b/AB.Domain/Repositories/ICustomerRepository.cs index 1880083..5365510 100644 --- a/AB.Domain/Repositories/ICustomerRepository.cs +++ b/AB.Domain/Repositories/ICustomerRepository.cs @@ -1,12 +1,16 @@ -using System; +using AB.Domain.Entities; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace AB.Domain.Repositories +namespace AB.Domain.Repositories; + +public interface ICustomerRepository { - public class ICustomerRepository - { - } + public Task> GetAllAsync(CancellationToken cancellationToken); + Task GetByIdAsync(Guid customerId, CancellationToken cancellationToken); + void Insert(Customer customer); + void Remove(Customer customer); } diff --git a/AB.Domain/Repositories/ISupplierRepository.cs b/AB.Domain/Repositories/ISupplierRepository.cs index a18dae7..7cd292a 100644 --- a/AB.Domain/Repositories/ISupplierRepository.cs +++ b/AB.Domain/Repositories/ISupplierRepository.cs @@ -4,9 +4,8 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace AB.Domain.Repositories +namespace AB.Domain.Repositories; + +public class ISupplierRepository { - public class ISupplierRepository - { - } } diff --git a/AB.Domain/Repositories/IUnitOfWork.cs b/AB.Domain/Repositories/IUnitOfWork.cs index 201b165..329cc04 100644 --- a/AB.Domain/Repositories/IUnitOfWork.cs +++ b/AB.Domain/Repositories/IUnitOfWork.cs @@ -1,12 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace AB.Domain.Repositories; -namespace AB.Domain.Repositories +public interface IUnitOfWork { - public class IUnitOfWork - { - } + public Task SaveChangesAsync(CancellationToken cancellationToken); } diff --git a/AB.Persistence/AB.Persistence.csproj b/AB.Persistence/AB.Persistence.csproj index 132c02c..6afb474 100644 --- a/AB.Persistence/AB.Persistence.csproj +++ b/AB.Persistence/AB.Persistence.csproj @@ -6,4 +6,12 @@ enable + + + + + + + + diff --git a/AB.Persistence/RepoDbContext.cs b/AB.Persistence/RepoDbContext.cs new file mode 100644 index 0000000..28a886c --- /dev/null +++ b/AB.Persistence/RepoDbContext.cs @@ -0,0 +1,25 @@ +using AB.Domain.Entities; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AB.Persistence; + +public sealed class RepoDbContext : DbContext +{ + + public RepoDbContext(DbContextOptions options) + : base (options) + { + } + + public DbSet Customers { get; set; } + + public DbSet Suppliers { get; set; } + + public DbSet Products { get; set; } + +} diff --git a/AB.Persistence/Repos/CustomerRepository.cs b/AB.Persistence/Repos/CustomerRepository.cs new file mode 100644 index 0000000..87a6196 --- /dev/null +++ b/AB.Persistence/Repos/CustomerRepository.cs @@ -0,0 +1,36 @@ +using AB.Domain.Entities; +using AB.Domain.Repositories; +using Microsoft.EntityFrameworkCore; + +namespace AB.Persistence.Repos; + +public class CustomerRepository : ICustomerRepository +{ + + private readonly RepoDbContext _dbContext; + + public CustomerRepository(RepoDbContext dbContext) + { + _dbContext = dbContext; + } + + public async Task> GetAllAsync(CancellationToken cancellationToken) + { + return await _dbContext.Customers.ToListAsync(cancellationToken); + } + + public async Task GetByIdAsync(Guid customerId, CancellationToken cancellationToken) + { + return await _dbContext.Customers.FirstOrDefaultAsync(x => x.CustomerId == customerId, cancellationToken); + } + + public void Insert(Customer customer) + { + _dbContext.Customers.Add(customer); + } + + public void Remove(Customer customer) + { + _dbContext.Customers.Remove(customer); + } +} diff --git a/AB.Persistence/Repos/SupplierRepository.cs b/AB.Persistence/Repos/SupplierRepository.cs new file mode 100644 index 0000000..04d4a11 --- /dev/null +++ b/AB.Persistence/Repos/SupplierRepository.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AB.Persistence.Repos; + +public class SupplierRepository +{ + + + +} diff --git a/AB.Persistence/Repos/UnitOfWork.cs b/AB.Persistence/Repos/UnitOfWork.cs new file mode 100644 index 0000000..a1df29a --- /dev/null +++ b/AB.Persistence/Repos/UnitOfWork.cs @@ -0,0 +1,19 @@ +using AB.Domain.Repositories; + +namespace AB.Persistence.Repos; + +public class UnitOfWork : IUnitOfWork +{ + + private readonly RepoDbContext _dbContext; + + public UnitOfWork(RepoDbContext dbContext) + { + _dbContext = dbContext; + } + + public Task SaveChangesAsync(CancellationToken cancellationToken) + { + return _dbContext.SaveChangesAsync(cancellationToken); + } +} diff --git a/AB.Services.Abstractions/ICustomerService.cs b/AB.Services.Abstractions/ICustomerService.cs index ce49d1c..0bea1de 100644 --- a/AB.Services.Abstractions/ICustomerService.cs +++ b/AB.Services.Abstractions/ICustomerService.cs @@ -5,13 +5,12 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace AB.Services.Abstractions +namespace AB.Services.Abstractions; + +public interface ICustomerService { - public interface ICustomerService - { - Task CreateAsync(CustomerForCreationDto customerForCreation); - Task DeleteAsync(Guid customerId, CancellationToken cancellationToken); - Task> GetAllAsync(CancellationToken cancellationToken); - Task GetByIdAsync(Guid customerId, CancellationToken cancellationToken); - } + Task CreateAsync(CustomerForCreationDto customerForCreation, CancellationToken cancellationToken = default); + Task DeleteAsync(Guid customerId, CancellationToken cancellationToken); + Task> GetAllAsync(CancellationToken cancellationToken); + Task GetByIdAsync(Guid customerId, CancellationToken cancellationToken); } diff --git a/AB.Services.Abstractions/ISupplierService.cs b/AB.Services.Abstractions/ISupplierService.cs new file mode 100644 index 0000000..e00decf --- /dev/null +++ b/AB.Services.Abstractions/ISupplierService.cs @@ -0,0 +1,11 @@ +using AB.Contracts; + +namespace AB.Services.Abstractions; + +public interface ISupplierService +{ + Task CreateAsync(SupplierForCreationDto supplierForCreation); + Task DeleteAsync(Guid supplierId, CancellationToken cancellationToken); + Task> GetAllAsync(CancellationToken cancellationToken); + Task GetSupplierByIdAsync(Guid supplierId, CancellationToken cancellationToken); +} diff --git a/AB.Services/AB.Services.csproj b/AB.Services/AB.Services.csproj index cc8a172..800ed3b 100644 --- a/AB.Services/AB.Services.csproj +++ b/AB.Services/AB.Services.csproj @@ -7,6 +7,7 @@ + diff --git a/AB.Services/CustomerService.cs b/AB.Services/CustomerService.cs index 3bff851..c7c04e5 100644 --- a/AB.Services/CustomerService.cs +++ b/AB.Services/CustomerService.cs @@ -1,15 +1,99 @@ using AB.Contracts; +using AB.Domain.Entities; +using AB.Domain.Exceptions; +using AB.Domain.Repositories; using AB.Services.Abstractions; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace AB.Services +namespace AB.Services; + +public class CustomerService : ICustomerService { - internal class CustomerService : ICustomerService + + private readonly ICustomerRepository _customerRepository; + + private readonly IUnitOfWork _unitOfWork; + + public CustomerService(ICustomerRepository customerRepository, IUnitOfWork unitOfWork) + { + _customerRepository = customerRepository; + _unitOfWork = unitOfWork; + } + + public async Task CreateAsync(CustomerForCreationDto customerForCreation, CancellationToken cancellationToken = default) { + + var customer = new Customer + { + Salutation = customerForCreation.Salutaion, + Name1 = customerForCreation.Name1, + Name2 = (customerForCreation.Name2 is null) ? string.Empty : customerForCreation.Name2, + Email = (customerForCreation.Email is null) ? string.Empty : customerForCreation.Email, + PhoneNumber = (customerForCreation.PhoneNumber is null) ? string.Empty : customerForCreation.PhoneNumber, + Iban = (customerForCreation.Iban is null) ? string.Empty : customerForCreation.Iban, + }; + + _customerRepository.Insert(customer); + + await _unitOfWork.SaveChangesAsync(cancellationToken); + + var customerDto = ConvertToCustomerDto(customer); + + return customerDto; + } + + public async Task DeleteAsync(Guid customerId, CancellationToken cancellationToken) + { + var customer = await _customerRepository.GetByIdAsync(customerId, cancellationToken); + + if (customer == null) + { + throw new BusinessPartnerNotFoundException(customerId); + } + + _customerRepository.Remove(customer); + + await _unitOfWork.SaveChangesAsync(cancellationToken); + } + + public async Task> GetAllAsync(CancellationToken cancellationToken) + { + var customers = await _customerRepository.GetAllAsync(cancellationToken); + + var customerDtoList = customers.Select(customer => + { + return ConvertToCustomerDto(customer); + }); + + return customerDtoList; + } + + public async Task GetByIdAsync(Guid customerId, CancellationToken cancellationToken) + { + var customer = await _customerRepository.GetByIdAsync(customerId, cancellationToken); + + if (customer is null) + { + throw new BusinessPartnerNotFoundException(customerId); + } + + var customerDto = ConvertToCustomerDto(customer); + + return customerDto; + } + + private static CustomerDto ConvertToCustomerDto(Customer customer) + { + var customerDto = new CustomerDto + { + Id = customer.CustomerId, + Salutaion = customer.Salutation, + Name1 = customer.Name1, + Name2 = customer.Name2, + Email = customer.Email, + PhoneNumber = customer.PhoneNumber, + Iban = customer.Iban, + }; + return customerDto; } }