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

74 lines
2.1 KiB
C#
Raw Normal View History

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;
2024-04-24 11:14:55 +02:00
using Microsoft.AspNetCore.SignalR;
using WD7UVN_HFT_2023241.Endpoint.Services;
namespace WD7UVN_HFT_2023241.Endpoint
{
[ApiController]
[Route("api/Service")]
public class ServiceController : ControllerBase
{
public ILogicServices LogicServices { get; set; }
2024-04-24 11:14:55 +02:00
IHubContext<SignalRHub> hub;
2024-04-24 11:14:55 +02:00
public ServiceController(ILogicServices LogicServices, IHubContext<SignalRHub> hub)
{
this.LogicServices = LogicServices;
2024-04-24 11:14:55 +02:00
this.hub = hub;
}
[HttpGet()]
2023-12-13 22:04:06 +01:00
public IQueryable<Service>? ReadAllServices()
{
2023-12-13 22:04:06 +01:00
try
{
return LogicServices.CRUDOperations.ReadAllServices();
}
catch (NullReferenceException)
{
return null;
}
}
[HttpGet("{id}")]
2023-12-13 22:04:06 +01:00
public Service? ReadService(int id)
{
2023-12-13 22:04:06 +01:00
try
{
return LogicServices.CRUDOperations.ReadService(id);
}
catch (NullReferenceException)
{
return null;
}
}
2024-04-24 11:14:55 +02:00
[HttpPut()]
public void PutService([FromBody] Service e)
{
LogicServices.CRUDOperations.CreateService(e);
2024-04-24 11:14:55 +02:00
hub.Clients.All.SendAsync("ServiceCreated", e);
}
2024-04-24 11:14:55 +02:00
[HttpPost()]
public void UpdateService([FromBody] Service e)
{
LogicServices.CRUDOperations.UpdateService(e);
2024-04-24 11:14:55 +02:00
hub.Clients.All.SendAsync("ServiceUpdated", 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 DeleteService(int id)
{
2024-04-24 11:14:55 +02:00
Service service = LogicServices.CRUDOperations.ReadService(id);
LogicServices.CRUDOperations.DeleteService(id);
2024-04-24 11:14:55 +02:00
hub.Clients.All.SendAsync("ServiceDeleted", service);
}
}
}