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;
}
///
/// GET request for retrieving employees.
///
[HttpGet("Employees")]
public async Task GetEmployeesAsync()
{
var result = await EmployeeProvider.GetEmployeesAsync();
if (result.IsSuccess)
{
return Ok(result.Employees);
}
return NoContent();
}
///
/// GET request for retrieving an employee by ID.
///
[HttpGet("Employees/{Id}")]
public async Task GetEmployeeByIdAsync(string Id)
{
var result = await EmployeeProvider.GetEmployeeByIdAsync(Id);
if (result.IsSuccess)
{
return Ok(result.Employee);
}
return NotFound();
}
///
/// PUT request for updating an existing employee.
///
/// The updated employee object.
[HttpPut("Employees")]
public async Task UpdateEmployee(string Id, Models.Employee Employee)
{
if (Employee != null)
{
var result = await this.EmployeeProvider.UpdateEmployeeAsync(Id,Employee);
if (result.IsSuccess)
{
return Ok(result.Employee);
}
if (result.ErrorMessage == "Not Found")
return NotFound(result.ErrorMessage);
return BadRequest(result.ErrorMessage);
}
return NotFound();
}
///
/// POST request for creating a new employee.
///
/// The employee information for creating a new employee.
[HttpPost("Employees")]
public async Task CreateEmployee(Models.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);
}
///
/// DELETE request for deleting an existing employee.
///
/// The ID of the employee to be deleted.
[HttpDelete("Employees/{id}")]
public async Task DeleteEmployee(string id)
{
var result = await this.EmployeeProvider.DeleteEmployeeAsync(id);
if (result.IsSuccess)
{
return Ok(result.Employee);
}
return NotFound();
}
}
}