Commit 57363ef1 authored by hasan khaddour's avatar hasan khaddour

add handler request to map the result

parent e44cd7d2
using Microsoft.AspNetCore.Http;
using Ardalis.Result;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
......@@ -11,5 +13,18 @@ namespace PSManagement.Api.Controllers.ApiBase
[ApiController]
public class APIController : ControllerBase
{
protected IActionResult HandleResult<T>(Result<T> result)
{
if (result.IsSuccess)
{
return Ok(result.Value);
}
else
{
return Problem(detail: result.Errors.FirstOrDefault(), statusCode: StatusCodes.Status400BadRequest);
}
}
}
}
......@@ -6,6 +6,7 @@ using PSManagement.Application.Contracts.Authentication;
using PSManagement.Contracts.Authentication;
using System.Threading.Tasks;
using AuthenticationResponse = PSManagement.Contracts.Authentication.AuthenticationResponse;
using AutoMapper;
namespace PSManagement.Api.Controllers.Authentication
{
......@@ -13,29 +14,23 @@ namespace PSManagement.Api.Controllers.Authentication
public class AuthenticationController : APIController
{
private readonly IAuthenticationService _authenticationService;
public AuthenticationController(IAuthenticationService authenticationService)
private readonly IMapper _mapper;
public AuthenticationController(IAuthenticationService authenticationService, IMapper mapper)
{
_authenticationService = authenticationService;
_mapper = mapper;
}
[HttpPost("Login")]
public async Task<IActionResult> Login([FromBody] LoginRequest loginRequest)
{
Result<AuthenticationResult> result = await _authenticationService.Login(loginRequest.Email, loginRequest.PassWord);
if (result.IsSuccess) {
AuthenticationResponse response = new (
result.Value.EmployeeId,
result.Value.FirstName,
result.Value.LastName,
result.Value.Email,
result.Value.Roles,
result.Value.Token);
return Ok(response);
}
return Problem(title: result.ValidationErrors.FirstOrDefault().ErrorCode,detail:result.ValidationErrors.FirstOrDefault().ErrorMessage,statusCode:401);
return HandleResult(_mapper.Map<Result<AuthenticationResponse>>(result));
}
[HttpPost("Register")]
public async Task<IActionResult> Register([FromBody] RegisterRequest registerRequest)
{
......
......@@ -23,7 +23,7 @@ using PSManagement.Application.Customers.UseCases.Commands.RemoveContactInfo;
namespace PSManagement.Api.Controllers.Customers
{
[Route("api/[controller]")]
// [Authorize]
[Authorize]
public class CustomersController : APIController
{
private readonly IMediator _sender;
......@@ -42,7 +42,7 @@ namespace PSManagement.Api.Controllers.Customers
var result = _mapper.Map<Result<IEnumerable<CustomerResponse>>>( await _sender.Send(query));
return Ok(result);
return HandleResult(result);
}
[HttpGet("{id}")]
......@@ -53,7 +53,7 @@ namespace PSManagement.Api.Controllers.Customers
var result = await _sender.Send(query);
return Ok(_mapper.Map<Result<CustomerResponse>>(result));
return HandleResult(_mapper.Map<Result<CustomerResponse>>(result));
}
[HttpPost]
public async Task<IActionResult> Post(CreateCustomerRequest request)
......@@ -69,13 +69,13 @@ namespace PSManagement.Api.Controllers.Customers
var response = await _sender.Send(query);
return Ok(_mapper.Map<Result<CustomerResponse>>(response));
return HandleResult(_mapper.Map<Result<CustomerResponse>>(response));
}
else
{
return Ok(result);
return HandleResult(result);
}
......@@ -88,7 +88,7 @@ namespace PSManagement.Api.Controllers.Customers
var result = await _sender.Send(command);
return Ok(result);
return HandleResult(result);
}
......@@ -102,7 +102,7 @@ namespace PSManagement.Api.Controllers.Customers
var result = await _sender.Send(command);
return Ok(result);
return HandleResult(result);
}
......@@ -114,7 +114,7 @@ namespace PSManagement.Api.Controllers.Customers
var result = await _sender.Send(command);
return Ok(result);
return HandleResult(result);
}
......@@ -125,7 +125,7 @@ namespace PSManagement.Api.Controllers.Customers
var result = await _sender.Send(command);
return Ok(result);
return HandleResult(result);
}
......
......@@ -47,7 +47,7 @@ namespace PSManagement.Api.Controllers.Employees
var result = await _sender.Send(query);
return Ok(_mapper.Map<Result<EmployeeResponse>>(result));
return HandleResult(_mapper.Map<Result<EmployeeResponse>>(result));
}
[HttpGet("ByFilter")]
......@@ -57,37 +57,37 @@ namespace PSManagement.Api.Controllers.Employees
var result = await _sender.Send(query);
return Ok(_mapper.Map<Result<IEnumerable<EmployeeResponse>>>(result));
return HandleResult(_mapper.Map<Result<IEnumerable<EmployeeResponse>>>(result));
}
[HttpGet]
public async Task<IActionResult> GetAll([FromQuery]GetAvailableEmployeesRequest request)
[HttpGet("Available")]
public async Task<IActionResult> GetAllAvailable([FromQuery]GetAvailableEmployeesRequest request)
{
GetAvailableEmployeesQuery query = _mapper.Map<GetAvailableEmployeesQuery>(request);
var result = await _sender.Send(query);
return Ok(_mapper.Map<Result<IEnumerable<EmployeeResponse>>>(result));
return HandleResult(_mapper.Map<Result<IEnumerable<EmployeeResponse>>>(result));
}
[HttpPost("EmployeeParticipations")]
public async Task<IActionResult> GetEmployeeParticipations([FromForm] GetEmployeeParticipationRequest request)
[HttpGet("EmployeeParticipations")]
public async Task<IActionResult> GetEmployeeParticipations([FromQuery] GetEmployeeParticipationRequest request)
{
var command = _mapper.Map<GetEmployeeParticipationQuery>(request);
var result = await _sender.Send(command);
return Ok(_mapper.Map<Result<IEnumerable<EmployeeParticipateResponse>>>(result));
return HandleResult(_mapper.Map<Result<IEnumerable<EmployeeParticipateResponse>>>(result));
}
[HttpPost("TrackHistory")]
[HttpGet("TrackHistory")]
public async Task<IActionResult> GetEmployeeTrackHistory([FromForm] GetEmployeeTrackHistoryRequest request)
{
var command = _mapper.Map<GetEmployeeTrackHistoryQuery>(request);
var result = await _sender.Send(command);
return Ok(_mapper.Map<Result<IEnumerable<EmployeeTrackResponse>>>(result));
return HandleResult(_mapper.Map<Result<IEnumerable<EmployeeTrackResponse>>>(result));
}
......@@ -108,7 +108,7 @@ namespace PSManagement.Api.Controllers.Employees
var result = await _sender.Send(query);
return Ok(result);
return HandleResult(result);
}
......
......@@ -45,13 +45,13 @@ namespace PSManagement.Api.Controllers.FinancialSpends
var query = new GetFinancialSpendItemByIdQuery(result.Value);
var response = await _sender.Send(query);
return Ok(_mapper.Map<FinancialSpendingResponse>(response));
return HandleResult(_mapper.Map<Result<FinancialSpendingResponse>>(response));
}
else
{
return Ok(result);
return HandleResult(result);
}
}
......@@ -63,7 +63,7 @@ namespace PSManagement.Api.Controllers.FinancialSpends
var result = await _sender.Send(query);
return Ok(_mapper.Map<Result<FinancialSpendingResponse>>(result)); ;
return HandleResult(_mapper.Map<Result<FinancialSpendingResponse>>(result)); ;
}
[HttpGet("GetByProject")]
......@@ -73,7 +73,7 @@ namespace PSManagement.Api.Controllers.FinancialSpends
var result = await _sender.Send(query);
return Ok(_mapper.Map<Result<IEnumerable<FinancialSpendingResponse>>>(result)); ;
return HandleResult(_mapper.Map<Result<IEnumerable<FinancialSpendingResponse>>>(result)); ;
}
[HttpDelete("{id}")]
......@@ -87,7 +87,7 @@ namespace PSManagement.Api.Controllers.FinancialSpends
var result = await _sender.Send(query);
return Ok(result); ;
return HandleResult(result); ;
}
[HttpPut("{id}")]
......@@ -102,7 +102,7 @@ namespace PSManagement.Api.Controllers.FinancialSpends
var result = await _sender.Send(query);
return Ok(result); ;
return HandleResult(result); ;
}
}
......
......@@ -53,7 +53,7 @@ namespace PSManagement.Api.Controllers.Projects
var result = _mapper.Map<Result<IEnumerable<ProjectResponse>>>(await _sender.Send(query));
return Ok(result);
return HandleResult(result);
}
......@@ -64,7 +64,7 @@ namespace PSManagement.Api.Controllers.Projects
var result = await _sender.Send(query);
return Ok(_mapper.Map<Result<IEnumerable<ProjectResponse>>>(result));
return HandleResult(_mapper.Map<Result<IEnumerable<ProjectResponse>>>(result));
}
[HttpGet("ByProjectManager")]
......@@ -74,7 +74,7 @@ namespace PSManagement.Api.Controllers.Projects
var result = await _sender.Send(query);
return Ok(_mapper.Map<Result<IEnumerable<ProjectResponse>>>(result));
return HandleResult(_mapper.Map<Result<IEnumerable<ProjectResponse>>>(result));
}
[HttpGet("GetParticipants/{id}")]
......@@ -84,7 +84,7 @@ namespace PSManagement.Api.Controllers.Projects
var result = await _sender.Send(query);
return Ok( _mapper.Map<Result<IEnumerable<EmployeeParticipateResponse>>>(result));
return HandleResult( _mapper.Map<Result<IEnumerable<EmployeeParticipateResponse>>>(result));
}
......@@ -95,7 +95,7 @@ namespace PSManagement.Api.Controllers.Projects
var result = await _sender.Send(query);
return Ok(result);
return HandleResult(result);
}
......@@ -106,7 +106,7 @@ namespace PSManagement.Api.Controllers.Projects
var result = await _sender.Send(query);
return Ok(result);
return HandleResult(result);
}
......@@ -117,7 +117,7 @@ namespace PSManagement.Api.Controllers.Projects
var result = await _sender.Send(query);
return Ok(result);
return HandleResult(result);
}
......@@ -128,7 +128,7 @@ namespace PSManagement.Api.Controllers.Projects
var result = await _sender.Send(query);
return Ok(result);
return HandleResult(result);
}
#region project state operations
......@@ -140,7 +140,7 @@ namespace PSManagement.Api.Controllers.Projects
var result = await _sender.Send(query);
return Ok(result);
return HandleResult(result);
}
[HttpPost("CancelProject/{id}")]
......@@ -154,7 +154,7 @@ namespace PSManagement.Api.Controllers.Projects
var result = await _sender.Send(query);
return Ok(result);
return HandleResult(result);
}
......@@ -168,7 +168,7 @@ namespace PSManagement.Api.Controllers.Projects
var result = await _sender.Send(query);
return Ok(result);
return HandleResult(result);
}
......@@ -181,7 +181,7 @@ namespace PSManagement.Api.Controllers.Projects
var result = await _sender.Send(query);
return Ok(_mapper.Map<Result<ProjectResponse>>(result));
return HandleResult(_mapper.Map<Result<ProjectResponse>>(result));
}
......@@ -197,12 +197,12 @@ namespace PSManagement.Api.Controllers.Projects
var query = new GetProjectByIdQuery(result.Value);
var response = await _sender.Send(query);
return Ok(_mapper.Map<Result<ProjectResponse>>(response));
return HandleResult(_mapper.Map<Result<ProjectResponse>>(response));
}
else {
return Ok(result);
return HandleResult(result);
}
......@@ -213,7 +213,7 @@ namespace PSManagement.Api.Controllers.Projects
{
var command = _mapper.Map<AddAttachmentCommand>(request);
var result = await _sender.Send(command);
return Ok(result);
return HandleResult(result);
}
[HttpGet("Attachments/{id}")]
......@@ -222,7 +222,7 @@ namespace PSManagement.Api.Controllers.Projects
var query = _mapper.Map<GetProjectAttachmentsQuery>(request);
var result = await _sender.Send(query);
return Ok(_mapper.Map<Result<IEnumerable<AttachmentReponse>>>(result));
return HandleResult(_mapper.Map<Result<IEnumerable<AttachmentReponse>>>(result));
}
......
......@@ -32,7 +32,7 @@ namespace PSManagement.Api.Controllers.Roles
public async Task<IActionResult> GetRoleByIdAsync(int id)
{
var getRolesById = await _roleService.GetRoleByIdAsync(id);
return Ok(getRolesById);
return HandleResult(getRolesById);
}
[Authorize( Roles = "Admin")]
......@@ -40,7 +40,7 @@ namespace PSManagement.Api.Controllers.Roles
public async Task<IActionResult> CreateRoleAsync(string roleName)
{
var roleCreated = await _roleService.CreateRoleAsync(roleName);
return Ok(roleCreated);
return HandleResult(roleCreated);
}
[Authorize(Roles = "Admin")]
......@@ -48,15 +48,15 @@ namespace PSManagement.Api.Controllers.Roles
public async Task<IActionResult> DeleteRoleAsync(int id)
{
var deleteRole = await _roleService.DeleteRoleAsync(id);
return Ok(deleteRole);
return HandleResult(deleteRole);
}
[Authorize(Roles = "Admin")]
[HttpPut("Edit/{id}")]
public async Task<ActionResult> UpdateRoleAsync(int id, string roleName)
public async Task<IActionResult> UpdateRoleAsync(int id, string roleName)
{
var updateRole = await _roleService.UpdateRole(id, roleName);
return Ok(updateRole);
return HandleResult(updateRole);
}
}
}
......@@ -23,22 +23,22 @@ namespace PSManagement.Api.Controllers.Roles
}
[HttpGet("GetUserRolesAsync")]
public async Task<ActionResult> GetUserRolesAsync(string email)
public async Task<IActionResult> GetUserRolesAsync(string email)
{
var userRoles = await _userRoleService.GetUserRolesAsync(email);
return Ok(userRoles);
return HandleResult(userRoles);
}
[Authorize(Roles = "Admin")]
[HttpPost("AssignUserToRoles")]
public async Task<ActionResult> AssignUserToRolesAsync(string email, string roleName)
public async Task<IActionResult> AssignUserToRolesAsync(string email, string roleName)
{
var roleAssigned = await _userRoleService.AssignUserToRole(email, roleName);
return Ok(roleAssigned);
return HandleResult(roleAssigned);
}
[HttpGet("IsInRole")]
public async Task<ActionResult> IsInRoleAsync(int id, string role)
public async Task<IActionResult> IsInRoleAsync(int id, string role)
{
var IsInRole = await _userRoleService.IsInRoleAsync(id, role);
return Ok(IsInRole);
......@@ -50,7 +50,7 @@ namespace PSManagement.Api.Controllers.Roles
public async Task<IActionResult> RemoveUserFromRole(string email, string roleName)
{
var deleteUserFromRole = await _userRoleService.RemoveUserFromRole(email, roleName);
return Ok(deleteUserFromRole);
return HandleResult(deleteUserFromRole);
}
......
......@@ -38,7 +38,7 @@ namespace PSManagement.Api.Controllers.Steps
var result = _mapper.Map<Result<StepResponse>>(await _sender.Send(query));
return Ok(result);
return HandleResult(result);
}
[HttpGet("ByProject")]
......@@ -48,20 +48,20 @@ namespace PSManagement.Api.Controllers.Steps
var result = _mapper.Map<Result<IEnumerable<StepResponse>>>(await _sender.Send(query));
return Ok(result);
return HandleResult(result);
}
[HttpPut("{id}")]
public async Task<IActionResult> PutChangeStepWeight(ChangeStepWeightRequest request,[FromRoute]int id)
{
if (request.StepId != id) {
return Ok(Result.NotFound("the step not found"));
return HandleResult(Result.NotFound("the step not found"));
}
var query = _mapper.Map<ChangeStepWeightCommand>(request); ;
var result = _mapper.Map<Result>(await _sender.Send(query));
return Ok(result);
return HandleResult(result);
}
......@@ -72,7 +72,7 @@ namespace PSManagement.Api.Controllers.Steps
var result = _mapper.Map<Result<IEnumerable<StepTrackResponse>>>(await _sender.Send(query));
return Ok(result);
return HandleResult(result);
}
[HttpPut]
......@@ -82,7 +82,7 @@ namespace PSManagement.Api.Controllers.Steps
var result = _mapper.Map<Result>(await _sender.Send(query));
return Ok(result);
return HandleResult(result);
}
[HttpDelete("{id}")]
......@@ -92,7 +92,7 @@ namespace PSManagement.Api.Controllers.Steps
var result = _mapper.Map<Result>(await _sender.Send(query));
return Ok(result);
return HandleResult(result);
}
}
}
......@@ -41,7 +41,7 @@ namespace PSManagement.Api.Controllers.Tracks
var result = _mapper.Map<Result<TrackResponse>>(await _sender.Send(query));
return Ok(result);
return HandleResult(result);
}
[HttpGet("GetStepsTrack{id}")]
......@@ -51,7 +51,7 @@ namespace PSManagement.Api.Controllers.Tracks
var result = _mapper.Map<Result<IEnumerable<StepTrackResponse>>>(await _sender.Send(query));
return Ok(result);
return HandleResult(result);
}
[HttpGet("GetEmployeesTrack{id}")]
......@@ -61,7 +61,7 @@ namespace PSManagement.Api.Controllers.Tracks
var result = _mapper.Map<Result<IEnumerable<EmployeeTrackResponse>>>(await _sender.Send(query));
return Ok(result);
return HandleResult(result);
}
......@@ -72,7 +72,7 @@ namespace PSManagement.Api.Controllers.Tracks
var result = _mapper.Map<Result<IEnumerable<TrackResponse>>>(await _sender.Send(query));
return Ok(result);
return HandleResult(result);
}
[HttpPost("AddStepTrack")]
......@@ -82,7 +82,7 @@ namespace PSManagement.Api.Controllers.Tracks
var result = _mapper.Map<Result<int>>(await _sender.Send(command));
return Ok(result);
return HandleResult(result);
}
[HttpPost("AddEmployeeTrack")]
......@@ -92,7 +92,7 @@ namespace PSManagement.Api.Controllers.Tracks
var result = _mapper.Map<Result<int>>(await _sender.Send(command));
return Ok(result);
return HandleResult(result);
}
[HttpPost("CompleteTrack")]
......@@ -102,7 +102,7 @@ namespace PSManagement.Api.Controllers.Tracks
var result =await _sender.Send(command);
return Ok(result);
return HandleResult(result);
}
[HttpPost("RemoveTrack")]
......@@ -112,7 +112,7 @@ namespace PSManagement.Api.Controllers.Tracks
var result =await _sender.Send(command);
return Ok(result);
return HandleResult(result);
}
[HttpPost]
......@@ -128,13 +128,13 @@ namespace PSManagement.Api.Controllers.Tracks
var query = new GetTrackByIdQuery(result.Value);
var response = await _sender.Send(query);
return Ok(_mapper.Map<TrackResponse>(response));
return HandleResult(_mapper.Map<Result<TrackResponse>>(response));
}
else
{
return Ok(result);
return HandleResult(result);
}
}
......@@ -146,7 +146,7 @@ namespace PSManagement.Api.Controllers.Tracks
var result =await _sender.Send(command);
return Ok(result);
return HandleResult(result);
}
[HttpPut("UpdateStepTrack")]
public async Task<IActionResult> PutStepTrack(UpdateStepTrackRequest request)
......@@ -155,7 +155,7 @@ namespace PSManagement.Api.Controllers.Tracks
var result = await _sender.Send(command);
return Ok(result);
return HandleResult(result);
}
......
using AutoMapper;
using PSManagement.Application.Contracts.Authentication;
using PSManagement.Application.Customers.Common;
using PSManagement.Application.Customers.UseCases.Commands.AddContactInfo;
using PSManagement.Application.Customers.UseCases.Commands.CreateCustomer;
......@@ -13,6 +14,7 @@ using PSManagement.Application.Projects.UseCases.Commands.CreateProject;
using PSManagement.Application.Projects.UseCases.Commands.RemoveParticipant;
using PSManagement.Application.Projects.UseCases.Queries.ListAllProject;
using PSManagement.Application.Tracks.Common;
using PSManagement.Contracts.Authentication;
using PSManagement.Contracts.Customers.Requests;
using PSManagement.Contracts.Customers.Responses;
using PSManagement.Contracts.Projects.Requests;
......@@ -41,7 +43,8 @@ namespace PSManagement.Api.Mappers
CreateMap<IEnumerable<CustomerResponse>, ListCustomersResponse>()
.ConstructUsing(src => new ListCustomersResponse(src));
CreateMap<AuthenticationResult,AuthenticationResponse>().ReverseMap();
}
}
public class ProjectMapperConfiguration : Profile
......
......@@ -49,7 +49,7 @@ namespace PSManagement.Application.Projects.UseCases.Commands.AddParticipant
return Result.Invalid(ProjectsErrors.InvalidEntryError);
}else {
if (project.EmployeeParticipates.Where(e => e.Id == request.ParticipantId).FirstOrDefault() is not null)
if (project.EmployeeParticipates.Where(e => e.EmployeeId == request.ParticipantId).FirstOrDefault() is not null)
{
return Result.Invalid(ProjectsErrors.ParticipantExistError);
......
......@@ -35,6 +35,7 @@ namespace PSManagement.Application.Steps.UseCases.Queries.GetStepsByProject
_specification.ApplyPaging((pageNumber - 1) * pageSize, pageSize);
_specification.Criteria = p => p.ProjectId == request.ProjectId;
var steps = await _stepsRepository.ListAsync(_specification);
return Result.Success(_mapper.Map<IEnumerable<StepDTO>>(steps));
......
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