add supplier repo and suppliercreationdto

This commit is contained in:
Jan-Lukas Pagel 2023-06-09 13:04:59 +02:00
parent 7b1aaacfb5
commit f5c030df01
14 changed files with 207 additions and 35 deletions

View File

@ -12,7 +12,7 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -5,12 +5,15 @@ using AB.Services;
using AB.Services.Abstractions; using AB.Services.Abstractions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using System.Text.Json.Serialization;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
builder.Services.AddControllers().AddApplicationPart(typeof(AB.API.AssemblyReference).Assembly); builder.Services.AddControllers().AddApplicationPart(typeof(AB.API.AssemblyReference).Assembly)
.AddJsonOptions(options => options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); 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" }));

View File

@ -28,9 +28,9 @@ public class SupplierController : ControllerBase
} }
[HttpPost] [HttpPost]
public async Task<CreatedAtActionResult> CreateSupplier([FromBody] SupplierForCreationDto supplierForCreation) public async Task<CreatedAtActionResult> CreateSupplier([FromBody] SupplierForCreationDto supplierForCreation, CancellationToken cancellationToken)
{ {
var supplierDto = await _supplierService.CreateAsync(supplierForCreation); var supplierDto = await _supplierService.CreateAsync(supplierForCreation, cancellationToken);
return CreatedAtAction(nameof(CreateSupplier), new { id = supplierDto.Id }, supplierDto); return CreatedAtAction(nameof(CreateSupplier), new { id = supplierDto.Id }, supplierDto);
} }

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AB.Contracts;
public class ContactPersonDto
{
public string Name { get; set; }
public string Salutation { get; set; }
public string PhoneNumer { get; set; }
public string Email { get; set; }
}

View File

@ -16,5 +16,4 @@ public class CustomerDto
public string Iban { get; set; } public string Iban { get; set; }
public string PhoneNumber { get; set; } public string PhoneNumber { get; set; }
} }

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace AB.Contracts
{
public enum PreferredCommunicationType
{
[EnumMember(Value = "Email")]
Email,
[EnumMember(Value = "Postal")]
Postal,
[EnumMember(Value = "Phone")]
Phone,
[EnumMember(Value = "Phone")]
Fax
}
}

View File

