DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Employees/Controllers/EmployeesController.cs
2023-08-15 23:52:30 -04:00

91 lines
2.7 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;
}
//get all Employees
[HttpGet("Employees")]
public async Task<ActionResult> GetEmployeesAsync()
{
var result = await EmployeeProvider.GetEmployeesAsync();
if (result.IsSuccess)
{
return Ok(result.Employees);
}
return NoContent();
}
//get Employee based on Employeeid
[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();
}
//update existing Employee
[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();
}
//save new Employee
[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);
}
//delete existing Employee
[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();
}
}
}