add supplier repo and suppliercreationdto
This commit is contained in:
parent
7b1aaacfb5
commit
f5c030df01
@ -12,7 +12,7 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -5,12 +5,15 @@ using AB.Services;
|
||||
using AB.Services.Abstractions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// 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
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApiInfo { Title = "AB-API", Version = "v1", Description = "An Api for the AB-Application" }));
|
||||
|
@ -28,9 +28,9 @@ public class SupplierController : ControllerBase
|
||||
}
|
||||
|
||||
[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);
|
||||
}
|
||||
|
23
AB.Contracts/ContactPersonDto.cs
Normal file
23
AB.Contracts/ContactPersonDto.cs
Normal 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; }
|
||||
|
||||
}
|
||||
|
@ -16,5 +16,4 @@ public class CustomerDto
|
||||
public string Iban { get; set; }
|
||||
|
||||
public string PhoneNumber { get; set; }
|
||||
|
||||
}
|
||||
|
24
AB.Contracts/PreferredCommunicationType.cs
Normal file
24
AB.Contracts/PreferredCommunicationType.cs
Normal 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
|
||||
}
|
||||
}
|
@ -1,11 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace AB.Contracts;
|
||||
|
||||
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; }
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ namespace AB.Domain.Repositories;
|
||||
public interface ICustomerRepository
|
||||
{
|
||||
public Task<IEnumerable<Customer>> GetAllAsync(CancellationToken cancellationToken);
|
||||
Task<Customer> GetByIdAsync(Guid customerId, CancellationToken cancellationToken);
|
||||
void Insert(Customer customer);
|
||||
void Remove(Customer customer);
|
||||
public Task<Customer> GetByIdAsync(Guid customerId, CancellationToken cancellationToken);
|
||||
public void Insert(Customer customer);
|
||||
public void Remove(Customer customer);
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using AB.Domain.Entities;
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -1,10 +1,5 @@
|
||||
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;
|
||||
|
||||
|
@ -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.Linq;
|
||||
using System.Text;
|
||||
@ -6,9 +9,33 @@ using System.Threading.Tasks;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -4,8 +4,8 @@ namespace AB.Services.Abstractions;
|
||||
|
||||
public interface ISupplierService
|
||||
{
|
||||
Task<SupplierDto> CreateAsync(SupplierForCreationDto supplierForCreation);
|
||||
Task DeleteAsync(Guid supplierId, CancellationToken cancellationToken);
|
||||
Task<IEnumerable<SupplierDto>> GetAllAsync(CancellationToken cancellationToken);
|
||||
Task<SupplierDto> GetSupplierByIdAsync(Guid supplierId, CancellationToken cancellationToken);
|
||||
public Task<SupplierDto> CreateAsync(SupplierForCreationDto supplierForCreation, CancellationToken cancellationToken);
|
||||
public Task DeleteAsync(Guid supplierId, CancellationToken cancellationToken);
|
||||
public Task<IEnumerable<SupplierDto>> GetAllAsync(CancellationToken cancellationToken);
|
||||
public Task<SupplierDto> GetSupplierByIdAsync(Guid supplierId, CancellationToken cancellationToken);
|
||||
}
|
||||
|
@ -21,8 +21,6 @@ public class CustomerService : ICustomerService
|
||||
|
||||
public async Task<CustomerDto> CreateAsync(CustomerForCreationDto customerForCreation, CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
|
||||
var customer = new Customer
|
||||
{
|
||||
Salutation = customerForCreation.Salutaion,
|
||||
@ -46,7 +44,7 @@ public class CustomerService : ICustomerService
|
||||
{
|
||||
var customer = await _customerRepository.GetByIdAsync(customerId, cancellationToken);
|
||||
|
||||
if (customer == null)
|
||||
if (customer is null)
|
||||
{
|
||||
throw new BusinessPartnerNotFoundException(customerId);
|
||||
}
|
||||
@ -122,7 +120,7 @@ public class CustomerService : ICustomerService
|
||||
|
||||
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);
|
||||
|
||||
|
87
AB.Services/SupplierService.cs
Normal file
87
AB.Services/SupplierService.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user