2023-11-22 12:08:03 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-11-22 14:09:15 +01:00
|
|
|
using WD7UVN_HFT_2023241.Logic;
|
2023-12-04 17:06:17 +01:00
|
|
|
using System.Linq;
|
2023-12-13 22:04:06 +01:00
|
|
|
using System;
|
2023-12-04 17:06:17 +01:00
|
|
|
using WD7UVN_HFT_2023241.Models;
|
2024-04-24 11:14:55 +02:00
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
using WD7UVN_HFT_2023241.Endpoint.Services;
|
2023-11-22 12:08:03 +01:00
|
|
|
|
|
|
|
|
namespace WD7UVN_HFT_2023241.Endpoint
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
2023-12-05 14:37:49 +01:00
|
|
|
[Route("api/Employee")]
|
2023-12-05 14:25:27 +01:00
|
|
|
public class EmployeeController : ControllerBase
|
2023-11-22 12:08:03 +01:00
|
|
|
{
|
2023-11-22 14:09:15 +01:00
|
|
|
public ILogicServices LogicServices { get; set; }
|
2024-04-24 11:14:55 +02:00
|
|
|
IHubContext<SignalRHub> hub;
|
2023-11-22 14:09:15 +01:00
|
|
|
|
2024-04-24 11:14:55 +02:00
|
|
|
public EmployeeController(ILogicServices LogicServices, IHubContext<SignalRHub> hub)
|
2023-11-22 14:09:15 +01:00
|
|
|
{
|
|
|
|
|
this.LogicServices = LogicServices;
|
2024-04-24 11:14:55 +02:00
|
|
|
this.hub = hub;
|
2023-11-22 14:09:15 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-05 14:25:27 +01:00
|
|
|
[HttpGet()]
|
2023-12-13 22:04:06 +01:00
|
|
|
public IQueryable<Employee>? ReadAllEmployees()
|
2023-12-04 17:06:17 +01:00
|
|
|
{
|
2023-12-13 22:04:06 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return LogicServices.CRUDOperations.ReadAllEmployees();
|
|
|
|
|
}
|
|
|
|
|
catch (NullReferenceException)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-12-04 17:06:17 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-05 14:25:27 +01:00
|
|
|
[HttpGet("{id}")]
|
2023-12-13 22:04:06 +01:00
|
|
|
public Employee? ReadEmployee(int id)
|
2023-12-04 17:06:17 +01:00
|
|
|
{
|
2023-12-13 22:04:06 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return LogicServices.CRUDOperations.ReadEmployee(id);
|
|
|
|
|
}
|
|
|
|
|
catch (NullReferenceException)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-12-04 17:06:17 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-13 22:04:06 +01:00
|
|
|
[HttpPut()]
|
2023-12-04 17:06:17 +01:00
|
|
|
public void PutEmployee([FromBody] Employee e)
|
|
|
|
|
{
|
|
|
|
|
LogicServices.CRUDOperations.CreateEmployee(e);
|
2024-04-24 11:14:55 +02:00
|
|
|
hub.Clients.All.SendAsync("EmployeeCreated", e);
|
2023-12-04 17:06:17 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-13 22:04:06 +01:00
|
|
|
[HttpPost()]
|
2023-12-04 17:06:17 +01:00
|
|
|
public void UpdateEmployee([FromBody] Employee e)
|
|
|
|
|
{
|
|
|
|
|
LogicServices.CRUDOperations.UpdateEmployee(e);
|
2024-04-24 11:14:55 +02:00
|
|
|
hub.Clients.All.SendAsync("EmployeeUpdated", e);
|
2023-12-04 17:06:17 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-09 18:22:22 +01:00
|
|
|
//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)
|
2023-12-04 17:06:17 +01:00
|
|
|
{
|
2024-04-24 11:14:55 +02:00
|
|
|
Employee e = LogicServices.CRUDOperations.ReadEmployee(id);
|
2023-12-04 17:06:17 +01:00
|
|
|
LogicServices.CRUDOperations.DeleteEmployee(id);
|
2024-04-24 11:14:55 +02:00
|
|
|
hub.Clients.All.SendAsync("EmployeeDeleted", e);
|
2023-12-04 17:06:17 +01:00
|
|
|
}
|
2023-11-22 12:08:03 +01:00
|
|
|
}
|
|
|
|
|
}
|