Commit 69af1fba authored by hasan khaddour's avatar hasan khaddour

Add Role Controller and userRoles Controller and some fixed error

parent c2f1792e
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PSManagement.Api.Controllers.ApiBase
{
[Route("api/[controller]")]
[ApiController]
public class APIController : ControllerBase
{
}
}

using FluentResults;
using System.Linq;
using Ardalis.Result;
using Microsoft.AspNetCore.Mvc;
using PSManagement.Api.Controllers.ApiBase;
using PSManagement.Application.Contracts.Authentication;
using PSManagement.Contracts.Authentication;
using System.Threading.Tasks;
......@@ -9,8 +10,7 @@ using AuthenticationResponse = PSManagement.Contracts.Authentication.Authenticat
namespace PSManagement.Api.Controllers.Authentication
{
[Route("api/[controller]")]
[ApiController]
public class AuthenticationController : ControllerBase
public class AuthenticationController : APIController
{
private readonly IAuthenticationService _authenticationService;
......@@ -33,7 +33,7 @@ namespace PSManagement.Api.Controllers.Authentication
return Ok(response);
}
return Problem(title: result.Errors[0].Message,detail:result.Errors[0].Reasons[0]?.Message,statusCode:401);
return Problem(title: result.ValidationErrors.FirstOrDefault().ErrorCode,detail:result.ValidationErrors.FirstOrDefault().ErrorMessage,statusCode:401);
}
[HttpPost("Register")]
public async Task<IActionResult> Register([FromBody] RegisterRequest registerRequest)
......
......@@ -15,16 +15,16 @@ using PSManagement.Application.Customers.UseCases.Commands.DeleteCustomer;
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;
using PSManagement.Api.Controllers.ApiBase;
using Ardalis.Result;
namespace PSManagement.Api.Controllers.Customers
{
[Route("api/[controller]")]
[ApiController]
// [Authorize]
public class CustomersController : ControllerBase
{
public class CustomersController : APIController
{
private readonly IMediator _sender;
private readonly IMapper _mapper;
......@@ -49,7 +49,7 @@ namespace PSManagement.Api.Controllers.Customers
{
var query = new GetCustomerQuery(id);
var result = _mapper.Map<Result<CustomerRecord>>(await _sender.Send(query));
var result = await _sender.Send(query);
return Ok(result);
}
......@@ -59,7 +59,6 @@ namespace PSManagement.Api.Controllers.Customers
var command = _mapper.Map<CreateCustomerCommand>(request);
var result = await _sender.Send(command);
return Ok(result);
}
......
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PSManagement.Api.Controllers.ApiBase;
using PSManagement.Application.Contracts.Providers;
using PSManagement.Application.Contracts.SyncData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PSManagement.Api.Controllers.Employees
{
[Route("api/[controller]")]
public class EmployeesController : APIController
{
private readonly ISyncEmployeesService _syncEmployeesService;
private readonly IEmployeesProvider _employeesProvider;
public EmployeesController(ISyncEmployeesService syncEmployeesService)
{
_syncEmployeesService = syncEmployeesService;
}
[HttpPost("SyncEmployees")]
public async Task<IActionResult> Post()
{
SyncResponse response = await _syncEmployeesService.SyncEmployees(_employeesProvider);
return Ok(response);
}
}
}
......@@ -14,9 +14,10 @@ namespace PSManagement.Api.Controllers
public class HomeController : ControllerBase
{
[HttpGet]
public async Task<IActionResult> Get()
public IActionResult Get()
{
return Ok( new { message = "success" });
}
......
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PSManagement.Api.Controllers.ApiBase;
using PSManagement.Application.Contracts.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PSManagement.Api.Controllers.Roles
{
[Route("api/[controller]")]
public class RolesController : APIController
{
private readonly IRoleService _roleService;
public RolesController(IRoleService roleService)
{
_roleService = roleService;
}
[HttpGet("GetRoles")]
public async Task<IActionResult> GetRoleAsync()
{
var getRoles = await _roleService.GetRolesAsync();
return Ok(getRoles);
}
[Authorize(Roles = "Admin")]
[HttpGet("{id}")]
public async Task<IActionResult> GetRoleByIdAsync(int id)
{
var getRolesById = await _roleService.GetRoleByIdAsync(id);
return Ok(getRolesById);
}
[Authorize( Roles = "Admin")]
[HttpPost("Create")]
public async Task<IActionResult> CreateRoleAsync(string roleName)
{
var roleCreated = await _roleService.CreateRoleAsync(roleName);
return Ok(roleCreated);
}
[Authorize(Roles = "Admin")]
[HttpDelete("Delete/{id}")]
public async Task<IActionResult> DeleteRoleAsync(int id)
{
var deleteRole = await _roleService.DeleteRoleAsync(id);
return Ok(deleteRole);
}
[Authorize(Roles = "Admin")]
[HttpPut("Edit/{id}")]
public async Task<ActionResult> UpdateRoleAsync(int id, string roleName)
{
var updateRole = await _roleService.UpdateRole(id, roleName);
return Ok(updateRole);
}
}
}
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PSManagement.Api.Controllers.ApiBase;
using PSManagement.Application.Contracts.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PSManagement.Api.Controllers.Roles
{
[Route("api/[controller]")]
public class UserRolesController : APIController
{
private readonly IUserRoleService _userRoleService;
public UserRolesController(
IUserRoleService userRoleService) : base()
{
_userRoleService = userRoleService;
}
[HttpGet("GetUserRolesAsync")]
public async Task<ActionResult> GetUserRolesAsync(string email)
{
var userRoles = await _userRoleService.GetUserRolesAsync(email);
return Ok(userRoles);
}
[Authorize(Roles = "Admin")]
[HttpPost("AssignUserToRoles")]
public async Task<ActionResult> AssignUserToRolesAsync(string email, string roleName)
{
var roleAssigned = await _userRoleService.AssignUserToRole(email, roleName);
return Ok(roleAssigned);
}
[HttpGet("IsInRole")]
public async Task<ActionResult> IsInRoleAsync(int id, string role)
{
var IsInRole = await _userRoleService.IsInRoleAsync(id, role);
return Ok(IsInRole);
}
[Authorize(Roles = "Admin")]
[HttpDelete("DeleteUserRole")]
public async Task<IActionResult> RemoveUserFromRole(string email, string roleName)
{
var deleteUserFromRole = await _userRoleService.RemoveUserFromRole(email, roleName);
return Ok(deleteUserFromRole);
}
}
}
......@@ -24,7 +24,7 @@ namespace PSManagement.Api.Mappers
CreateMap<CustomerDTO, CustomerRecord>();
CreateMap<ContactInfoDTO, ContactInfoRecord>();
CreateMap<CustomerRecord, CustomerDTO>().ForMember(src =>src.ContactInfo , des => des.Ignore());
CreateMap<CustomerRecord, CustomerDTO>().ReverseMap();
CreateMap<IEnumerable<CustomerRecord>, ListCustomersResponse>()
.ConstructUsing(src => new ListCustomersResponse(src));
......
using AutoMapper;
using FluentResults;
using Ardalis.Result;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -21,11 +21,11 @@ namespace PSManagement.Api.Mappers
if (source.IsSuccess)
{
var mappedValue = context.Mapper.Map<TDestination>(source.Value);
return Result.Ok(mappedValue);
return Result.Success(mappedValue);
}
else
{
return Result.Fail<TDestination>(source.Errors);
return Result.Invalid(source.ValidationErrors);
}
}
}
......
......@@ -36,8 +36,8 @@ namespace PSManagement.Api
.AddAPI()
.AddPresentation()
.AddApplication()
.AddInfrastructure(Configuration)
.AddPersistence(Configuration);
.AddPersistence(Configuration)
.AddInfrastructure(Configuration);
}
......
......@@ -15,6 +15,8 @@
},
"ConnectionStrings": {
"DefaultConnection": "Data Source=.\\sqlexpress;Initial Catalog=PSManagement ;Integrated Security=True"
},
"EmpoyeesSyncJobSettings": {
"SyncIntervalInMinutes": 60
}
}
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