@ -1,11 +1,27 @@
using System; using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AB.Contracts; namespace AB.Contracts;
public class SupplierForCreationDto public class SupplierForCreationDto
{ {
public string Salutation { get; set; }
public string Name1 { get; set; }
public string Name2 { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string TaxId { get; set; }
[DataMember(Name = "PreferredCommunication", EmitDefaultValue = true)]
public PreferredCommunicationType PreferredCommunication { get; set; }
public IEnumerable<ContactPersonDto> ContactPersons { get; set; }
} }

View File

@ -10,7 +10,7 @@ namespace AB.Domain.Repositories;
public interface ICustomerRepository public interface ICustomerRepository
{ {
public Task<IEnumerable<Customer>> GetAllAsync(CancellationToken cancellationToken); public Task<IEnumerable<Customer>> GetAllAsync(CancellationToken cancellationToken);
Task<Customer> GetByIdAsync(Guid customerId, CancellationToken cancellationToken); public Task<Customer> GetByIdAsync(Guid customerId, CancellationToken cancellationToken);
void Insert(Customer customer); public void Insert(Customer customer);
void Remove(Customer customer); public void Remove(Customer customer);
} }

View File

@ -1,11 +1,11 @@
using System; using AB.Domain.Entities;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AB.Domain.Repositories; namespace AB.Domain.Repositories;
public class ISupplierRepository public interface ISupplierRepository
{ {
public Task<IEnumerable<Supplier>> GetAllAsync(CancellationToken cancellationToken);
public Task<Supplier> GetByIdAsync(Guid supplierId, CancellationToken cancellationToken);
public void Insert(Supplier supplier);
public void Remove(Supplier supplier);
} }

View File

@ -1,10 +1,5 @@
using AB.Domain.Entities; using AB.Domain.Entities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AB.Persistence; namespace AB.Persistence;

View File

@ -1,4 +1,7 @@
using System; using AB.Domain.Entities;
using AB.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -6,9 +9,33 @@ using System.Threading.Tasks;
namespace AB.Persistence.Repos; namespace AB.Persistence.Repos;
public class SupplierRepository public class SupplierRepository : ISupplierRepository
{
private readonly RepoDbContext _dbContext;
public SupplierRepository(RepoDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<IEnumerable<Supplier>> GetAllAsync(CancellationToken cancellationToken)
{
return await _dbContext.Suppliers.ToListAsync(cancellationToken);
}
public async Task<Supplier> GetByIdAsync(Guid supplierId, CancellationToken cancellationToken)
{
return await _dbContext.Suppliers.FirstOrDefaultAsync(x => x.SupplierId == supplierId, cancellationToken);
}
public void Insert(Supplier supplier)
{
_dbContext.Add(supplier);
}
public void Remove(Supplier supplier)
{ {
_dbContext.Suppliers.Remove(supplier);
}
} }

View File

@ -4,8 +4,8 @@ namespace AB.Services.Abstractions;
public interface ISupplierService public interface ISupplierService
{ {
Task<SupplierDto> CreateAsync(SupplierForCreationDto supplierForCreation); public Task<SupplierDto> CreateAsync(SupplierForCreationDto supplierForCreation, CancellationToken cancellationToken);
Task DeleteAsync(Guid supplierId, CancellationToken cancellationToken); public Task DeleteAsync(Guid supplierId, CancellationToken cancellationToken);
Task<IEnumerable<SupplierDto>> GetAllAsync(CancellationToken cancellationToken); public Task<IEnumerable<SupplierDto>> GetAllAsync(CancellationToken cancellationToken);
Task<SupplierDto> GetSupplierByIdAsync(Guid supplierId, CancellationToken cancellationToken); public Task<SupplierDto> GetSupplierByIdAsync(Guid supplierId, CancellationToken cancellationToken);
} }

View File

@ -21,8 +21,6 @@ public class CustomerService : ICustomerService
public async Task<CustomerDto> CreateAsync(CustomerForCreationDto customerForCreation, CancellationToken cancellationToken = default) public async Task<CustomerDto> CreateAsync(CustomerForCreationDto customerForCreation, CancellationToken cancellationToken = default)
{ {
var customer = new Customer var customer = new Customer
{ {
Salutation = customerForCreation.Salutaion, Salutation = customerForCreation.Salutaion,
@ -46,7 +44,7 @@ public class CustomerService : ICustomerService
{ {
var customer = await _customerRepository.GetByIdAsync(customerId, cancellationToken); var customer = await _customerRepository.GetByIdAsync(customerId, cancellationToken);
if (customer == null) if (customer is null)
{ {
throw new BusinessPartnerNotFoundException(customerId); throw new BusinessPartnerNotFoundException(customerId);
} }
@ -122,7 +120,7 @@ public class CustomerService : ICustomerService
public static void SetIfContains<T>(T value, Customer customer, string propertyName) public static void SetIfContains<T>(T value, Customer customer, string propertyName)
{ {
if (value is not null) if (value is not null && !string.IsNullOrWhiteSpace(propertyName))
{ {
customer.GetType().GetProperty(propertyName).SetValue(customer, value); customer.GetType().GetProperty(propertyName).SetValue(customer, value);

View File

@ -0,0 +1,87 @@
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
{
public class SupplierService : ISupplierService
{
private readonly ISupplierRepository _supplierRepository;
private readonly IUnitOfWork _unitOfWork;
public SupplierService(ISupplierRepository supplierRepository, IUnitOfWork unitOfWork)
{
_supplierRepository = supplierRepository;
_unitOfWork = unitOfWork;
}
public async Task<SupplierDto> CreateAsync(SupplierForCreationDto supplierForCreation, CancellationToken cancellationToken)
{
var supplier = new Supplier
{
};
_supplierRepository.Insert(supplier);
await _unitOfWork.SaveChangesAsync(cancellationToken);
var supplierDto = ConvertToSupplierDto(supplier);
return supplierDto;
}
public async Task DeleteAsync(Guid supplierId, CancellationToken cancellationToken)
{
var supplier = await _supplierRepository.GetByIdAsync(supplierId, cancellationToken);
if (supplier is null)
{
throw new BusinessPartnerNotFoundException(supplierId);
}
_supplierRepository.Remove(supplier);
await _unitOfWork.SaveChangesAsync(cancellationToken);
}
public async Task<IEnumerable<SupplierDto>> GetAllAsync(CancellationToken cancellationToken)
{
var supplierList = await _supplierRepository.GetAllAsync(cancellationToken);
var supplierDtoList = supplierList.Select(supplier =>
{
return ConvertToSupplierDto(supplier);
});
return supplierDtoList;
}
public async Task<SupplierDto> GetSupplierByIdAsync(Guid supplierId, CancellationToken cancellationToken)
{
var supplier = await _supplierRepository.GetByIdAsync(supplierId, cancellationToken);
var supplierDto = ConvertToSupplierDto(supplier);
return supplierDto;
}
private SupplierDto ConvertToSupplierDto(Supplier supplier)
{
var supplierDto = new SupplierDto
{
Id = supplier.SupplierId,
};
return supplierDto;
}
}
}