add supplier attributes

This commit is contained in:
Jan-Lukas Pagel 2023-06-12 13:13:40 +02:00
parent 9cb007399a
commit 73bfcccc9c
15 changed files with 218 additions and 46 deletions

View File

@ -21,6 +21,9 @@ builder.Services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApiInfo { Title =
builder.Services.AddScoped<ICustomerService, CustomerService>();
builder.Services.AddScoped<ICustomerRepository, CustomerRepository>();
builder.Services.AddScoped<ISupplierService, SupplierService>();
builder.Services.AddScoped<ISupplierRepository, SupplierRepository>();
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
builder.Services.AddDbContextPool<RepoDbContext>(builder =>

View File

@ -11,6 +11,11 @@ public class SupplierController : ControllerBase
public readonly ISupplierService _supplierService;
public SupplierController(ISupplierService supplierService)
{
_supplierService = supplierService;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<SupplierDto>>> GetSupplieres(CancellationToken cancellationToken)
{

View File

@ -0,0 +1,19 @@
using System.Runtime.Serialization;
namespace AB.Contracts
{
public enum CommunicationType
{
[EnumMember(Value = "Email")]
Email,
[EnumMember(Value = "Postal")]
Postal,
[EnumMember(Value = "Phone")]
Phone,
[EnumMember(Value = "Fax")]
Fax
}
}

View File

@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AB.Contracts;
namespace AB.Contracts;
public class ContactPersonDto
{
@ -17,5 +11,6 @@ public class ContactPersonDto
public string Email { get; set; }
public string Notes { get; set; }
}

View File

@ -1,24 +0,0 @@
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,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace AB.Contracts;
@ -11,4 +7,21 @@ public class SupplierDto
public Guid Id { get; set; }
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 CommunicationType PreferredCommunication { get; set; }
public IEnumerable<ContactPersonDto> ContactPersons { get; set; }
}

View File

@ -18,7 +18,7 @@ public class SupplierForCreationDto
public string TaxId { get; set; }
[DataMember(Name = "PreferredCommunication", EmitDefaultValue = true)]
public PreferredCommunicationType PreferredCommunication { get; set; }
public CommunicationType PreferredCommunication { get; set; }
public IEnumerable<ContactPersonDto> ContactPersons { get; set; }

View File

@ -0,0 +1,11 @@
namespace AB.Domain.Enums
{
public enum CommunicationType
{
Email,
Postal,
Phone,
Fax
}
}

View File

@ -0,0 +1,20 @@
namespace AB.Domain.Entities;
public class ContactPerson
{
public Guid ContactPersonId { get; set; }
public Guid SupplierId { get; set; }
public string Salutation { get; set; }
public string Name { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }
public string Notes { get; set; }
}

View File

@ -1,4 +1,5 @@
using System;
using AB.Domain.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -10,4 +11,20 @@ public class Supplier
{
public Guid SupplierId { get; set; }
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; }
public CommunicationType PreferredCommunication { get; set; }
public List<ContactPerson> ContactPersons { get; set; }
}

View File

@ -15,6 +15,8 @@ public sealed class RepoDbContext : DbContext
public DbSet<Supplier> Suppliers { get; set; }
public DbSet<ContactPerson> ContactPersons { get; set; }
public DbSet<Product> Products { get; set; }
}

View File

@ -20,12 +20,12 @@ public class SupplierRepository : ISupplierRepository
public async Task<IEnumerable<Supplier>> GetAllAsync(CancellationToken cancellationToken)
{
return await _dbContext.Suppliers.ToListAsync(cancellationToken);
return await _dbContext.Suppliers.Include(x => x.ContactPersons).ToListAsync(cancellationToken);
}
public async Task<Supplier> GetByIdAsync(Guid supplierId, CancellationToken cancellationToken)
{
return await _dbContext.Suppliers.FirstOrDefaultAsync(x => x.SupplierId == supplierId, cancellationToken);
return await _dbContext.Suppliers.Include(x => x.ContactPersons).FirstOrDefaultAsync(x => x.SupplierId == supplierId, cancellationToken);
}
public void Insert(Supplier supplier)

View File

@ -0,0 +1,54 @@
using AB.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AB.Services.Converter;
internal class CommunicationTypeConverter
{
public static CommunicationType ConvertFromBusinessValue
(Domain.Enums.CommunicationType communicationType)
{
switch (communicationType)
{
case Domain.Enums.CommunicationType.Email:
return CommunicationType.Email;
case Domain.Enums.CommunicationType.Phone:
return CommunicationType.Phone;
case Domain.Enums.CommunicationType.Postal:
return CommunicationType.Postal;
case Domain.Enums.CommunicationType.Fax:
return CommunicationType.Fax;
default:
var ex = new ArgumentException(
$"Invalid Type of {nameof(Domain.Enums.CommunicationType)}: {communicationType}",
nameof(communicationType));
throw ex;
}
}
public static Domain.Enums.CommunicationType ConvertToBusinessValue
(CommunicationType communicationType)
{
switch (communicationType)
{
case CommunicationType.Email:
return Domain.Enums.CommunicationType.Email;
case CommunicationType.Postal:
return Domain.Enums.CommunicationType.Postal;
case CommunicationType.Fax:
return Domain.Enums.CommunicationType.Fax;
case CommunicationType.Phone:
return Domain.Enums.CommunicationType.Phone;
default:
var ex = new ArgumentException(
$"Invalid Type of {nameof(Domain.Enums.CommunicationType)}: {communicationType}",
nameof(communicationType));
throw ex;
}
}
}

View File

@ -0,0 +1,44 @@
using AB.Contracts;
using AB.Domain.Entities;
namespace AB.Services.Converter;
internal static class ContactPersonConverter
{
public static ContactPersonDto ConvertToDto(this ContactPerson contactPerson)
{
var dto = new ContactPersonDto
{
Salutation = contactPerson.Salutation,
Name = contactPerson.Name,
Email = contactPerson.Email,
PhoneNumer = contactPerson.PhoneNumber,
Notes = contactPerson.Notes,
};
return dto;
}
public static ContactPerson ConvertToBusinessType(this ContactPersonDto dto)
{
var contactPerson = new ContactPerson
{
Salutation = dto.Salutation,
Name = dto.Name,
Email = dto.Email,
PhoneNumber = dto.PhoneNumer,
Notes = dto.Notes,
};
return contactPerson;
}
public static List<ContactPerson> ConvertToContactPersonList(this IEnumerable<ContactPersonDto> contactPersons)
{
return contactPersons.Select(x => x.ConvertToBusinessType()).ToList();
}
public static IEnumerable<ContactPersonDto> ConvertToContactPersonDtoList(this List<ContactPerson> contactPersons)
{
return contactPersons.Select(x => x.ConvertToDto());
}
}

View File

@ -3,11 +3,7 @@ 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;
using AB.Services.Converter;
namespace AB.Services
{
@ -28,7 +24,14 @@ namespace AB.Services
{
var supplier = new Supplier
{
Salutation = supplierForCreation.Salutation,
Name1 = supplierForCreation.Name1,
Name2 = supplierForCreation.Name2,
Email = supplierForCreation.Email,
PhoneNumber = supplierForCreation.PhoneNumber,
TaxId = supplierForCreation.TaxId,
PreferredCommunication = CommunicationTypeConverter.ConvertToBusinessValue(supplierForCreation.PreferredCommunication),
ContactPersons = supplierForCreation.ContactPersons.ConvertToContactPersonList(),
};
_supplierRepository.Insert(supplier);
@ -80,8 +83,18 @@ namespace AB.Services
var supplierDto = new SupplierDto
{
Id = supplier.SupplierId,
Salutation = supplier.Salutation,
Name1 = supplier.Name1,
Name2 = supplier.Name2,
Email = supplier.Email,
PhoneNumber = supplier.PhoneNumber,
TaxId = supplier.TaxId,
PreferredCommunication = CommunicationTypeConverter.ConvertFromBusinessValue(supplier.PreferredCommunication),
ContactPersons = supplier.ContactPersons.ConvertToContactPersonDtoList(),
};
return supplierDto;
}
}
}