Commit c6a1b9ab authored by hasan khaddour's avatar hasan khaddour

fix s .

parent 142bdb84
using FluentResults; 
using FluentResults;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using PSManagement.Application.Contracts.Authentication; using PSManagement.Application.Contracts.Authentication;
using PSManagement.Contracts.Authentication; using PSManagement.Contracts.Authentication;
...@@ -32,7 +33,7 @@ namespace PSManagement.Api.Controllers.Authentication ...@@ -32,7 +33,7 @@ namespace PSManagement.Api.Controllers.Authentication
return Ok(response); return Ok(response);
} }
return Problem(title: result.Errors[0].Message,detail:result.Errors[0].Reasons[0].Message,statusCode:400); return Problem(title: result.Errors[0].Message,detail:result.Errors[0].Reasons[0]?.Message,statusCode:401);
} }
[HttpPost("Register")] [HttpPost("Register")]
public async Task<IActionResult> Register([FromBody] RegisterRequest registerRequest) public async Task<IActionResult> Register([FromBody] RegisterRequest registerRequest)
...@@ -55,7 +56,7 @@ namespace PSManagement.Api.Controllers.Authentication ...@@ -55,7 +56,7 @@ namespace PSManagement.Api.Controllers.Authentication
return Ok(response); return Ok(response);
} }
return Problem(title: "An Errorr Occured "+result.Errors[0].Message , detail: result.Reasons[0].Message, statusCode: 400); return Problem(title: "An Errorr Occured " , detail:"", statusCode: 400);
} }
} }
......
...@@ -13,12 +13,16 @@ using AutoMapper; ...@@ -13,12 +13,16 @@ using AutoMapper;
using PSManagement.Application.Customers.UseCases.Commands.AddContactInfo; using PSManagement.Application.Customers.UseCases.Commands.AddContactInfo;
using PSManagement.Application.Customers.UseCases.Commands.DeleteCustomer; using PSManagement.Application.Customers.UseCases.Commands.DeleteCustomer;
using PSManagement.Application.Customers.UseCases.Commands.UpdateCustomer; using PSManagement.Application.Customers.UseCases.Commands.UpdateCustomer;
using PSManagement.Application.Customers.UseCases.Queries.ListAllCustomers;
using PSManagement.Contracts.Customers.Responses;
using FluentResults;
using PSManagement.Application.Customers.UseCases.Queries.GetCustomer;
namespace PSManagement.Api.Controllers.Customers namespace PSManagement.Api.Controllers.Customers
{ {
[Route("api/[controller]")] [Route("api/[controller]")]
[ApiController] [ApiController]
[Authorize] // [Authorize]
public class CustomersController : ControllerBase public class CustomersController : ControllerBase
{ {
private readonly IMediator _sender; private readonly IMediator _sender;
...@@ -30,6 +34,25 @@ namespace PSManagement.Api.Controllers.Customers ...@@ -30,6 +34,25 @@ namespace PSManagement.Api.Controllers.Customers
_mapper = mapper; _mapper = mapper;
} }
[HttpGet]
public async Task<IActionResult> ListCustomers()
{
var query = new ListAllCustomersQuery();
var result = _mapper.Map<Result<IEnumerable<CustomerRecord>>>( await _sender.Send(query));
return Ok(result);
}
[HttpGet("{id}")]
public async Task<IActionResult> GetCustomer(int id )
{
var query = new GetCustomerQuery(id);
var result = _mapper.Map<Result<CustomerRecord>>(await _sender.Send(query));
return Ok(result);
}
[HttpPost] [HttpPost]
public async Task<IActionResult> CreateCustomer(CreateCustomerRequest request) public async Task<IActionResult> CreateCustomer(CreateCustomerRequest request)
{ {
...@@ -50,9 +73,12 @@ namespace PSManagement.Api.Controllers.Customers ...@@ -50,9 +73,12 @@ namespace PSManagement.Api.Controllers.Customers
return Ok(result); return Ok(result);
} }
[HttpPut] [HttpPut("{id}")]
public async Task<IActionResult> UpdateCustomer(UpdateCustomerRequest request) public async Task<IActionResult> UpdateCustomer(int id ,UpdateCustomerRequest request)
{ {
if(id != request.CustomerId){
return Problem();
}
var command = _mapper.Map<UpdateCustomerCommand>(request); var command = _mapper.Map<UpdateCustomerCommand>(request);
var result = await _sender.Send(command); var result = await _sender.Send(command);
......
...@@ -21,7 +21,7 @@ namespace PSManagement.Api.DI ...@@ -21,7 +21,7 @@ namespace PSManagement.Api.DI
services.AddApiCors(); services.AddApiCors();
services.AddScoped<Mapper>(); services.AddScoped<Mapper>();
services.AddAutoMapper(typeof(CustomerMapperConfiguration)); services.AddAutoMapper(typeof(CustomerMapperConfiguration),typeof(MapperConfigurations));
return services; return services;
} }
......
...@@ -4,6 +4,8 @@ using PSManagement.Application.Customers.UseCases.Commands.AddContactInfo; ...@@ -4,6 +4,8 @@ using PSManagement.Application.Customers.UseCases.Commands.AddContactInfo;
using PSManagement.Application.Customers.UseCases.Commands.CreateCustomer; using PSManagement.Application.Customers.UseCases.Commands.CreateCustomer;
using PSManagement.Application.Customers.UseCases.Commands.UpdateCustomer; using PSManagement.Application.Customers.UseCases.Commands.UpdateCustomer;
using PSManagement.Contracts.Customers.Requests; using PSManagement.Contracts.Customers.Requests;
using PSManagement.Contracts.Customers.Responses;
using PSManagement.SharedKernel.Utilities;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
...@@ -20,7 +22,19 @@ namespace PSManagement.Api.Mappers ...@@ -20,7 +22,19 @@ namespace PSManagement.Api.Mappers
CreateMap<AddContactInfoRequest, AddContactInfoCommand>().ReverseMap(); CreateMap<AddContactInfoRequest, AddContactInfoCommand>().ReverseMap();
CreateMap<AddressRecord, AddressDTO>().ReverseMap(); CreateMap<AddressRecord, AddressDTO>().ReverseMap();
// CreateMap< IEnumerable<CustomerDTO>, ListCustomersResponse>().ma(src => src.Customers ,des => des.);
CreateMap<CustomerDTO, CustomerRecord>();
CreateMap<ContactInfoDTO, ContactInfoRecord>();
CreateMap<CustomerRecord, CustomerDTO>().ForMember(src =>src.ContactInfo , des => des.Ignore());
CreateMap<CustomerDTO, CustomerRecord>()
.ForMember(dest => dest.CustomerName, opt => opt.MapFrom(src => src.CustomerName))
.ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.Email))
.ForMember(dest => dest.Address, opt => opt.MapFrom(src => src.Address));
CreateMap<IEnumerable<CustomerRecord>, ListCustomersResponse>()
.ConstructUsing(src => new ListCustomersResponse(src));
} }
} }
} }
using AutoMapper;
using FluentResults;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PSManagement.Api.Mappers
{
public class MapperConfigurations :Profile
{
public MapperConfigurations()
{
CreateMap(typeof(Result<>), typeof(Result<>)).ConvertUsing(typeof(ResultToResultConverter<,>));
}
}
public class ResultToResultConverter<TSource, TDestination> : ITypeConverter<Result<TSource>, Result<TDestination>>
{
public Result<TDestination> Convert(Result<TSource> source, Result<TDestination> destination, ResolutionContext context)
{
if (source.IsSuccess)
{
var mappedValue = context.Mapper.Map<TDestination>(source.Value);
return Result.Ok(mappedValue);
}
else
{
return Result.Fail<TDestination>(source.Errors);
}
}
}
}
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="AutoMapper" Version="7.0.1" /> <PackageReference Include="AutoMapper" Version="7.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="5.0.1" /> <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="5.0.1" />
<PackageReference Include="MediatR" Version="5.1.0" /> <PackageReference Include="MediatR" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.17"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.17">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
......
using System;
namespace PSManagement.Contracts.Customers.Responses
{
public class ContactInfoRecord {
public String ContactType { get; set; }
public String ContactValue { get; set; }
}
}
using PSManagement.Contracts.Customers.Requests;
using System;
using System.Collections.Generic;
namespace PSManagement.Contracts.Customers.Responses
{
public class CustomerRecord {
public CustomerRecord()
{
}
public int Id { get; set; }
public String CustomerName { get; set; }
public String Email { get; set; }
public AddressRecord Address { get; set; }
public IEnumerable<ContactInfoRecord> ContactInfo { get; set; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSManagement.Contracts.Customers.Responses
{
public record ListCustomersResponse( IEnumerable<CustomerRecord> Customers );
}
...@@ -4,8 +4,4 @@ ...@@ -4,8 +4,4 @@
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Folder Include="Customers\Responses\" />
</ItemGroup>
</Project> </Project>
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PSManagement.Domain.Customers.Aggregate; using PSManagement.Domain.Customers.Aggregate;
using PSManagement.Domain.Departments.Aggregate;
using PSManagement.Domain.Employees.Aggregate;
using PSManagement.Domain.Identity.Aggregate; using PSManagement.Domain.Identity.Aggregate;
using PSManagement.Domain.Projects.Aggregate;
using PSManagement.Domain.Steps.Aggregate;
using PSManagement.Domain.Steps.Entities;
using PSManagement.Domain.Tracking;
using PSManagement.Domain.Tracking.Entities;
namespace PSManagement.Infrastructure.Persistence namespace PSManagement.Infrastructure.Persistence
{ {
...@@ -14,6 +21,19 @@ namespace PSManagement.Infrastructure.Persistence ...@@ -14,6 +21,19 @@ namespace PSManagement.Infrastructure.Persistence
public DbSet<User> Users { get; set; } public DbSet<User> Users { get; set; }
public DbSet<Customer> Customers { get; set; } public DbSet<Customer> Customers { get; set; }
public DbSet<Employee> Employees { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<Project> Projects { get; set; }
public DbSet<Step> Steps { get; set; }
public DbSet<Item> Items { get; set; }
public DbSet<Track> Tracks { get; set; }
public DbSet<EmployeeTrack> EmployeeTraks { get; set; }
public DbSet<StepTrack> StepTracks { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
......
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using PSManagement.Domain.Employees.Aggregate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSManagement.Infrastructure.Persistence.EntitiesConfiguration
{
public class EmployeeEntityConfiguration : IEntityTypeConfiguration<Employee>
{
public void Configure(EntityTypeBuilder<Employee> builder)
{
builder.OwnsOne(c => c.Availability,
p => {
p.Property(e => e.IsAvailable).HasColumnName("IsAvailable");
p.Property(e => e.WorkingHours).HasColumnName("WorkingHours");
}
);
builder.OwnsOne(c => c.PersonalInfo,
p => {
p.Property(e => e.LastName).HasColumnName("LastName");
p.Property(e => e.FirstName).HasColumnName("FirstName");
}
);
}
}
}
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using PSManagement.Domain.Steps.Entities;
namespace PSManagement.Infrastructure.Persistence.EntitiesConfiguration
{
public class ItemEntityConfiguration : IEntityTypeConfiguration<Item>
{
public void Configure(EntityTypeBuilder<Item> builder)
{
builder.OwnsOne(c => c.Price,
p => {
p.Property(e => e.Ammount).HasColumnName("Ammount");
p.Property(e => e.Currency).HasColumnName("Currency");
}
);
}
}
}
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using PSManagement.Domain.Projects.Aggregate;
namespace PSManagement.Infrastructure.Persistence.EntitiesConfiguration
{
public class ProjectEntityConfiguration : IEntityTypeConfiguration<Project>
{
public void Configure(EntityTypeBuilder<Project> builder)
{
builder.OwnsOne(c => c.ProjectAggreement,
p => {
p.Property(e => e.AggreementDate).HasColumnName("AggreementDate");
p.Property(e => e.AggreementNumber).HasColumnName("AggreementNumber");
}
);
builder.OwnsOne(c => c.ProjectInfo,
p => {
p.Property(e => e.Description).HasColumnName("Description");
p.Property(e => e.Code).HasColumnName("Code");
p.Property(e => e.Name).HasColumnName("Name");
}
);
builder.OwnsOne(c => c.ProposalInfo,
p => {
p.Property(e => e.ProposingBookDate).HasColumnName("ProposingBookDate");
p.Property(e => e.ProposingBookNumber).HasColumnName("ProposingBookNumber");
}
);
}
}
}
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using PSManagement.Domain.Steps.Aggregate;
namespace PSManagement.Infrastructure.Persistence.EntitiesConfiguration
{
public class StepEntityConfiguration : IEntityTypeConfiguration<Step>
{
public void Configure(EntityTypeBuilder<Step> builder)
{
}
}
}
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using PSManagement.Infrastructure.Persistence;
namespace PSManagement.Infrastructure.Persistence.Migrations
{
[DbContext(typeof(AppDbContext))]
[Migration("20240731070622_addCustomer4")]
partial class addCustomer4
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Relational:Collation", "Arabic_CI_AS")
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("ProductVersion", "5.0.17")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("PSManagement.Domain.Customers.Aggregate.Customer", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("CustomerName")
.HasColumnType("nvarchar(max)");
b.Property<string>("Email")
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Customers");
});
modelBuilder.Entity("PSManagement.Domain.Identity.Aggregate.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("Email")
.HasColumnType("nvarchar(max)");
b.Property<string>("FirstName")
.HasColumnType("nvarchar(max)");
b.Property<string>("HashedPassword")
.HasColumnType("nvarchar(max)");
b.Property<string>("LastName")
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Users");
});
modelBuilder.Entity("PSManagement.Domain.Customers.Aggregate.Customer", b =>
{
b.OwnsMany("PSManagement.Domain.Customers.Entities.ContactInfo", "ContactInfo", b1 =>
{
b1.Property<int>("CustomerId")
.HasColumnType("int");
b1.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b1.Property<string>("ContactType")
.HasColumnType("nvarchar(max)");
b1.Property<string>("ContactValue")
.HasColumnType("nvarchar(max)");
b1.HasKey("CustomerId", "Id");
b1.ToTable("ContactInfo");
b1.WithOwner()
.HasForeignKey("CustomerId");
});
b.OwnsOne("PSManagement.Domain.Customers.ValueObjects.Address", "Address", b1 =>
{
b1.Property<int>("CustomerId")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b1.Property<string>("City")
.HasColumnType("nvarchar(max)")
.HasColumnName("City");
b1.Property<string>("StreetName")
.HasColumnType("nvarchar(max)")
.HasColumnName("StreetName");
b1.Property<int>("StreetNumber")
.HasColumnType("int")
.HasColumnName("StreetNumber");
b1.Property<int>("ZipCode")
.HasColumnType("int")
.HasColumnName("ZipCode");
b1.HasKey("CustomerId");
b1.ToTable("Customers");
b1.WithOwner()
.HasForeignKey("CustomerId");
});
b.Navigation("Address");
b.Navigation("ContactInfo");
});
#pragma warning restore 612, 618
}
}
}
using Microsoft.EntityFrameworkCore.Migrations;
namespace PSManagement.Infrastructure.Persistence.Migrations
{
public partial class addCustomer4 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameColumn(
name: "ConatctValue",
table: "ContactInfo",
newName: "ContactValue");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameColumn(
name: "ContactValue",
table: "ContactInfo",
newName: "ConatctValue");
}
}
}
using FluentResults; 
using FluentResults;
using PSManagement.Application.Contracts.Authentication; using PSManagement.Application.Contracts.Authentication;
using PSManagement.Application.Contracts.Authorization; using PSManagement.Application.Contracts.Authorization;
using PSManagement.Domain.Customers.DomainErrors;
using PSManagement.Domain.Identity.Aggregate; using PSManagement.Domain.Identity.Aggregate;
using PSManagement.Domain.Identity.DomainErrors; using PSManagement.Domain.Identity.DomainErrors;
using PSManagement.Domain.Identity.Repositories; using PSManagement.Domain.Identity.Repositories;
//using PSManagement.SharedKernel.Utilities;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
...@@ -25,7 +27,7 @@ namespace PSManagement.Infrastructure.Services.Authentication ...@@ -25,7 +27,7 @@ namespace PSManagement.Infrastructure.Services.Authentication
User u = await _userRepository.GetByEmail(email); User u = await _userRepository.GetByEmail(email);
if (u is null || u.HashedPassword != password) { if (u is null || u.HashedPassword != password) {
return Result.Fail<AuthenticationResult>(new InvalidLoginDataError()); return Result.Fail<AuthenticationResult>(UserErrors.InvalidLoginAttempt);
} }
String token = _jwtTokenGenerator.GenerateToken(u.Id,u.FirstName,u.LastName,u.Email); String token = _jwtTokenGenerator.GenerateToken(u.Id,u.FirstName,u.LastName,u.Email);
...@@ -41,7 +43,7 @@ namespace PSManagement.Infrastructure.Services.Authentication ...@@ -41,7 +43,7 @@ namespace PSManagement.Infrastructure.Services.Authentication
// check if the user exist // check if the user exist
var u = await _userRepository.GetByEmail(email); var u = await _userRepository.GetByEmail(email);
if (u is not null) { if (u is not null) {
return Result.Fail<AuthenticationResult>(new AlreadyExistError()); return Result.Fail(UserErrors.AlreadyUserExist);
} }
var user = await _userRepository.AddAsync( var user = await _userRepository.AddAsync(
new User{ new User{
......
...@@ -7,8 +7,18 @@ using System.Threading.Tasks; ...@@ -7,8 +7,18 @@ using System.Threading.Tasks;
namespace PSManagement.SharedKernel.DomainErrors namespace PSManagement.SharedKernel.DomainErrors
{ {
public interface IDomainError : IError public class DomainError : Error
{ {
public DomainError(string message) : base(message)
{
}
public DomainError(string message, IError causedBy) : base(message, causedBy)
{
}
protected DomainError()
{
}
} }
} }
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="FluentResults" Version="3.16.0" /> <PackageReference Include="FluentResults" Version="3.16.0" />
<PackageReference Include="MediatR" Version="5.1.0" /> <PackageReference Include="MediatR" Version="8.0.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>
...@@ -8,8 +8,8 @@ namespace PSManagement.SharedKernel.Utilities ...@@ -8,8 +8,8 @@ namespace PSManagement.SharedKernel.Utilities
{ {
public record Error(string Code, string Name) public record Error(string Code, string Name)
{ {
public static Error None = new(string.Empty, string.Empty); public static readonly Error None = new(string.Empty, string.Empty);
public static Error NullValue = new("Error.NullValue", "Null value was provided"); public static readonly Error NullValue = new("Error.NullValue", "Null value was provided");
} }
} }
...@@ -27,7 +27,7 @@ namespace PSManagement.SharedKernel.Utilities ...@@ -27,7 +27,7 @@ namespace PSManagement.SharedKernel.Utilities
public bool IsSuccess { get; } public bool IsSuccess { get; }
public bool IsFailure => !IsSuccess; public bool IsFail => !IsSuccess;
public Error Error { get; } public Error Error { get; }
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSManagement.SharedKernel.ValueObjects
{
public record Money(
int Ammount ,
String Currency
);
}
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