166 lines
5.5 KiB
C#
166 lines
5.5 KiB
C#
using LCU.API.Internal.Operations.IncentiveApi.Exceptions;
|
|
using LCU.API.Internal.Operations.IncentiveApi.Services;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.FeatureManagement.Mvc;
|
|
using LCU.API.Internal.Operations.IncentiveApi.Configuration;
|
|
using LCU.API.Internal.Operations.IncentiveApiLib.Services;
|
|
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
|
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
|
|
|
namespace LCU.API.Internal.Operations.IncentiveApi.Controllers;
|
|
|
|
[FeatureGate(FeatureFlags.EnableControllers)]
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
|
public class DepartmentController : ControllerBase
|
|
{
|
|
private readonly IDepartmentService _departmentService;
|
|
private readonly IUserService _userService;
|
|
private readonly ICurrentUserService _currentUser;
|
|
|
|
public DepartmentController(IDepartmentService departmentService, IUserService userService, ICurrentUserService currentUser)
|
|
{
|
|
_departmentService = departmentService;
|
|
_userService = userService;
|
|
_currentUser = currentUser;
|
|
}
|
|
|
|
[HttpGet("", Name = "GetAllDepartments")]
|
|
public async Task<IActionResult> GetDepartments([FromQuery] bool? canEditDepartmentGoals, [FromQuery] bool? canEditPersonnelGoals)
|
|
{
|
|
try
|
|
{
|
|
if (canEditDepartmentGoals != null || canEditPersonnelGoals != null)
|
|
{
|
|
var appUser = _currentUser.GetCurrentUser();
|
|
if (appUser?.Email == null)
|
|
{
|
|
return Unauthorized("No AD User Found.");
|
|
}
|
|
|
|
var employee = await _userService.GetUserByAdEmailAsync(appUser.Email);
|
|
|
|
if (employee == null)
|
|
{
|
|
return Unauthorized("No AD User Found.");
|
|
}
|
|
|
|
if (canEditDepartmentGoals.HasValue)
|
|
{
|
|
return Ok(await _departmentService.GetHierarchicalDepartmentsForEmployeeAsync(employee, false));
|
|
}
|
|
if (canEditPersonnelGoals.HasValue)
|
|
{
|
|
return Ok( await _departmentService.GetHierarchicalDepartmentsForEmployeeAsync(employee, true));
|
|
}
|
|
}
|
|
var result = await _departmentService.GetAllDepartmentsAsync();
|
|
return Ok(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpGet("{departmentId:int}", Name = "GetDepartmentById")]
|
|
public async Task<IActionResult> GetDepartmentById([FromRoute] int departmentId)
|
|
{
|
|
try
|
|
{
|
|
var result = await _departmentService.GetDepartmentByIdAsync(departmentId);
|
|
return Ok(result);
|
|
}
|
|
catch (NotFoundException)
|
|
{
|
|
return NotFound($"Could not find Department with that Id");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpPost("", Name = "CreateNewDepartment")]
|
|
public async Task<IActionResult> CreateDepartment([FromBody] Department newDepartment)
|
|
{
|
|
try
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
if (newDepartment.Id != null)
|
|
{
|
|
return BadRequest("Id Field Cannot Be Assigned");
|
|
}
|
|
|
|
var result = await _departmentService.SaveDepartmentAsync(newDepartment);
|
|
return (Ok(result));
|
|
}
|
|
catch (DuplicateDataException)
|
|
{
|
|
return BadRequest($"Department Name Must Be Unique.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpPut("{id:int}", Name = "UpdateExistingDepartment")]
|
|
public async Task<IActionResult> UpdateDepartment([FromBody] Department newDepartment, [FromRoute] int id)
|
|
{
|
|
try
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
var result = await _departmentService.UpdateDepartmentAsync(newDepartment, id);
|
|
return (Ok(result));
|
|
}
|
|
catch (DuplicateDataException)
|
|
{
|
|
return BadRequest($"Department Name Must Be Unique.");
|
|
}
|
|
catch (NotFoundException)
|
|
{
|
|
return NotFound($"Could not find Department with that Id");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpGet("{id:int}/details", Name = "GetDepartmentDetails")]
|
|
public async Task<IActionResult> GetDepartmentDetails([FromRoute] int id)
|
|
{
|
|
try
|
|
{
|
|
var details = await _departmentService.GetDepartmentDetailsAsync(id);
|
|
return Ok(details);
|
|
}
|
|
catch (NotFoundException ex)
|
|
{
|
|
return NotFound(ex.Message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
}
|