Files
Prog4_Beadando/WD7UVN_HFT_2023241.Endpoint/Controllers/EmployeeController.cs

66 lines
1.7 KiB
C#
Raw Normal View History

2023-11-22 12:08:03 +01:00
using Microsoft.AspNetCore.Mvc;
using WD7UVN_HFT_2023241.Logic;
using System.Linq;
2023-12-13 22:04:06 +01:00
using System;
using WD7UVN_HFT_2023241.Models;
2023-11-22 12:08:03 +01:00
namespace WD7UVN_HFT_2023241.Endpoint
{
[ApiController]
[Route("api/Employee")]
public class EmployeeController : ControllerBase
2023-11-22 12:08:03 +01:00
{
public ILogicServices LogicServices { get; set; }
public EmployeeController(ILogicServices LogicServices)
{
this.LogicServices = LogicServices;
}
[HttpGet()]
2023-12-13 22:04:06 +01:00
public IQueryable<Employee>? ReadAllEmployees()
{
2023-12-13 22:04:06 +01:00
try
{
return LogicServices.CRUDOperations.ReadAllEmployees();
}
catch (NullReferenceException)
{
return null;
}
}
[HttpGet("{id}")]
2023-12-13 22:04:06 +01:00
public Employee? ReadEmployee(int id)
{
2023-12-13 22:04:06 +01:00
try
{
return LogicServices.CRUDOperations.ReadEmployee(id);
}
catch (NullReferenceException)
{
return null;
}
}
2023-12-13 22:04:06 +01:00
[HttpPut()]
public void PutEmployee([FromBody] Employee e)
{
LogicServices.CRUDOperations.CreateEmployee(e);
}
2023-12-13 22:04:06 +01:00
[HttpPost()]
public void UpdateEmployee([FromBody] Employee e)
{
LogicServices.CRUDOperations.UpdateEmployee(e);
}
//HttpClient does not support sending data in the body of a DELETE request. Instead, we can send the data in the URL like with a GET request.
[HttpDelete("{id}")]
public void DeleteEmployee(int id)
{
LogicServices.CRUDOperations.DeleteEmployee(id);
}
2023-11-22 12:08:03 +01:00
}
}