2023-08-15 22:52:30 -05:00
|
|
|
|
using DamageAssesment.Api.Employees.Interfaces;
|
2023-10-19 14:59:02 -05:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-08-15 22:52:30 -05:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace DamageAssesment.Api.Employees.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class EmployeesController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private IEmployeesProvider EmployeeProvider;
|
|
|
|
|
|
|
|
|
|
public EmployeesController(IEmployeesProvider EmployeesProvider)
|
|
|
|
|
{
|
|
|
|
|
this.EmployeeProvider = EmployeesProvider;
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// GET request for retrieving employees.
|
|
|
|
|
/// </summary>
|
2023-10-19 14:59:02 -05:00
|
|
|
|
[Authorize(Roles = "admin")]
|
2023-10-03 23:07:33 -05:00
|
|
|
|
[HttpGet("employees")]
|
2023-08-15 22:52:30 -05:00
|
|
|
|
public async Task<ActionResult> GetEmployeesAsync()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var result = await EmployeeProvider.GetEmployeesAsync();
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Employees);
|
|
|
|
|
}
|
|
|
|
|
return NoContent();
|
|
|
|
|
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// GET request for retrieving an employee by ID.
|
|
|
|
|
/// </summary>
|
2023-10-19 14:59:02 -05:00
|
|
|
|
[Authorize(Roles = "admin")]
|
2023-10-03 23:07:33 -05:00
|
|
|
|
[HttpGet("employees/{id}")]
|
2023-09-13 00:28:24 -05:00
|
|
|
|
public async Task<ActionResult> GetEmployeeByIdAsync(int id)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
|
2023-09-13 00:28:24 -05:00
|
|
|
|
var result = await EmployeeProvider.GetEmployeeByIdAsync(id);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Employee);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
}
|
2023-10-19 14:59:02 -05:00
|
|
|
|
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// PUT request for updating an existing employee.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="Employee">The updated employee object.</param>
|
2023-10-19 14:59:02 -05:00
|
|
|
|
[Authorize(Roles = "admin")]
|
2023-10-03 23:07:33 -05:00
|
|
|
|
[HttpPut("employees/{id}")]
|
2023-09-13 00:28:24 -05:00
|
|
|
|
public async Task<IActionResult> UpdateEmployee(int id, Models.Employee Employee)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
if (Employee != null)
|
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
var result = await this.EmployeeProvider.UpdateEmployeeAsync(id,Employee);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Employee);
|
|
|
|
|
}
|
|
|
|
|
if (result.ErrorMessage == "Not Found")
|
|
|
|
|
return NotFound(result.ErrorMessage);
|
|
|
|
|
|
|
|
|
|
return BadRequest(result.ErrorMessage);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// POST request for creating a new employee.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="Employee">The employee information for creating a new employee.</param>
|
2023-10-19 14:59:02 -05:00
|
|
|
|
[Authorize(Roles = "admin")]
|
2023-10-03 23:07:33 -05:00
|
|
|
|
[HttpPost("employees")]
|
2023-08-25 17:44:04 -05:00
|
|
|
|
public async Task<IActionResult> CreateEmployee(Models.Employee Employee)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
if (Employee != null)
|
|
|
|
|
{
|
|
|
|
|
var result = await this.EmployeeProvider.PostEmployeeAsync(Employee);
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Employee);
|
|
|
|
|
}
|
|
|
|
|
return BadRequest(result.ErrorMessage);
|
|
|
|
|
}
|
2023-09-13 00:28:24 -05:00
|
|
|
|
return CreatedAtRoute("DefaultApi", new { Id = Employee.Id }, Employee);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// DELETE request for deleting an existing employee.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id">The ID of the employee to be deleted.</param>
|
2023-10-19 14:59:02 -05:00
|
|
|
|
[Authorize(Roles = "admin")]
|
2023-10-03 23:07:33 -05:00
|
|
|
|
[HttpDelete("employees/{id}")]
|
2023-09-13 00:28:24 -05:00
|
|
|
|
public async Task<IActionResult> DeleteEmployee(int id)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
var result = await this.EmployeeProvider.DeleteEmployeeAsync(id);
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Employee);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|