Commit 1fcab024 authored by hasan khaddour's avatar hasan khaddour

Add Customer Domain Clontrollers.

parent 23f45a3d
...@@ -9,6 +9,10 @@ using MediatR; ...@@ -9,6 +9,10 @@ using MediatR;
using PSManagement.Contracts.Customers.Requests; using PSManagement.Contracts.Customers.Requests;
using PSManagement.Application.Customers.UseCases.Commands.CreateCustomer; using PSManagement.Application.Customers.UseCases.Commands.CreateCustomer;
using PSManagement.Domain.Customers.ValueObjects; using PSManagement.Domain.Customers.ValueObjects;
using AutoMapper;
using PSManagement.Application.Customers.UseCases.Commands.AddContactInfo;
using PSManagement.Application.Customers.UseCases.Commands.DeleteCustomer;
using PSManagement.Application.Customers.UseCases.Commands.UpdateCustomer;
namespace PSManagement.Api.Controllers.Customers namespace PSManagement.Api.Controllers.Customers
{ {
...@@ -18,21 +22,57 @@ namespace PSManagement.Api.Controllers.Customers ...@@ -18,21 +22,57 @@ namespace PSManagement.Api.Controllers.Customers
public class CustomersController : ControllerBase public class CustomersController : ControllerBase
{ {
private readonly IMediator _sender; private readonly IMediator _sender;
private readonly IMapper _mapper;
public CustomersController(IMediator sender) public CustomersController(IMediator sender, IMapper mapper )
{ {
_sender = sender; _sender = sender;
_mapper = mapper;
} }
[HttpPost] [HttpPost]
public async Task<IActionResult> CreateCustomer(CreateCustomerRequest request) public async Task<IActionResult> CreateCustomer(CreateCustomerRequest request)
{ {
Address address = new Address(request.City,request.StreetName,request.ZipCode,request.StreetNumber); var command = _mapper.Map<CreateCustomerCommand>(request);
var command = new CreateCustomerCommand(request.CustomerName,address);
var result = await _sender.Send(command); var result = await _sender.Send(command);
return Ok(result); return Ok(result);
} }
[HttpDelete]
public async Task<IActionResult> DeleteCustomer(DeleteCustomerRequest request)
{
var command = _mapper.Map<DeleteCustomerCommand>(request);
var result = await _sender.Send(command);
return Ok(result);
}
[HttpPut]
public async Task<IActionResult> UpdateCustomer(UpdateCustomerRequest request)
{
var command = _mapper.Map<UpdateCustomerCommand>(request);
var result = await _sender.Send(command);
return Ok(result);
}
[HttpPost("AddContactInfo")]
public async Task<IActionResult> AddContactInfo(AddContactInfoRequest request)
{
var command = _mapper.Map<AddContactInfoCommand>(request);
var result = await _sender.Send(command);
return Ok(result);
}
} }
} }
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PSManagement.Application.Contracts.Authentication;
using MediatR;
using System.Reflection;
using Microsoft.OpenApi.Models;
using System;
using AutoMapper;
using PSManagement.Api.Mappers;
namespace PSManagement.Api.DI
{
public static class DependencyInjection
{
public static IServiceCollection AddAPI(this IServiceCollection services)
{
services.AddControllers();
services.AddApiSwagger();
services.AddApiCors();
services.AddScoped<Mapper>();
services.AddAutoMapper(typeof(CustomerMapperConfiguration));
return services;
}
private static IServiceCollection AddApiSwagger(this IServiceCollection services)
{
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "PSManagement.Api", Version = "v1" });
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
{
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Scheme = "Bearer"
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
});
return services;
}
private static IServiceCollection AddApiCors(this IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowFrontend",
builder => builder
.WithOrigins("http://localhost:4200") // Add your frontend URL here
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials());
});
return services;
}
}
}
using AutoMapper;
using PSManagement.Application.Customers.Common;
using PSManagement.Application.Customers.UseCases.Commands.AddContactInfo;
using PSManagement.Application.Customers.UseCases.Commands.CreateCustomer;
using PSManagement.Application.Customers.UseCases.Commands.UpdateCustomer;
using PSManagement.Contracts.Customers.Requests;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PSManagement.Api.Mappers
{
public class CustomerMapperConfiguration : Profile
{
public CustomerMapperConfiguration()
{
CreateMap<CreateCustomerRequest, CreateCustomerCommand>().ReverseMap();
CreateMap<UpdateCustomerCommand, UpdateCustomerRequest>().ReverseMap();
CreateMap<AddContactInfoRequest, AddContactInfoCommand>().ReverseMap();
CreateMap<AddressRecord, AddressDTO>().ReverseMap();
}
}
}
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AutoMapper" Version="7.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="5.0.1" />
<PackageReference Include="MediatR" Version="5.1.0" /> <PackageReference Include="MediatR" Version="5.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.17"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.17">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
......
...@@ -14,6 +14,7 @@ using System.Collections.Generic; ...@@ -14,6 +14,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using PSManagement.Infrastructure.Persistence.DI; using PSManagement.Infrastructure.Persistence.DI;
using PSManagement.Api.DI;
namespace PSManagement.Api namespace PSManagement.Api
{ {
...@@ -32,50 +33,12 @@ namespace PSManagement.Api ...@@ -32,50 +33,12 @@ namespace PSManagement.Api
// adding dependency injection // adding dependency injection
services services
.AddAPI()
.AddPresentation() .AddPresentation()
.AddApplication() .AddApplication()
.AddInfrastructure(Configuration) .AddInfrastructure(Configuration)
.AddPersistence(Configuration); .AddPersistence(Configuration);
services.AddControllers();
services.AddCors(options =>
{
options.AddPolicy("AllowFrontend",
builder => builder
.WithOrigins("http://localhost:4200") // Add your frontend URL here
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials());
});
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "PSManagement.Api", Version = "v1" });
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
{
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Scheme = "Bearer"
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
});
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment