109 lines
3.3 KiB
C#
109 lines
3.3 KiB
C#
using DamageAssesment.Api.Employees.Interfaces;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace DamageAssesment.Api.Employees.Controllers
|
|
{
|
|
[Route("api")]
|
|
[ApiController]
|
|
public class EmployeesController : ControllerBase
|
|
{
|
|
|
|
private IEmployeesProvider EmployeeProvider;
|
|
|
|
public EmployeesController(IEmployeesProvider EmployeesProvider)
|
|
{
|
|
this.EmployeeProvider = EmployeesProvider;
|
|
}
|
|
|
|
/// <summary>
|
|
/// GET request for retrieving employees.
|
|
/// </summary>
|
|
|
|
[HttpGet("Employees")]
|
|
public async Task<ActionResult> GetEmployeesAsync()
|
|
{
|
|
|
|
var result = await EmployeeProvider.GetEmployeesAsync();
|
|
if (result.IsSuccess)
|
|
{
|
|
return Ok(result.Employees);
|
|
}
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// GET request for retrieving an employee by ID.
|
|
/// </summary>
|
|
|
|
[HttpGet("Employees/{Id}")]
|
|
public async Task<ActionResult> GetEmployeeByIdAsync(string Id)
|
|
{
|
|
|
|
var result = await EmployeeProvider.GetEmployeeByIdAsync(Id);
|
|
if (result.IsSuccess)
|
|
{
|
|
return Ok(result.Employee);
|
|
}
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// PUT request for updating an existing employee.
|
|
/// </summary>
|
|
/// <param name="Employee">The updated employee object.</param>
|
|
[HttpPut("Employees")]
|
|
public async Task<IActionResult> UpdateEmployee(Db.Employee Employee)
|
|
{
|
|
if (Employee != null)
|
|
{
|
|
var result = await this.EmployeeProvider.UpdateEmployeeAsync(Employee);
|
|
if (result.IsSuccess)
|
|
{
|
|
return Ok(result.Employee);
|
|
}
|
|
if (result.ErrorMessage == "Not Found")
|
|
return NotFound(result.ErrorMessage);
|
|
|
|
return BadRequest(result.ErrorMessage);
|
|
}
|
|
return NotFound();
|
|
}
|
|
|
|
/// <summary>
|
|
/// POST request for creating a new employee.
|
|
/// </summary>
|
|
/// <param name="Employee">The employee information for creating a new employee.</param>
|
|
[HttpPost("Employees")]
|
|
public async Task<IActionResult> CreateEmployee(Db.Employee Employee)
|
|
{
|
|
if (Employee != null)
|
|
{
|
|
var result = await this.EmployeeProvider.PostEmployeeAsync(Employee);
|
|
if (result.IsSuccess)
|
|
{
|
|
return Ok(result.Employee);
|
|
}
|
|
return BadRequest(result.ErrorMessage);
|
|
}
|
|
return CreatedAtRoute("DefaultApi", new { id = Employee.Id }, Employee);
|
|
}
|
|
/// <summary>
|
|
/// DELETE request for deleting an existing employee.
|
|
/// </summary>
|
|
/// <param name="id">The ID of the employee to be deleted.</param>
|
|
[HttpDelete("Employees/{id}")]
|
|
public async Task<IActionResult> DeleteEmployee(string id)
|
|
{
|
|
var result = await this.EmployeeProvider.DeleteEmployeeAsync(id);
|
|
if (result.IsSuccess)
|
|
{
|
|
return Ok(result.Employee);
|
|
}
|
|
return NotFound();
|
|
}
|
|
}
|
|
}
|